-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sql): Add JSON support for SQL databases
- Loading branch information
Showing
6 changed files
with
199 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
connectors/jdbc/src/main/java/io/camunda/connector/jdbc/model/client/JdbiJsonHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.camunda.connector.jdbc.model.client; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.jdbi.v3.core.qualifier.QualifiedType; | ||
import org.jdbi.v3.core.result.ResultIterable; | ||
import org.jdbi.v3.core.result.UnableToProduceResultException; | ||
import org.jdbi.v3.core.statement.Query; | ||
import org.jdbi.v3.json.Json; | ||
|
||
public class JdbiJsonHelper { | ||
static Map<String, Set<String>> dbJsonTypeMapping = | ||
Map.of( | ||
"Microsoft SQL Server", | ||
Set.of(), | ||
"MySQL", | ||
Set.of("JSON"), | ||
"PostgreSQL", | ||
Set.of("json", "jsonb"), | ||
"MariaDB", | ||
Set.of("JSON")); | ||
|
||
public static ResultIterable<Map<String, Object>> mapToParsedMap( | ||
String databaseProductName, Query query) { | ||
return query.map( | ||
(rs, ctx) -> { | ||
Map<String, Object> row = new HashMap<>(); | ||
// Java SQL ResultSet and ResultSetMetadata columns start with index 1: | ||
// https://docs.oracle.com/en/java/javase/17/docs/api/java.sql/java/sql/ResultSetMetaData.html#getColumnTypeName(int) | ||
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { | ||
String columnName = rs.getMetaData().getColumnLabel(i); | ||
Object value = rs.getObject(i); | ||
if (isJsonColumn(databaseProductName, rs.getMetaData().getColumnTypeName(i))) { | ||
try { | ||
value = | ||
ctx.findColumnMapperFor(QualifiedType.of(JsonNode.class).with(Json.class)) | ||
.orElseThrow() | ||
.map(rs, i, ctx); | ||
} catch (UnableToProduceResultException ignored) { | ||
row.put(columnName, value); | ||
} | ||
} | ||
row.put(columnName, value); | ||
} | ||
return row; | ||
}); | ||
} | ||
|
||
private static boolean isJsonColumn(String databaseProductName, String columnTypeName) { | ||
return dbJsonTypeMapping.getOrDefault(databaseProductName, Set.of()).contains(columnTypeName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters