Skip to content

Commit

Permalink
Fixes 5028 PostgreSql json (#5030)
Browse files Browse the repository at this point in the history
* Add new method bindObjectOther

With JDBC, setObject...Types.OTHER can be used to bind a String to a JSON type

Keep the jsonDataType as a String for reading and writing

This is also useful for other types like TSVector

* Add JSON enum type

JSON type needs special object binding
to write as a String.
Read as a String as normal

* Add JsonQueries integration tests

Previously there were no jsonQueries integration tests even though source existed
  • Loading branch information
griffio authored Mar 19, 2024
1 parent 3602833 commit be82f84
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.LONG
import com.squareup.kotlinpoet.SHORT
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.TypeName

internal enum class PostgreSqlType(override val javaType: TypeName) : DialectType {
Expand All @@ -19,6 +20,7 @@ internal enum class PostgreSqlType(override val javaType: TypeName) : DialectTyp
INTERVAL(ClassName("org.postgresql.util", "PGInterval")),
UUID(ClassName("java.util", "UUID")),
NUMERIC(ClassName("java.math", "BigDecimal")),
JSON(STRING),
;

override fun prepareStatementBinder(columnIndex: CodeBlock, value: CodeBlock): CodeBlock {
Expand All @@ -30,6 +32,7 @@ internal enum class PostgreSqlType(override val javaType: TypeName) : DialectTyp
BIG_INT -> "bindLong"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "bindObject"
NUMERIC -> "bindBigDecimal"
JSON -> "bindObjectOther"
},
)
.add("(%L, %L)\n", columnIndex, value)
Expand All @@ -44,6 +47,7 @@ internal enum class PostgreSqlType(override val javaType: TypeName) : DialectTyp
BIG_INT -> "$cursorName.getLong($columnIndex)"
DATE, TIME, TIMESTAMP, TIMESTAMP_TIMEZONE, INTERVAL, UUID -> "$cursorName.getObject<%T>($columnIndex)"
NUMERIC -> "$cursorName.getBigDecimal($columnIndex)"
JSON -> "$cursorName.getString($columnIndex)"
},
javaType,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
else -> throw IllegalArgumentException("Unknown date type ${dateDataType!!.text}")
}
}
jsonDataType != null -> TEXT
jsonDataType != null -> PostgreSqlType.JSON
booleanDataType != null -> BOOLEAN
blobDataType != null -> BLOB
else -> throw IllegalArgumentException("Unknown kotlin type for sql type ${this.text}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ class JdbcPreparedStatement(
}
}

fun bindObjectOther(index: Int, obj: Any?) {
if (obj == null) {
preparedStatement.setNull(index + 1, Types.OTHER)
} else {
preparedStatement.setObject(index + 1, obj, Types.OTHER)
}
}

override fun bindString(index: Int, string: String?) {
if (string == null) {
preparedStatement.setNull(index + 1, Types.VARCHAR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ INSERT INTO myJson(data, datab) VALUES(
);

buildJson:
SELECT json_build_object('key', 'value'), jsonb_build_object('key', 'value');
SELECT json_build_object('key', 'value'), jsonb_build_object('key', 'value');

insertLiteral:
INSERT INTO myJson(data, datab) VALUES (?, ?);

select:
SELECT *
FROM myJson;
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,22 @@ class PostgreSqlTest {
val result = database.binaryArgumentsQueries.selectDataBinaryIntervalComparison2(created).executeAsList()
assertThat(result.first().datum).isEqualTo(10)
}

@Test
fun testInsertJson() {
database.jsonQueries.insert("another key", "another value")
with(database.jsonQueries.select().executeAsList()) {
assertThat(first().data_).isEqualTo("""{"another key" : "another value"}""")
assertThat(first().datab).isEqualTo("""{"key": "value"}""")
}
}

@Test
fun testInsertJsonLiteral() {
database.jsonQueries.insertLiteral("""{"key a" : "value a"}""", """{"key b" : "value b"}""")
with(database.jsonQueries.select().executeAsList()) {
assertThat(first().data_).isEqualTo("""{"key a" : "value a"}""")
assertThat(first().datab).isEqualTo("""{"key b": "value b"}""")
}
}
}

0 comments on commit be82f84

Please sign in to comment.