readDataFrameSchema

fun DbConnectionConfig.readDataFrameSchema(sqlQueryOrTableName: String, dbType: DbType? = null): DataFrameSchema

Retrieves the schema of an SQL query result or the SQL table using the provided database configuration.

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 schema of the SQL query as a DataFrameSchema object.

Parameters

sqlQueryOrTableName

the SQL query to execute and retrieve the schema from.

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.


fun DataSource.readDataFrameSchema(sqlQueryOrTableName: String, dbType: DbType? = null): DataFrameSchema

Retrieves the schema of an SQL query result or the SQL table using the provided DataSource.

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)

// Get schema for a table
val tableSchema = dataSource.getDataFrameSchema("customers")

// Or get schema for a query
val querySchema = dataSource.getDataFrameSchema("SELECT name, age FROM customers WHERE age > 25")

// Inspect the schema
println(tableSchema.columns)

Return

the schema of the SQL query as a DataFrameSchema object.

Parameters

sqlQueryOrTableName

the SQL query to execute or name of the SQL table. It should be a name of one of the existing SQL tables, or the SQL query should start from SELECT and contain one query for reading data without any manipulation. It should not contain ; symbol.

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.

See also


fun Connection.readDataFrameSchema(sqlQueryOrTableName: String, dbType: DbType? = null): DataFrameSchema

Retrieves the schema of an SQL query result or the SQL table using the provided database configuration.

Return

the schema of the SQL query as a DataFrameSchema object.

Parameters

sqlQueryOrTableName

the SQL query to execute and retrieve the schema from.

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.


Retrieves the schema from ResultSet.

NOTE: This function will not close connection and result set and not retrieve data from the result set.

Return

the schema of the ResultSet as a DataFrameSchema object.

Parameters

dbType

the type of database that the ResultSet belongs to, could be a custom object, provided by user.