Skip to content

Commit

Permalink
duckdb queryForJsonString
Browse files Browse the repository at this point in the history
  • Loading branch information
sullis committed Oct 14, 2024
1 parent 7ae004d commit 8e7c0a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/main/java/io/github/sullis/duckdb/playground/Duckdb.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ public org.duckdb.JsonNode queryForJsonNode(CharSequence sql) throws SQLExceptio
return queryForSingleValue(sql, org.duckdb.JsonNode.class);
}

public String queryForJsonString(CharSequence sql) throws SQLException {
var node = queryForJsonNode(sql);
if (!node.isString()) {
throw new SQLException("wrong type of JsonNode. Expected a string");
}
String s = node.toString();
if (s.isEmpty()) {
return s;
} else {
return s.substring(1, s.length() - 1);
}
}

public <T> T queryForSingleValue(CharSequence sql, Class<T> clazz) throws SQLException {
try (DuckDBConnection conn = getConnection()) {
try (Statement statement = conn.createStatement()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,8 @@ public void happyPath() throws Exception {
assertThat(aJsonValue.isString()).isTrue();
assertThat(aJsonValue.toString()).isEqualTo("\"a1-value\"");

var bJsonValue = duckdb.queryForJsonNode("select b.b1 from " + tableName);
assertThat(bJsonValue.isString()).isTrue();
assertThat(bJsonValue.toString()).isEqualTo("\"b1-value\"");

var cJsonValue = duckdb.queryForJsonNode("select c.c1 from " + tableName);
assertThat(cJsonValue.isString()).isTrue();
assertThat(cJsonValue.toString()).isEqualTo("\"c1-value\"");
var aJsonString = duckdb.queryForJsonString("select a.a1 from " + tableName);
assertThat(aJsonString).isEqualTo("a1-value");

List<Map<String, Object>> rowData = duckdb.query("select id from " + tableName);
assertThat(rowData).contains(
Expand Down

0 comments on commit 8e7c0a4

Please sign in to comment.