Skip to content

Commit

Permalink
Added uuid to java.util.UUID Type Mapper (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitulrana committed Jun 28, 2021
1 parent dca9775 commit 3fce781
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cli/src/test/kotlin/norm/codegen/CodeGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,11 @@ class CodeGeneratorTest : StringSpec() {
println(generatedFileContent)
generatedFileContent shouldBe "/gen/left-joined-nullable-check.expected.txt".readAsResource()
}

"should generate kotlin file with sql type uuid mapped to UUID." {
val generatedFileContent = codegen(connection, "SELECT * FROM logs", "com.foo", "Foo")

generatedFileContent shouldBe "/gen/uuid-column-type-generator.expected.txt".readAsResource()
}
}
}
43 changes: 43 additions & 0 deletions cli/src/test/resources/gen/uuid-column-type-generator.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 java.time.OffsetDateTime
import java.util.UUID
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 data class FooResult(
public val id: UUID,
public val columnName: String?,
public val oldValue: String?,
public val newValue: String?,
public val capturedAt: OffsetDateTime?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as java.util.UUID,
columnName = rs.getObject("column_name") as kotlin.String?,
oldValue = rs.getObject("old_value") as kotlin.String?,
newValue = rs.getObject("new_value") as kotlin.String?,
capturedAt = rs.getObject("captured_at") as java.time.OffsetDateTime?)
}

public class FooQuery : Query<FooParams, FooResult> {
public override val sql: String = "SELECT * FROM logs"

public override val mapper: RowMapper<FooResult> = FooRowMapper()

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}
10 changes: 9 additions & 1 deletion cli/src/test/resources/init_postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ CREATE TABLE time_travel_log(
from_time timestamptz,
to_time timestamptz,
duration time
)
);

CREATE TABLE logs(
id uuid PRIMARY KEY,
column_name VARCHAR,
old_value VARCHAR,
new_value VARCHAR,
captured_at TIMESTAMPTZ
);
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class DbToKtDefaultTypeMapper : DbToKtTypeMapper {

"jsonb" -> PGobject::class

"uuid" -> java.util.UUID::class

"varchar" -> String::class
"text" -> String::class
"_varchar" -> String::class
Expand Down

0 comments on commit 3fce781

Please sign in to comment.