Skip to content

Commit 9924b86

Browse files
authored
Add local time support (#40)
Co-authored-by: hfhbd <[email protected]>
1 parent edb0a85 commit 9924b86

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

postgres-native-sqldelight-dialect/src/main/kotlin/app/softwork/sqldelight/postgresdialect/PostgresNativeDialect.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PostgresNativeDialect : SqlDelightDialect by PostgreSqlDialect() {
3232
dateDataType != null -> {
3333
when (dateDataType!!.firstChild.text) {
3434
"DATE" -> PostgreSqlType.DATE
35-
//"TIME" -> PostgreSqlType.TIME
35+
"TIME" -> PostgreSqlType.TIME
3636
"TIMESTAMP" -> if (dateDataType!!.node.getChildren(null)
3737
.any { it.text == "WITH" }
3838
) PostgreSqlType.TIMESTAMP_TIMEZONE else PostgreSqlType.TIMESTAMP
@@ -65,7 +65,7 @@ internal enum class PostgreSqlType(override val javaType: TypeName): DialectType
6565
},
6666
BIG_INT(LONG),
6767
DATE(ClassName("kotlinx.datetime", "LocalDate")),
68-
//TIME(kotlinx.datetime.LocalTime::class.asTypeName()),
68+
TIME(ClassName("kotlinx.datetime", "LocalTime")),
6969
TIMESTAMP(ClassName("kotlinx.datetime", "LocalDateTime")),
7070
TIMESTAMP_TIMEZONE(ClassName("kotlinx.datetime", "Instant")),
7171
INTERVAL(ClassName("kotlin.time", "Duration")),
@@ -77,7 +77,7 @@ internal enum class PostgreSqlType(override val javaType: TypeName): DialectType
7777
when (this) {
7878
SMALL_INT, INTEGER, BIG_INT -> "bindLong"
7979
DATE -> "bindDate"
80-
//TIME -> "bindTime"
80+
TIME -> "bindTime"
8181
TIMESTAMP -> "bindLocalTimestamp"
8282
TIMESTAMP_TIMEZONE -> "bindTimestamp"
8383
INTERVAL -> "bindInterval"
@@ -93,7 +93,7 @@ internal enum class PostgreSqlType(override val javaType: TypeName): DialectType
9393
when (this) {
9494
SMALL_INT, INTEGER, BIG_INT -> "$cursorName.getLong($columnIndex)"
9595
DATE -> "$cursorName.getDate($columnIndex)"
96-
//TIME -> "$cursorName.getTime($columnIndex)"
96+
TIME -> "$cursorName.getTime($columnIndex)"
9797
TIMESTAMP -> "$cursorName.getLocalTimestamp($columnIndex)"
9898
TIMESTAMP_TIMEZONE -> "$cursorName.getTimestamp($columnIndex)"
9999
INTERVAL -> "$cursorName.getInterval($columnIndex)"

postgres-native-sqldelight-driver/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ kotlin {
2828
commonMain {
2929
dependencies {
3030
api("app.cash.sqldelight:runtime:2.0.0-alpha03")
31-
api("org.jetbrains.kotlinx:kotlinx-datetime:0.3.3")
31+
api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
3232
api("app.softwork:kotlinx-uuid-core:0.0.15")
3333
}
3434
}

postgres-native-sqldelight-driver/src/commonMain/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeDriver.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ class PostgresCursor(
300300
}
301301

302302
fun getDate(index: Int): LocalDate? = getString(index)?.toLocalDate()
303-
304-
// fun getTime(index: Int): LocalTime? = getInt(index)?.toLocalTime()
303+
fun getTime(index: Int): LocalTime? = getString(index)?.toLocalTime()
305304
fun getLocalTimestamp(index: Int): LocalDateTime? = getString(index)?.replace(" ", "T")?.toLocalDateTime()
306305
fun getTimestamp(index: Int): Instant? = getString(index)?.let {
307306
Instant.parse(it.replace(" ", "T"))
@@ -373,11 +372,10 @@ class PostgresPreparedStatement(private val parameters: Int) : SqlPreparedStatem
373372
bind(index, value?.toString(), dateOid)
374373
}
375374

376-
/*
375+
377376
fun bindTime(index: Int, value: LocalTime?) {
378377
bind(index, value?.toString(), timeOid)
379378
}
380-
*/
381379

382380
fun bindLocalTimestamp(index: Int, value: LocalDateTime?) {
383381
bind(index, value?.toString(), timestampOid)

testing/src/commonMain/sqldelight/app/softwork/sqldelight/postgresdriver/1.sqm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CREATE TABLE foo(
44
a INT PRIMARY KEY,
55
b TEXT NOT NULL,
66
date DATE NOT NULL,
7+
time TIME NOT NULL,
78
timestamp TIMESTAMP NOT NULL,
89
instant TIMESTAMPTZ NOT NULL,
910
interval INTERVAL NOT NULL,

testing/src/commonTest/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeDriverTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class PostgresNativeDriverTest {
2323
a = 42,
2424
b = "answer",
2525
date = LocalDate(2020, Month.DECEMBER, 12),
26+
time = LocalTime(12, 42, 0, 0),
2627
timestamp = LocalDateTime(2014, Month.AUGUST, 1, 12, 1, 2, 0),
2728
instant = Instant.fromEpochMilliseconds(10L),
2829
interval = 42.seconds,
@@ -44,12 +45,13 @@ class PostgresNativeDriverTest {
4445
val queries = NativePostgres(driver).fooQueries
4546
NativePostgres.Schema.migrate(driver, 0, NativePostgres.Schema.version)
4647
queries.copy()
47-
val result = driver.copy("42,answer,2020-12-12,2014-08-01T12:01:02.0000,1970-01-01T00:00:00.010Z,PT42S,00000000-0000-0000-0000-000000000000")
48+
val result = driver.copy("42,answer,2020-12-12,12:42:00.0000,2014-08-01T12:01:02.0000,1970-01-01T00:00:00.010Z,PT42S,00000000-0000-0000-0000-000000000000")
4849
assertEquals(1, result)
4950
val foo = Foo(
5051
a = 42,
5152
b = "answer",
5253
date = LocalDate(2020, Month.DECEMBER, 12),
54+
time = LocalTime(12, 42, 0, 0),
5355
timestamp = LocalDateTime(2014, Month.AUGUST, 1, 12, 1, 2, 0),
5456
instant = Instant.fromEpochMilliseconds(10L),
5557
interval = 42.seconds,

0 commit comments

Comments
 (0)