Skip to content

Commit

Permalink
Fix: Supporting all types array aggregations (#266)
Browse files Browse the repository at this point in the history
* Fix: Supporting all types array aggregations
  • Loading branch information
dattatray-gund authored Apr 8, 2022
1 parent 470918a commit de18245
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
10 changes: 10 additions & 0 deletions cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<kotlin.*>" {
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/array-agg-type.expected.txt".readAsResource()
}
}
}
43 changes: 43 additions & 0 deletions cli/src/test/resources/gen/array-agg-type.expected.txt
Original file line number Diff line number Diff line change
@@ -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<FooParams> {
public override fun map(ps: PreparedStatement, params: FooParams): Unit {
}
}

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
ids = rs.getArray("ids")?.array as kotlin.Array<kotlin.Int>?,
docids = rs.getArray("docids")?.array as kotlin.Array<kotlin.Long>?,
arrTypes = rs.getArray("arr_types")?.array as kotlin.Array<kotlin.String>?,
strTypes = rs.getObject("str_types") as kotlin.String?)
}

public class FooQuery : Query<FooParams, FooResult> {
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<FooResult> = FooRowMapper()

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val ids: Array<Int>?,
public val docids: Array<Long>?,
public val arrTypes: Array<String>?,
public val strTypes: String?
)
9 changes: 9 additions & 0 deletions cli/src/test/resources/init_postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
4 changes: 3 additions & 1 deletion codegen/src/main/kotlin/norm/model/ColumnModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "_"
14 changes: 10 additions & 4 deletions codegen/src/main/kotlin/norm/typemapper/DbToKtDefaultTypeMapper.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = "_"

0 comments on commit de18245

Please sign in to comment.