@@ -32,10 +32,11 @@ Also, there are a few **extension functions** available on `Connection`,
32
32
** NOTE:** This is an experimental module, and for now,
33
33
we only support four databases: MS SQL, MariaDB, MySQL, PostgreSQL, and SQLite.
34
34
35
+ Moreover, since release 0.15 we support the possibility to register custom SQL database, read more in our [ guide] ( readSqlFromCustomDatabase.md ) .
36
+
35
37
Additionally, support for JSON and date-time types is limited.
36
38
Please take this into consideration when using these functions.
37
39
38
-
39
40
## Getting started with reading from SQL database in Gradle Project
40
41
41
42
In the first, you need to add a dependency
@@ -70,15 +71,15 @@ implementation("com.mysql:mysql-connector-j:$version")
70
71
71
72
Maven Central version could be found [ here] ( https://mvnrepository.com/artifact/com.mysql/mysql-connector-j ) .
72
73
73
- For SQLite:
74
+ For ** SQLite** :
74
75
75
76
``` kotlin
76
77
implementation(" org.xerial:sqlite-jdbc:$version " )
77
78
```
78
79
79
80
Maven Central version could be found [ here] ( https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc ) .
80
81
81
- For MS SQL:
82
+ For ** MS SQL** :
82
83
83
84
``` kotlin
84
85
implementation(" com.microsoft.sqlserver:mssql-jdbc:$version " )
@@ -158,14 +159,17 @@ otherwise, it will be considered non-nullable for the newly created `DataFrame`
158
159
These functions read all data from a specific table in the database.
159
160
Variants with a limit parameter restrict how many rows will be read from the table.
160
161
161
- ** readSqlTable(dbConfig: DbConnectionConfig, tableName: String, limit: Int, inferNullability: Boolean): AnyFrame**
162
+ ** readSqlTable(dbConfig: DbConnectionConfig, tableName: String, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
162
163
163
164
Read all data from a specific table in the SQL database and transform it into an ` AnyFrame ` object.
164
165
165
166
The ` dbConfig: DbConnectionConfig ` parameter represents the configuration for a database connection,
166
167
created under the hood and managed by the library.
167
168
Typically, it requires a URL, username, and password.
168
169
170
+ The ` dbType ` parameter is the type of database, could be a custom object, provided by user, optional, default is ` null ` ,
171
+ to know more, read the [ guide] ( readSqlFromCustomDatabase.md ) .
172
+
169
173
``` kotlin
170
174
import org.jetbrains.kotlinx.dataframe.io.DbConnectionConfig
171
175
@@ -180,7 +184,7 @@ The `limit: Int` parameter allows setting the maximum number of records to be re
180
184
val users = DataFrame .readSqlTable(dbConfig, " Users" , limit = 100 )
181
185
```
182
186
183
- ** readSqlTable(connection: Connection, tableName: String, limit: Int, inferNullability: Boolean): AnyFrame**
187
+ ** readSqlTable(connection: Connection, tableName: String, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
184
188
185
189
Another variant, where instead of ` dbConfig: DbConnectionConfig ` we use a JDBC connection: ` Connection ` object.
186
190
@@ -210,7 +214,7 @@ val users = connection.readDataFrame("Users", 100)
210
214
connection.close()
211
215
```
212
216
213
- ** Connection.readDataFrame(sqlQueryOrTableName: String, limit: Int, inferNullability: Boolean): AnyFrame**
217
+ ** Connection.readDataFrame(sqlQueryOrTableName: String, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
214
218
215
219
Read all data from a specific table in the SQL database and transform it into an ` AnyFrame ` object.
216
220
@@ -222,7 +226,7 @@ It should not contain `;` symbol.
222
226
223
227
All other parameters are described above.
224
228
225
- ** DbConnectionConfig.readDataFrame(sqlQueryOrTableName: String, limit: Int, inferNullability: Boolean): AnyFrame**
229
+ ** DbConnectionConfig.readDataFrame(sqlQueryOrTableName: String, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
226
230
227
231
If you do not have a connection object or need to run a quick,
228
232
isolated experiment reading data from an SQL database,
@@ -233,7 +237,7 @@ you can delegate the creation of the connection to `DbConnectionConfig`.
233
237
These functions execute an SQL query on the database and convert the result into a ` DataFrame ` object.
234
238
If a limit is provided, only that many rows will be returned from the result.
235
239
236
- ** readSqlQuery(dbConfig: DbConnectionConfig, sqlQuery: String, limit: Int, inferNullability: Boolean): AnyFrame**
240
+ ** readSqlQuery(dbConfig: DbConnectionConfig, sqlQuery: String, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
237
241
238
242
Execute a specific SQL query on the SQL database and retrieve the resulting data as an AnyFrame.
239
243
@@ -249,7 +253,7 @@ val dbConfig = DbConnectionConfig("URL_TO_CONNECT_DATABASE", "USERNAME", "PASSWO
249
253
val df = DataFrame .readSqlQuery(dbConfig, " SELECT * FROM Users WHERE age > 35" )
250
254
```
251
255
252
- ** readSqlQuery(connection: Connection, sqlQuery: String, limit: Int, inferNullability: Boolean): AnyFrame**
256
+ ** readSqlQuery(connection: Connection, sqlQuery: String, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
253
257
254
258
Another variant, where instead of ` dbConfig: DbConnectionConfig ` we use a JDBC connection: ` Connection ` object.
255
259
@@ -301,16 +305,18 @@ The `dbType: DbType` parameter specifies the type of our database (e.g., Postgre
301
305
supported by a library.
302
306
Currently, the following classes are available: ` H2, MsSql, MariaDb, MySql, PostgreSql, Sqlite ` .
303
307
308
+ Also, users have an ability to pass objects, describing their custom databases, more information in [ guide] ( readSqlFromCustomDatabase.md ) .
309
+
304
310
``` kotlin
305
311
import org.jetbrains.kotlinx.dataframe.io.db.PostgreSql
306
312
import java.sql.ResultSet
307
313
308
314
val df = DataFrame .readResultSet(resultSet, PostgreSql )
309
315
```
310
316
311
- ** readResultSet(resultSet: ResultSet, connection: Connection, limit: Int, inferNullability: Boolean): AnyFrame**
317
+ ** readResultSet(resultSet: ResultSet, connection: Connection, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
312
318
313
- Another variant, where instead of ` dbType: DbType ` we use a JDBC connection: ` Connection ` object.
319
+ Another variant, we use a JDBC connection: ` Connection ` object.
314
320
315
321
``` kotlin
316
322
import java.sql.Connection
@@ -340,7 +346,7 @@ val df = rs.readDataFrame(connection, 10)
340
346
connection.close()
341
347
```
342
348
343
- ** ResultSet.readDataFrame(connection: Connection, limit: Int, inferNullability: Boolean): AnyFrame**
349
+ ** ResultSet.readDataFrame(connection: Connection, limit: Int, inferNullability: Boolean, dbType: DbType? ): AnyFrame**
344
350
345
351
Reads the data from a ` ResultSet ` and converts it into a ` DataFrame ` .
346
352
@@ -352,7 +358,7 @@ that the `ResultSet` belongs to.
352
358
These functions read all data from all tables in the connected database.
353
359
Variants with a limit parameter restrict how many rows will be read from each table.
354
360
355
- ** readAllSqlTables(dbConfig: DbConnectionConfig, limit: Int, inferNullability: Boolean): Map\< String, AnyFrame>**
361
+ ** readAllSqlTables(dbConfig: DbConnectionConfig, limit: Int, inferNullability: Boolean, dbType: DbType? ): Map\< String, AnyFrame>**
356
362
357
363
Retrieves data from all the non-system tables in the SQL database and returns them as a map of table names to ` AnyFrame ` objects.
358
364
@@ -368,7 +374,7 @@ val dbConfig = DbConnectionConfig("URL_TO_CONNECT_DATABASE", "USERNAME", "PASSWO
368
374
val dataframes = DataFrame .readAllSqlTables(dbConfig)
369
375
```
370
376
371
- ** readAllSqlTables(connection: Connection, limit: Int, inferNullability: Boolean): Map\< String, AnyFrame>**
377
+ ** readAllSqlTables(connection: Connection, limit: Int, inferNullability: Boolean, dbType: DbType? ): Map\< String, AnyFrame>**
372
378
373
379
Another variant, where instead of ` dbConfig: DbConnectionConfig ` we use a JDBC connection: ` Connection ` object.
374
380
@@ -389,7 +395,7 @@ The purpose of these functions is to facilitate the retrieval of table schema.
389
395
By providing a table name and either a database configuration or connection,
390
396
these functions return the [ DataFrameSchema] ( schema.md ) of the specified table.
391
397
392
- ** getSchemaForSqlTable(dbConfig: DbConnectionConfig, tableName: String): DataFrameSchema**
398
+ ** getSchemaForSqlTable(dbConfig: DbConnectionConfig, tableName: String, dbType: DbType? ): DataFrameSchema**
393
399
394
400
This function captures the schema of a specific table from an SQL database.
395
401
@@ -405,7 +411,7 @@ val dbConfig = DbConnectionConfig("URL_TO_CONNECT_DATABASE", "USERNAME", "PASSWO
405
411
val schema = DataFrame .getSchemaForSqlTable(dbConfig, " Users" )
406
412
```
407
413
408
- ** getSchemaForSqlTable(connection: Connection, tableName: String): DataFrameSchema**
414
+ ** getSchemaForSqlTable(connection: Connection, tableName: String, dbType: DbType? ): DataFrameSchema**
409
415
410
416
Another variant, where instead of ` dbConfig: DbConnectionConfig ` we use a JDBC connection: ` Connection ` object.
411
417
@@ -427,7 +433,7 @@ These functions return the schema of an SQL query result.
427
433
Once you provide a database configuration or connection and an SQL query,
428
434
they return the [ DataFrameSchema] ( schema.md ) of the query result.
429
435
430
- ** getSchemaForSqlQuery(dbConfig: DbConnectionConfig, sqlQuery: String): DataFrameSchema**
436
+ ** getSchemaForSqlQuery(dbConfig: DbConnectionConfig, sqlQuery: String, dbType: DbType? ): DataFrameSchema**
431
437
432
438
This function executes an SQL query on the database and then retrieves the resulting schema.
433
439
@@ -443,7 +449,7 @@ val dbConfig = DbConnectionConfig("URL_TO_CONNECT_DATABASE", "USERNAME", "PASSWO
443
449
val schema = DataFrame .getSchemaForSqlQuery(dbConfig, " SELECT * FROM Users WHERE age > 35" )
444
450
```
445
451
446
- ** getSchemaForSqlQuery(connection: Connection, sqlQuery: String): DataFrameSchema**
452
+ ** getSchemaForSqlQuery(connection: Connection, sqlQuery: String, dbType: DbType? ): DataFrameSchema**
447
453
448
454
Another variant, where instead of ` dbConfig: DbConnectionConfig ` we use a JDBC connection: ` Connection ` object.
449
455
@@ -472,11 +478,11 @@ val schema = connection.getDataFrameSchema("SELECT * FROM Users WHERE age > 35")
472
478
473
479
connection.close()
474
480
```
475
- ** Connection.getDataFrameSchema(sqlQueryOrTableName: String): DataFrameSchema**
481
+ ** Connection.getDataFrameSchema(sqlQueryOrTableName: String, dbType: DbType? ): DataFrameSchema**
476
482
477
483
Retrieves the schema of an SQL query result or an SQL table using the provided database configuration.
478
484
479
- ** DbConnectionConfig.getDataFrameSchema(sqlQueryOrTableName: String): DataFrameSchema**
485
+ ** DbConnectionConfig.getDataFrameSchema(sqlQueryOrTableName: String, dbType: DbType? ): DataFrameSchema**
480
486
481
487
Retrieves the schema of an SQL query result or an SQL table using the provided database configuration.
482
488
@@ -507,49 +513,19 @@ The `dbType: DbType` parameter specifies the type of our database (e.g., Postgre
507
513
supported by a library.
508
514
Currently, the following classes are available: ` H2, MariaDb, MySql, PostgreSql, Sqlite ` .
509
515
516
+ Also, users have an ability to pass objects, describing their custom databases, more information in [ guide] ( readSqlFromCustomDatabase.md ) .
517
+
510
518
``` kotlin
511
519
import org.jetbrains.kotlinx.dataframe.io.db.PostgreSql
512
520
import java.sql.ResultSet
513
521
514
522
val schema = DataFrame .getSchemaForResultSet(resultSet, PostgreSql )
515
523
```
516
524
517
- ** getSchemaForResultSet(connection: Connection, sqlQuery: String): DataFrameSchema**
518
-
519
- Another variant, where instead of ` dbType: DbType ` we use a JDBC connection: ` Connection ` object.
520
-
521
- ``` kotlin
522
- import java.sql.Connection
523
- import java.sql.DriverManager
524
-
525
- val connection = DriverManager .getConnection(" URL_TO_CONNECT_DATABASE" )
526
-
527
- val schema = DataFrame .getSchemaForResultSet(resultSet, connection)
528
-
529
- connection.close()
530
- ```
531
-
532
525
### Extension functions for schema reading from the ResultSet
533
526
534
527
The same example, rewritten with the extension function:
535
528
536
- ``` kotlin
537
- import java.sql.Connection
538
- import java.sql.DriverManager
539
-
540
- val connection = DriverManager .getConnection(" URL_TO_CONNECT_DATABASE" )
541
-
542
- val schema = resultSet.getDataFrameSchema(connection)
543
-
544
- connection.close()
545
- ```
546
-
547
- if you are using this extension function
548
-
549
- ** ResultSet.getDataFrameSchema(connection: Connection): DataFrameSchema**
550
-
551
- or
552
-
553
529
``` kotlin
554
530
import org.jetbrains.kotlinx.dataframe.io.db.PostgreSql
555
531
import java.sql.ResultSet
@@ -566,7 +542,7 @@ based on
566
542
These functions return a list of all [ ` DataFrameSchema ` ] ( schema.md ) from all the non-system tables in the SQL database.
567
543
They can be called with either a database configuration or a connection.
568
544
569
- ** getSchemaForAllSqlTables(dbConfig: DbConnectionConfig): Map\< String, DataFrameSchema>**
545
+ ** getSchemaForAllSqlTables(dbConfig: DbConnectionConfig, dbType: DbType? ): Map\< String, DataFrameSchema>**
570
546
571
547
This function retrieves the schema of all tables from an SQL database
572
548
and returns them as a map of table names to [ ` DataFrameSchema ` ] ( schema.md ) objects.
@@ -583,7 +559,7 @@ val dbConfig = DbConnectionConfig("URL_TO_CONNECT_DATABASE", "USERNAME", "PASSWO
583
559
val schemas = DataFrame .getSchemaForAllSqlTables(dbConfig)
584
560
```
585
561
586
- ** getSchemaForAllSqlTables(connection: Connection): Map\< String, DataFrameSchema>**
562
+ ** getSchemaForAllSqlTables(connection: Connection, dbType: DbType? ): Map\< String, DataFrameSchema>**
587
563
588
564
This function retrieves the schema of all tables using a JDBC connection: ` Connection ` object
589
565
and returns them as a list of [ ` DataFrameSchema ` ] ( schema.md ) .
0 commit comments