Skip to content

Commit

Permalink
getObject method supports automatic filling of the actual type. (#3401
Browse files Browse the repository at this point in the history
)

* `getObject` method supports automatic filling of the actual type.

* Fixes
  • Loading branch information
robxyy committed Aug 3, 2022
1 parent 7a44385 commit 4072cfd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Expand Up @@ -61,9 +61,10 @@ internal enum class MySqlType(override val javaType: TypeName) : DialectType {
return CodeBlock.of(
when (this) {
TINY_INT, TINY_INT_BOOL, SMALL_INT, INTEGER, BIG_INT, BIT -> "$cursorName.getLong($columnIndex)"
DATE, TIME, DATETIME, TIMESTAMP -> "$cursorName.getObject($columnIndex)"
DATE, TIME, DATETIME, TIMESTAMP -> "$cursorName.getObject<%T>($columnIndex)"
NUMERIC -> "$cursorName.getBigDecimal($columnIndex)"
},
javaType,
)
}
}
Expand Up @@ -46,9 +46,10 @@ internal enum class PostgreSqlType(override val javaType: TypeName) : DialectTyp
return CodeBlock.of(
when (this) {
SMALL_INT, INTEGER, BIG_INT -> "$cursorName.getLong($columnIndex)"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "$cursorName.getObject($columnIndex)"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "$cursorName.getObject<%T>($columnIndex)"
NUMERIC -> "$cursorName.getBigDecimal($columnIndex)"
},
javaType,
)
}
}
Expand Up @@ -592,6 +592,8 @@ class SelectQueryFunctionTest {

val file = FixtureCompiler.parseSql(
"""
|import kotlinx.datetime.Instant;
|
|CREATE TABLE data (
| boolean0 BOOLEAN NOT NULL,
| boolean1 BOOLEAN,
Expand All @@ -616,7 +618,9 @@ class SelectQueryFunctionTest {
| bigint0 BIGINT NOT NULL,
| bigint1 BIGINT,
| bigint2 BIGINT AS kotlin.String NOT NULL,
| bigint3 BIGINT AS kotlin.String
| bigint3 BIGINT AS kotlin.String,
| timestamp0 TIMESTAMP AS Instant NOT NULL,
| timestamp1 TIMESTAMP AS Instant
|);
|
|selectData:
Expand Down Expand Up @@ -657,6 +661,8 @@ class SelectQueryFunctionTest {
| bigint1: kotlin.Long?,
| bigint2: kotlin.String,
| bigint3: kotlin.String?,
| timestamp0: kotlinx.datetime.Instant,
| timestamp1: kotlinx.datetime.Instant?,
|) -> T): app.cash.sqldelight.Query<T> = app.cash.sqldelight.Query(${query.id}, arrayOf("data"), driver, "Test.sq", "selectData", ""${'"'}
||SELECT *
||FROM data
Expand Down Expand Up @@ -686,7 +692,9 @@ class SelectQueryFunctionTest {
| cursor.getLong(20)!!,
| cursor.getLong(21),
| data_Adapter.bigint2Adapter.decode(cursor.getLong(22)!!),
| cursor.getLong(23)?.let { data_Adapter.bigint3Adapter.decode(it) }
| cursor.getLong(23)?.let { data_Adapter.bigint3Adapter.decode(it) },
| data_Adapter.timestamp0Adapter.decode(cursor.getObject<java.time.OffsetDateTime>(24)!!),
| cursor.getObject<java.time.OffsetDateTime>(25)?.let { data_Adapter.timestamp1Adapter.decode(it) }
| )
|}
|
Expand All @@ -699,6 +707,8 @@ class SelectQueryFunctionTest {

val file = FixtureCompiler.parseSql(
"""
|import kotlinx.datetime.Instant;
|
|CREATE TABLE data (
| intArray SMALLINT[],
| smallint0 SMALLINT NOT NULL,
Expand All @@ -713,7 +723,9 @@ class SelectQueryFunctionTest {
| bigint1 BIGINT,
| bigint2 BIGINT AS kotlin.String NOT NULL,
| bigint3 BIGINT AS kotlin.String,
| uuid UUID NOT NULL
| uuid UUID NOT NULL,
| timestamp0 TIMESTAMP AS Instant NOT NULL,
| timestamp1 TIMESTAMP AS Instant
|);
|
|selectData:
Expand Down Expand Up @@ -744,6 +756,8 @@ class SelectQueryFunctionTest {
| bigint2: kotlin.String,
| bigint3: kotlin.String?,
| uuid: java.util.UUID,
| timestamp0: kotlinx.datetime.Instant,
| timestamp1: kotlinx.datetime.Instant?,
|) -> T): app.cash.sqldelight.Query<T> = app.cash.sqldelight.Query(${query.id}, arrayOf("data"), driver, "Test.sq", "selectData", ""${'"'}
||SELECT *
||FROM data
Expand All @@ -763,7 +777,9 @@ class SelectQueryFunctionTest {
| cursor.getLong(10),
| data_Adapter.bigint2Adapter.decode(cursor.getLong(11)!!),
| cursor.getLong(12)?.let { data_Adapter.bigint3Adapter.decode(it) },
| cursor.getObject(13)!!
| cursor.getObject<java.util.UUID>(13)!!,
| data_Adapter.timestamp0Adapter.decode(cursor.getObject<java.time.LocalDateTime>(14)!!),
| cursor.getObject<java.time.LocalDateTime>(15)?.let { data_Adapter.timestamp1Adapter.decode(it) }
| )
|}
|
Expand Down

0 comments on commit 4072cfd

Please sign in to comment.