withCustomConverters
Builds a Sqlite with custom type converters registered via a SqliteCustomConvertersBuilder DSL block.
use
forTypeto register a converter keyed by the declared SQL type name (as written inCREATE TABLE);use
forColumnto register a converter keyed by column name (takes precedence overforTypefor the named column).
Both DSL functions accept two generic type parameters:
T— the storage class of the raw stored valueString,Int,Long,Double, or ByteArray or their nullable variants).R— the target Kotlin type of the resulting DataFrame column; must be reified.
The converting lambda receives the raw value and returns the converted result;
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)Content copied to clipboard