From fe6e84d5bf6ac7833368a88fa1654ced750966ca Mon Sep 17 00:00:00 2001 From: Dattatray Gund Date: Thu, 7 Apr 2022 19:36:50 +0530 Subject: [PATCH 1/3] Fix: Supporting all types array aggregations --- .../kotlin/norm/codegen/CodeGeneratorTest.kt | 10 +++++ .../gen/int8-type-array-agg.expected.txt | 43 +++++++++++++++++++ cli/src/test/resources/init_postgres.sql | 9 ++++ .../src/main/kotlin/norm/model/ColumnModel.kt | 4 +- .../typemapper/DbToKtDefaultTypeMapper.kt | 14 ++++-- 5 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 cli/src/test/resources/gen/int8-type-array-agg.expected.txt diff --git a/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt b/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt index 41057c3..345ff52 100644 --- a/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt +++ b/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt @@ -119,5 +119,15 @@ class CodeGeneratorTest : StringSpec() { generatedFileContent shouldBe "/gen/uuid-column-type-generator.expected.txt".readAsResource() } + + "should generate kotlin file with sql ARRAY type mapped to kotlin.Array" { + val generatedFileContent = codegen( + connection, + "SELECT ARRAY_AGG(id) AS ids, ARRAY_AGG(documentId) AS docIds, ARRAY_AGG(type) AS arr_types, STRING_AGG(type, '-') AS str_types FROM requests GROUP BY type", + "com.foo", + "Foo" + ) + generatedFileContent shouldBe "/gen/int8-type-array-agg.expected.txt".readAsResource() + } } } diff --git a/cli/src/test/resources/gen/int8-type-array-agg.expected.txt b/cli/src/test/resources/gen/int8-type-array-agg.expected.txt new file mode 100644 index 0000000..e9a1627 --- /dev/null +++ b/cli/src/test/resources/gen/int8-type-array-agg.expected.txt @@ -0,0 +1,43 @@ +package com.foo + +import java.sql.PreparedStatement +import java.sql.ResultSet +import kotlin.Array +import kotlin.Int +import kotlin.Long +import kotlin.String +import kotlin.Unit +import norm.ParamSetter +import norm.Query +import norm.RowMapper + +public class FooParams() + +public class FooParamSetter : ParamSetter { + public override fun map(ps: PreparedStatement, params: FooParams): Unit { + } +} + +public class FooRowMapper : RowMapper { + public override fun map(rs: ResultSet): FooResult = FooResult( + ids = rs.getArray("ids")?.array as kotlin.Array?, + docids = rs.getArray("docids")?.array as kotlin.Array?, + arrTypes = rs.getArray("arr_types")?.array as kotlin.Array?, + strTypes = rs.getObject("str_types") as kotlin.String?) +} + +public class FooQuery : Query { + public override val sql: String = + "SELECT ARRAY_AGG(id) AS ids, ARRAY_AGG(documentId) AS docIds, ARRAY_AGG(type) AS arr_types, STRING_AGG(type, '-') AS str_types FROM requests GROUP BY type" + + public override val mapper: RowMapper = FooRowMapper() + + public override val paramSetter: ParamSetter = FooParamSetter() +} + +public data class FooResult( + public val ids: Array?, + public val docids: Array?, + public val arrTypes: Array?, + public val strTypes: String? +) diff --git a/cli/src/test/resources/init_postgres.sql b/cli/src/test/resources/init_postgres.sql index 1e2e8c3..4895e61 100644 --- a/cli/src/test/resources/init_postgres.sql +++ b/cli/src/test/resources/init_postgres.sql @@ -34,3 +34,12 @@ CREATE TABLE logs( new_value VARCHAR, captured_at TIMESTAMPTZ ); + +CREATE TABLE requests +( + id serial PRIMARY KEY, + documentId int8, + type varchar, + status varchar, + requested_at TIMESTAMPTZ +); diff --git a/codegen/src/main/kotlin/norm/model/ColumnModel.kt b/codegen/src/main/kotlin/norm/model/ColumnModel.kt index c60b3a0..b8fd730 100644 --- a/codegen/src/main/kotlin/norm/model/ColumnModel.kt +++ b/codegen/src/main/kotlin/norm/model/ColumnModel.kt @@ -11,7 +11,9 @@ data class ColumnModel( val isNullable: Boolean ) { fun getTypeName() = - if (colType.startsWith("_")) ARRAY.parameterizedBy(DbToKtTypeMapperFactory.getType(colType, false)) + if (colType.startsWith(ARRAY_TYPE_PREFIX)) ARRAY.parameterizedBy(DbToKtTypeMapperFactory.getType(colType, false)) .copy(nullable = this.isNullable) else DbToKtTypeMapperFactory.getType(colType, isNullable) } + +const val ARRAY_TYPE_PREFIX = "_" diff --git a/codegen/src/main/kotlin/norm/typemapper/DbToKtDefaultTypeMapper.kt b/codegen/src/main/kotlin/norm/typemapper/DbToKtDefaultTypeMapper.kt index 037f396..3955d5c 100644 --- a/codegen/src/main/kotlin/norm/typemapper/DbToKtDefaultTypeMapper.kt +++ b/codegen/src/main/kotlin/norm/typemapper/DbToKtDefaultTypeMapper.kt @@ -1,16 +1,22 @@ package norm.typemapper +import java.math.BigDecimal +import java.util.* + import norm.api.typemapper.DbToKtTypeMapper import org.postgresql.util.PGobject -import java.math.BigDecimal import kotlin.reflect.KClass + class DbToKtDefaultTypeMapper : DbToKtTypeMapper { override fun accepts(type: String): Boolean = true override fun getType(type: String): KClass<*> { - return when (type.toLowerCase()) { + + val dataType = type.removePrefix(ARRAY_TYPE_PREFIX) + + return when (dataType.lowercase(Locale.getDefault())) { "int4" -> Int::class "int" -> Int::class "serial" -> Int::class @@ -34,9 +40,9 @@ class DbToKtDefaultTypeMapper : DbToKtTypeMapper { "varchar" -> String::class "text" -> String::class - "_varchar" -> String::class - "_int4" -> Int::class else -> String::class } } } + +const val ARRAY_TYPE_PREFIX = "_" From a9ba3698c0a932c82d7b2df9432b1a7518432aab Mon Sep 17 00:00:00 2001 From: Dattatray Gund Date: Thu, 7 Apr 2022 19:47:12 +0530 Subject: [PATCH 2/3] Fix: Renamed output file name --- cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt | 2 +- ...-type-array-agg.expected.txt => array-agg-type.expected.txt} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename cli/src/test/resources/gen/{int8-type-array-agg.expected.txt => array-agg-type.expected.txt} (100%) diff --git a/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt b/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt index 345ff52..df5a4a7 100644 --- a/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt +++ b/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt @@ -127,7 +127,7 @@ class CodeGeneratorTest : StringSpec() { "com.foo", "Foo" ) - generatedFileContent shouldBe "/gen/int8-type-array-agg.expected.txt".readAsResource() + generatedFileContent shouldBe "/gen/array-agg-type.expected.txt".readAsResource() } } } diff --git a/cli/src/test/resources/gen/int8-type-array-agg.expected.txt b/cli/src/test/resources/gen/array-agg-type.expected.txt similarity index 100% rename from cli/src/test/resources/gen/int8-type-array-agg.expected.txt rename to cli/src/test/resources/gen/array-agg-type.expected.txt From 7e3efe0fd2b9711f602192229d723befaa0b3634 Mon Sep 17 00:00:00 2001 From: Dattatray Gund Date: Thu, 7 Apr 2022 21:19:29 +0530 Subject: [PATCH 3/3] Fix: Renamed testcase --- cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt b/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt index df5a4a7..bd54536 100644 --- a/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt +++ b/cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt @@ -120,7 +120,7 @@ class CodeGeneratorTest : StringSpec() { generatedFileContent shouldBe "/gen/uuid-column-type-generator.expected.txt".readAsResource() } - "should generate kotlin file with sql ARRAY type mapped to kotlin.Array" { + "should generate kotlin file with sql ARRAY type mapped to kotlin.Array" { val generatedFileContent = codegen( connection, "SELECT ARRAY_AGG(id) AS ids, ARRAY_AGG(documentId) AS docIds, ARRAY_AGG(type) AS arr_types, STRING_AGG(type, '-') AS str_types FROM requests GROUP BY type",