-
Notifications
You must be signed in to change notification settings - Fork 495
/
PostgreSqlType.kt
55 lines (49 loc) · 1.93 KB
/
PostgreSqlType.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package app.cash.sqldelight.dialects.postgresql
import app.cash.sqldelight.dialect.api.DialectType
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.LONG
import com.squareup.kotlinpoet.SHORT
import com.squareup.kotlinpoet.TypeName
internal enum class PostgreSqlType(override val javaType: TypeName) : DialectType {
SMALL_INT(SHORT) {
override fun decode(value: CodeBlock) = CodeBlock.of("%L.toShort()", value)
override fun encode(value: CodeBlock) = CodeBlock.of("%L.toLong()", value)
},
INTEGER(INT) {
override fun decode(value: CodeBlock) = CodeBlock.of("%L.toInt()", value)
override fun encode(value: CodeBlock) = CodeBlock.of("%L.toLong()", value)
},
BIG_INT(LONG),
DATE(ClassName("java.time", "LocalDate")),
TIME(ClassName("java.time", "LocalTime")),
TIMESTAMP(ClassName("java.time", "LocalDateTime")),
TIMESTAMP_TIMEZONE(ClassName("java.time", "OffsetDateTime")),
INTERVAL(ClassName("org.postgresql.util", "PGInterval")),
UUID(ClassName("java.util", "UUID")),
NUMERIC(ClassName("java.math", "BigDecimal")),
;
override fun prepareStatementBinder(columnIndex: String, value: CodeBlock): CodeBlock {
return CodeBlock.builder()
.add(
when (this) {
SMALL_INT, INTEGER, BIG_INT -> "bindLong"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "bindObject"
NUMERIC -> "bindBigDecimal"
},
)
.add("($columnIndex, %L)\n", value)
.build()
}
override fun cursorGetter(columnIndex: Int, cursorName: String): CodeBlock {
return CodeBlock.of(
when (this) {
SMALL_INT, INTEGER, BIG_INT -> "$cursorName.getLong($columnIndex)"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "$cursorName.getObject<%T>($columnIndex)"
NUMERIC -> "$cursorName.getBigDecimal($columnIndex)"
},
javaType,
)
}
}