readDataFrame

fun DbConnectionConfig.readDataFrame(sqlQueryOrTableName: String, limit: Int? = null, inferNullability: Boolean = true, dbType: DbType? = null, validation: SqlValidation = SqlValidation.None, configureStatement: (PreparedStatement) -> Unit = {}): AnyFrame

Converts the result of an SQL query or SQL table (by name) to the DataFrame.

Note that if input dataframe contains duplicate column names, they will be automatically renamed in the resulting DataFrame.

Default Behavior:

If DbConnectionConfig.readOnly is true (which is the default), the connection will be:

  • explicitly set as read-only via Connection.setReadOnly(true)

  • used with autoCommit = false

  • automatically rolled back after reading, ensuring no changes to the database

Even if DbConnectionConfig.readOnly is set to false, the library still prevents data-modifying queries and only permits safe SELECT operations internally.

Return

the DataFrame containing the result of the SQL query.

Parameters

sqlQueryOrTableName

the SQL query to execute or the name of an SQL table.

limit

the maximum number of rows to retrieve from the result of the SQL query execution. null (default) means no limit - all available rows will be fetched or positive integer (e.g., 100) - fetch at most that many rows

inferNullability

indicates how the column nullability should be inferred.

dbType

the type of database, could be a custom object, provided by user, optional, default is null, in that case the dbType will be recognized from the DbConnectionConfig.

validation

controls SQL query validation when a query (not a table name) is provided. SqlValidation.ReadOnly blocks DDL/DML; SqlValidation.None (default) skips validation. Table name validation is always strict regardless of this parameter.

configureStatement

optional lambda to configure the PreparedStatement before execution. This allows for custom tuning of fetch size, query timeout, and other JDBC parameters.


fun Connection.readDataFrame(sqlQueryOrTableName: String, limit: Int? = null, inferNullability: Boolean = true, dbType: DbType? = null, validation: SqlValidation = SqlValidation.None, configureStatement: (PreparedStatement) -> Unit = {}): AnyFrame

Converts the result of an SQL query or SQL table (by name) to the DataFrame.

Note that if input dataframe contains duplicate column names, they will be automatically renamed in the resulting DataFrame.

Return

the DataFrame containing the result of the SQL query.

Parameters

sqlQueryOrTableName

the SQL query to execute or the name of an SQL table.

limit

the maximum number of rows to retrieve from the result of the SQL query execution. null (default) means no limit - all available rows will be fetched or positive integer (e.g., 100) - fetch at most that many rows

inferNullability

indicates how the column nullability should be inferred.

dbType

the type of database, could be a custom object, provided by user, optional, default is null, in that case the dbType will be recognized from the Connection.

validation

controls SQL query validation when a query (not a table name) is provided. SqlValidation.ReadOnly blocks DDL/DML; SqlValidation.None (default) skips validation. Table name validation is always strict regardless of this parameter.

configureStatement

optional lambda to configure the PreparedStatement before execution. This allows for custom tuning of fetch size, query timeout, and other JDBC parameters.


fun DataSource.readDataFrame(sqlQueryOrTableName: String, limit: Int? = null, inferNullability: Boolean = true, dbType: DbType? = null, validation: SqlValidation = SqlValidation.None, configureStatement: (PreparedStatement) -> Unit = {}): AnyFrame

Converts the result of an SQL query or SQL table (by name) to the DataFrame.

Note that if input dataframe contains duplicate column names, they will be automatically renamed in the resulting DataFrame.

Example with HikariCP:

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource

val config = HikariConfig().apply {
jdbcUrl = "jdbc:postgresql://localhost:5432/mydb"
username = "user"
password = "password"
}
val dataSource = HikariDataSource(config)

// Read from a table
val customersDF = dataSource.readDataFrame("customers", limit = 100)

// Or execute a query
val queryDF = dataSource.readDataFrame("SELECT * FROM orders WHERE amount > 100")

Return

the DataFrame containing the result of the SQL query.

Parameters

sqlQueryOrTableName

the SQL query to execute or the name of an SQL table.

limit

the maximum number of rows to retrieve from the result of the SQL query execution. null (default) means no limit - all available rows will be fetched or positive integer (e.g., 100) - fetch at most that many rows

inferNullability

indicates how the column nullability should be inferred.

dbType

the type of database, could be a custom object, provided by user, optional, default is null, in that case the dbType will be recognized from the DataSource.

validation

controls SQL query validation when a query (not a table name) is provided. SqlValidation.ReadOnly blocks DDL/DML; SqlValidation.None (default) skips validation. Table name validation is always strict regardless of this parameter.

configureStatement

optional lambda to configure the PreparedStatement before execution. This allows for custom tuning of fetch size, query timeout, and other JDBC parameters.

See also


fun ResultSet.readDataFrame(dbType: DbType, limit: Int? = null, inferNullability: Boolean = true): AnyFrame

Reads the data from a ResultSet and converts it into a DataFrame.

Note that if input dataframe contains duplicate column names, they will be automatically renamed in the resulting DataFrame.

A ResultSet object maintains a cursor pointing to its current row of data. By default, a ResultSet object is not updatable and has a cursor that can only move forward. Therefore, you can iterate through it only once, from the first row to the last row.

For more details, refer to the official Java documentation on ResultSet.

NOTE: Reading from the ResultSet could potentially change its state.

Return

the DataFrame generated from the ResultSet data.

Parameters

dbType

the type of database that the ResultSet belongs to.

limit

the maximum number of rows to read from the ResultSet. null (default) means no limit - all available rows will be fetched or positive integer (e.g., 100) - fetch at most that many rows

inferNullability

indicates how the column nullability should be inferred.

See also


fun ResultSet.readDataFrame(connection: Connection, limit: Int? = null, inferNullability: Boolean = true, dbType: DbType? = null): AnyFrame

Reads the data from a ResultSet and converts it into a DataFrame.

Note that if input dataframe contains duplicate column names, they will be automatically renamed in the resulting DataFrame.

A ResultSet object maintains a cursor pointing to its current row of data. By default, a ResultSet object is not updatable and has a cursor that can only move forward. Therefore, you can iterate through it only once, from the first row to the last row.

For more details, refer to the official Java documentation on ResultSet.

NOTE: Reading from the ResultSet could potentially change its state.

Return

the DataFrame generated from the ResultSet data.

Parameters

connection

the connection to the database (it's required to extract the database type) that the ResultSet belongs to.

limit

the maximum number of rows to read from the ResultSet. null (default) means no limit - all available rows will be fetched or positive integer (e.g., 100) - fetch at most that many rows

inferNullability

indicates how the column nullability should be inferred.

dbType

the type of database, could be a custom object, provided by user, optional, default is null, in that case the dbType will be recognized from the ResultSet.

See also