From 432d0841c7fc82dfefa1f588888cee83da28617f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?griff=20=D1=96=E2=8A=99?= <346896+griffio@users.noreply.github.com> Date: Thu, 27 Mar 2025 13:19:13 +0000 Subject: [PATCH 1/2] Update PostgreSqlType.kt Make the PostgreSqlType public so it can be accessed in third party modules --- .../app/cash/sqldelight/dialects/postgresql/PostgreSqlType.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlType.kt b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlType.kt index cb66bc336c1..b6d2674a77e 100644 --- a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlType.kt +++ b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/PostgreSqlType.kt @@ -10,7 +10,7 @@ import com.squareup.kotlinpoet.SHORT import com.squareup.kotlinpoet.STRING import com.squareup.kotlinpoet.TypeName -internal enum class PostgreSqlType(override val javaType: TypeName) : DialectType { +enum class PostgreSqlType(override val javaType: TypeName) : DialectType { SMALL_INT(SHORT), INTEGER(INT), BIG_INT(LONG), From 7a817c190ca5b8789ed6afa78ebcbf07114acb13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?griff=20=D1=96=E2=8A=99?= <346896+griffio@users.noreply.github.com> Date: Thu, 27 Mar 2025 13:20:41 +0000 Subject: [PATCH 2/2] Update PostgreSqlTypeResolver.kt open PostgreSqlTypeResolver for extension and polymorphic calls booleanBinaryExprTypes is a new protected method to allow subclasses to add types --- .../postgresql/PostgreSqlTypeResolver.kt | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) 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 78e3598f716..9a678452943 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 @@ -48,7 +48,7 @@ import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.asTypeName -class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeResolver by parentResolver { +open class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeResolver by parentResolver { override fun definitionType(typeName: SqlTypeName): IntermediateType = with(typeName) { check(this is PostgreSqlTypeName) val type = IntermediateType( @@ -250,6 +250,11 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes } } + // dialects or modules would need to extend this if they add types that use operators in binaryExprChildTypesResolvingToBool + protected open fun booleanBinaryExprTypes(): Array { + return booleanBinaryExprTypes + } + private fun SqlExpr.postgreSqlType(): IntermediateType = when (this) { is SqlIsExpr -> IntermediateType(BOOLEAN) is SqlBinaryExpr -> { @@ -262,23 +267,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes (this is SqlBinaryAddExpr || this is SqlBinaryMultExpr || this is SqlBinaryPipeExpr) && exprListNullability.any { it } }, - SMALL_INT, - PostgreSqlType.INTEGER, - INTEGER, - BIG_INT, - REAL, - PostgreSqlType.NUMERIC, - TEXT, - BLOB, - BOOLEAN, - DATE, - PostgreSqlType.UUID, - PostgreSqlType.INTERVAL, - PostgreSqlType.TIMESTAMP_TIMEZONE, - PostgreSqlType.TIMESTAMP, - PostgreSqlType.TIME, - PostgreSqlType.JSON, - PostgreSqlType.TSVECTOR, + typeOrder = booleanBinaryExprTypes(), ) } } @@ -343,6 +332,26 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes } companion object { + + private val booleanBinaryExprTypes: Array = arrayOf( + SMALL_INT, + PostgreSqlType.INTEGER, + INTEGER, + BIG_INT, + REAL, + PostgreSqlType.NUMERIC, + TEXT, + BLOB, + DATE, + PostgreSqlType.UUID, + PostgreSqlType.INTERVAL, + PostgreSqlType.TIMESTAMP_TIMEZONE, + PostgreSqlType.TIMESTAMP, + PostgreSqlType.TIME, + PostgreSqlType.JSON, + PostgreSqlType.TSVECTOR, + BOOLEAN, // is last as expected that boolean expression resolve to boolean + ) private val binaryExprChildTypesResolvingToBool = TokenSet.create( SqlTypes.EQ, SqlTypes.EQ2,