JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.05k stars 674 forks source link

fix: EXPOSED-393 H2 upsert with JSON column creates invalid data #2104

Closed obabichevjb closed 2 weeks ago

obabichevjb commented 1 month ago

The original issue is that upsert() creates incorrect value in the database for Json column and H2 database.

As I understood the root of the problem is that jdbc driver works differently for insert and merge prepared statements. In the case of insert the json string is saved in the db as it is, in the merge case it is stored with escaping characters...

Some more details on YouTrack/EXPOSED-393

With this PR the new method in IColumnType is introduced: fun parameterMarker(value: T?) = "?"

It allows for the JsonColumnType to specify modifier for the parameter marker inside a prepared statement (so it looks like ? FORMAT JSON).

It's done on the column type, because another db-specific check is made on this level also (like in valueFromDB/valueToDB), and it would also allow to make similar customizations for other columns (CAST in MySql, cast via ::<type> in Postgres, probably named/ordered parameters, but it's more complicated because more information is needed) if it's necessary.