Skip to content

Commit

Permalink
Specialize Postgres integer JDBC interactions
Browse files Browse the repository at this point in the history
The Postgres JDBC driver and wire protocol treat varying integer sizes differently. For example, an Int only transfers 4 bytes, while Long transfers the full 8. Since we have the ability to call the proper JDBC methods, we should do so.
  • Loading branch information
MariusVolkhart committed Sep 1, 2023
1 parent 6778bc7 commit 32b9f12
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ internal enum class PostgreSqlType(override val javaType: TypeName) : DialectTyp
SMALL_INT(SHORT) {
override fun decode(value: CodeBlock) = CodeBlock.of("%L.toShort()", value)

override fun encode(value: CodeBlock) = CodeBlock.of("%L.toLong()", value)
override fun encode(value: CodeBlock) = CodeBlock.of("%L.toShort()", value)
},
INTEGER(INT) {
override fun decode(value: CodeBlock) = CodeBlock.of("%L.toInt()", value)

override fun encode(value: CodeBlock) = CodeBlock.of("%L.toLong()", value)
override fun encode(value: CodeBlock) = CodeBlock.of("%L.toInt()", value)
},
BIG_INT(LONG),
DATE(ClassName("java.time", "LocalDate")),
Expand All @@ -33,7 +33,9 @@ internal enum class PostgreSqlType(override val javaType: TypeName) : DialectTyp
return CodeBlock.builder()
.add(
when (this) {
SMALL_INT, INTEGER, BIG_INT -> "bindLong"
SMALL_INT -> "bindShort"
INTEGER -> "bindInt"
BIG_INT -> "bindLong"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "bindObject"
NUMERIC -> "bindBigDecimal"
},
Expand All @@ -45,7 +47,9 @@ internal enum class PostgreSqlType(override val javaType: TypeName) : DialectTyp
override fun cursorGetter(columnIndex: Int, cursorName: String): CodeBlock {
return CodeBlock.of(
when (this) {
SMALL_INT, INTEGER, BIG_INT -> "$cursorName.getLong($columnIndex)"
SMALL_INT -> "$cursorName.getShort($columnIndex)"
INTEGER -> "$cursorName.getInt($columnIndex)"
BIG_INT -> "$cursorName.getLong($columnIndex)"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "$cursorName.getObject<%T>($columnIndex)"
NUMERIC -> "$cursorName.getBigDecimal($columnIndex)"
},
Expand Down

0 comments on commit 32b9f12

Please sign in to comment.