SqliteCustomTypeConverter
A user-provided converter from an SQLite declared column type name to the DataFrame column type and a lambda that converts each stored value.
The type parameter T (and expectedType — this type as KType) is the storage-class type of the column — the actual class of values returned by java.sql.ResultSet.getObject for that column. Used as a result of DbType.getExpectedJdbcType. The Xerial SQLite JDBC driver's getObject(int) returns exactly one of the following, chosen from the runtime storage class of the value (not the declared column type):
java.lang.Integer(Int) —INTEGERvalues that fit inInt(-2^31 .. 2^31 - 1)java.lang.Long(Long) —INTEGERvalues outsideIntrangejava.lang.Double(Double) —REALvaluesjava.lang.String(String) —TEXTvaluesbyte[](ByteArray) —BLOBvaluesnull— NULL
The driver never produces any other type — in particular, declared DATE / TIME / TIMESTAMP / BOOLEAN columns still surface as one of the six types above, not as their Java-time / Boolean equivalent.
The type parameter R (and resultingType — this type as KType) is the resulting type of the column after applying convert. Used as a result of DbType.getPreprocessedValueType.
Example
val format = LocalDateTime.Format {
year(); char('-'); monthNumber(); char('-'); day()
char(' ')
hour(); char(':'); minute(); char(':'); second()
chars(" UTC")
}
val sqlite = Sqlite.withCustomConverters {
// Parse a custom text representation into Instant.
forType("MY_DATETIME") { raw: String? ->
raw?.let { LocalDateTime.parse(it, format).toInstant(TimeZone.UTC) }
}
// Explicitly treat values columns with of `LONGVARCHAR` type as nullable strings.
// This is necessary because SQLite type affinity assigns those columns the `NUMERIC`
// base type, causing DataFrame to expect integer values.
// No conversion is performed; only the expected value type is specified.
forType<String?>("LONGVARCHAR")
// Override conversion for a specific "time" column.
// Column-specific converters take precedence over type-specific converters.
forColumn("time") { raw: String -> raw.toDouble() }
}
val df = DataFrame.readSqlTable(connection, "events", dbType = sqlite)