From e60c2658e43f5da50cab99e50dfc3c0fa30faa7b Mon Sep 17 00:00:00 2001 From: Philip Wedemann <22521688+hfhbd@users.noreply.github.com> Date: Wed, 3 Aug 2022 16:08:24 +0200 Subject: [PATCH] Add NOW() to PostgreSQL (#3403) Co-authored-by: hfhbd --- .../dialects/postgresql/PostgreSqlTypeResolver.kt | 1 + .../src/test/fixtures_postgresql/custom-functions/Test.s | 5 +++++ .../app/cash/sqldelight/postgresql/integration/Dates.sq | 3 +++ .../sqldelight/postgresql/integration/PostgreSqlTest.kt | 6 ++++++ 4 files changed, 15 insertions(+) create mode 100644 dialects/postgresql/src/test/fixtures_postgresql/custom-functions/Test.s 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 c69c35d7ef0..08ee34bcfaf 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 @@ -86,6 +86,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes "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) + "now" -> IntermediateType(TIMESTAMP_TIMEZONE) else -> null } diff --git a/dialects/postgresql/src/test/fixtures_postgresql/custom-functions/Test.s b/dialects/postgresql/src/test/fixtures_postgresql/custom-functions/Test.s new file mode 100644 index 00000000000..8a7bf4a793a --- /dev/null +++ b/dialects/postgresql/src/test/fixtures_postgresql/custom-functions/Test.s @@ -0,0 +1,5 @@ +CREATE TABLE foo ( +bar TIMESTAMPTZ NOT NULL +); + +SELECT NOW() AS now, date_trunc('hour', bar) AS d FROM foo; 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 1f148d89a11..115883395cf 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 @@ -12,3 +12,6 @@ RETURNING *; selectDateTrunc: SELECT date_trunc('hour', timestamp), date_trunc('hour', timestamp_with_timezone) FROM dates; + +selectNow: +SELECT NOW(); 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 848652b17fd..28ebb8b8957 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 @@ -126,6 +126,12 @@ class PostgreSqlTest { } } + @Test fun now() { + val now = database.datesQueries.selectNow().executeAsOne() + assertThat(now).isNotNull() + assertThat(now).isGreaterThan(OffsetDateTime.MIN) + } + @Test fun successfulOptimisticLock() { with(database.withLockQueries) { val row = insertText("sup").executeAsOne()