Skip to content

Commit

Permalink
[#13249] Emulate JSON_OBJECT's ABSENT ON NULL clause in MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Sep 30, 2022
1 parent 05508c3 commit 239cfb8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/JSONObjectNullStep.java
Expand Up @@ -84,7 +84,7 @@ public interface JSONObjectNullStep<T> extends JSONObjectReturningStep<T> {
* <p>
* Exclude <code>NULL</code> values in output JSON.
*/
@Support({ H2, POSTGRES, YUGABYTEDB })
@Support({ H2, MYSQL, POSTGRES, YUGABYTEDB })
@NotNull @CheckReturnValue
JSONObjectReturningStep<T> absentOnNull();
}
25 changes: 25 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/JSONObject.java
Expand Up @@ -207,6 +207,31 @@ else if (!entries.isEmpty() && isJSONArray((first = entries.iterator().next()).v
break;
}

case MYSQL: {

// [#13249] ABSENT ON NULL emulation using JSON_TABLE
if (onNull == JSONOnNull.ABSENT_ON_NULL) {
Field<String> k = DSL.field(name("jt", "k"), VARCHAR);
Field<JSON> o = DSL.field(name("j", "o"), JSON);

ctx.visit(DSL.field(
select(DSL.coalesce(
DSL.jsonObjectAgg(k, DSL.function(N_JSON_EXTRACT, JSON, o, DSL.concat(inline("$.\""), k, inline("\"")))),
DSL.jsonObject()))
.from(
select(CustomField.of("o", JSON, c -> acceptStandard(c)).as(o)).asTable("j"),
jsonTable(function(N_JSON_KEYS, JSON, o), inline("$[*]"))
.column("k", VARCHAR).path("$")
.asTable("jt"))
.where(DSL.function(N_JSON_EXTRACT, JSON, o, DSL.concat(inline("$.\""), k, inline("\""))).ne(DSL.inline("null").cast(JSON)))
));
}
else
acceptStandard(ctx);

break;
}

default:
acceptStandard(ctx);
break;
Expand Down
1 change: 1 addition & 0 deletions jOOQ/src/main/java/org/jooq/impl/Names.java
Expand Up @@ -183,6 +183,7 @@ final class Names {
static final Name N_JSON_EXTRACT = systemName("json_extract");
static final Name N_JSON_GROUP_ARRAY = systemName("json_group_array");
static final Name N_JSON_GROUP_OBJECT = systemName("json_group_object");
static final Name N_JSON_KEYS = systemName("json_keys");
static final Name N_JSON_MERGE = systemName("json_merge");
static final Name N_JSON_MERGE_PRESERVE = systemName("json_merge_preserve");
static final Name N_JSON_MODIFY = systemName("json_modify");
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -29,7 +29,7 @@
<!-- JDBC drivers for jOOQ-xyz-extensions modules and vendor-specific API access -->
<postgres.version>42.4.1</postgres.version>
<sqlserver.version>10.2.0.jre11</sqlserver.version>
<oracle.version>21.5.0.0</oracle.version>
<oracle.version>21.7.0.0</oracle.version>

<!-- R2DBC SPI version and some matching driver versions -->
<io.r2dbc.version>0.9.1.RELEASE</io.r2dbc.version>
Expand Down

0 comments on commit 239cfb8

Please sign in to comment.