From b306fbb52f972efc81b195ceeaa118a4848f26e4 Mon Sep 17 00:00:00 2001 From: hfhbd Date: Sat, 11 Jun 2022 21:50:30 +0200 Subject: [PATCH] Postgres: Add date_trunc --- .../postgresql/PostgreSqlTypeResolver.kt | 1 + .../postgresql/integration/Dates.sq | 5 ++++- .../postgresql/integration/PostgreSqlTest.kt | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlTypeResolver.kt b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlTypeResolver.kt index a010234ba5d..a345caf4715 100644 --- a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlTypeResolver.kt +++ b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlTypeResolver.kt @@ -83,6 +83,7 @@ open class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : Ty "coalesce", "ifnull" -> encapsulatingType(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB) "max" -> encapsulatingType(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB).asNullable() "min" -> encapsulatingType(exprList, BLOB, TEXT, SMALL_INT, INTEGER, PostgreSqlType.INTEGER, BIG_INT, REAL).asNullable() + "date_trunc" -> encapsulatingType(exprList, TIMESTAMP_TIMEZONE, TIMESTAMP) else -> null } diff --git a/sqldelight-gradle-plugin/src/test/integration-postgresql/src/main/sqldelight/app/cash/sqldelight/postgresql/integration/Dates.sq b/sqldelight-gradle-plugin/src/test/integration-postgresql/src/main/sqldelight/app/cash/sqldelight/postgresql/integration/Dates.sq index 4d19c95bcdf..1f148d89a11 100644 --- a/sqldelight-gradle-plugin/src/test/integration-postgresql/src/main/sqldelight/app/cash/sqldelight/postgresql/integration/Dates.sq +++ b/sqldelight-gradle-plugin/src/test/integration-postgresql/src/main/sqldelight/app/cash/sqldelight/postgresql/integration/Dates.sq @@ -8,4 +8,7 @@ CREATE TABLE dates( insertDate: INSERT INTO dates VALUES (?, ?, ?, ?) -RETURNING *; \ No newline at end of file +RETURNING *; + +selectDateTrunc: +SELECT date_trunc('hour', timestamp), date_trunc('hour', timestamp_with_timezone) FROM dates; diff --git a/sqldelight-gradle-plugin/src/test/integration-postgresql/src/test/kotlin/app/cash/sqldelight/postgresql/integration/PostgreSqlTest.kt b/sqldelight-gradle-plugin/src/test/integration-postgresql/src/test/kotlin/app/cash/sqldelight/postgresql/integration/PostgreSqlTest.kt index 73560930b13..8a47712d585 100644 --- a/sqldelight-gradle-plugin/src/test/integration-postgresql/src/test/kotlin/app/cash/sqldelight/postgresql/integration/PostgreSqlTest.kt +++ b/sqldelight-gradle-plugin/src/test/integration-postgresql/src/test/kotlin/app/cash/sqldelight/postgresql/integration/PostgreSqlTest.kt @@ -89,6 +89,25 @@ class PostgreSqlTest { ) } + @Test fun testDateTrunc() { + database.datesQueries.insertDate( + date = LocalDate.of(2020, 1, 1), + time = LocalTime.of(21, 30, 59, 10000), + timestamp = LocalDateTime.of(2020, 1, 1, 21, 30, 59, 10000), + timestamp_with_timezone = OffsetDateTime.of(1980, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)) + ).executeAsOne() + + assertThat( + database.datesQueries.selectDateTrunc().executeAsOne() + ) + .isEqualTo( + SelectDateTrunc( + date_trunc = LocalDateTime.of(2020, 1, 1, 21, 0, 0, 0), + date_trunc_ = OffsetDateTime.of(1980, 4, 9, 20, 0, 0, 0, ZoneOffset.ofHours(0)) + ) + ) + } + @Test fun testSerial() { database.run { oneEntityQueries.transaction {