Skip to content

Commit

Permalink
Merge branch 'master' into lars-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge committed Nov 25, 2024
2 parents 5c70bde + c0b0f23 commit 9e7738c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public static String getDisplayName(

private static String extractJsonValue(
SqlBuilder sqlBuilder, String tablePrefix, String originColumn, String path) {
String jsonExtracted = sqlBuilder.jsonExtract(tablePrefix, originColumn, path);
String json = tablePrefix + "." + originColumn;
String jsonExtracted = sqlBuilder.jsonExtract(json, path);
return sqlBuilder.trim(jsonExtracted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,14 @@ public String coalesce(String expression, String defaultValue) {
}

@Override
public String jsonExtract(String column, String property) {
return String.format("JSONExtractString(%s, '%s')", column, property);
public String jsonExtract(String json, String property) {
return String.format("JSONExtractString(%s, '%s')", json, property);
}

@Override
public String jsonExtract(String tablePrefix, String column, String jsonPath) {
return String.format("JSONExtractString(%s.%s, '%s')", tablePrefix, column, jsonPath);
}

@Override
public String jsonExtractNested(String column, String... jsonPath) {
String path = String.join(".", jsonPath);
return String.format("JSONExtractString(%s, '%s')", column, path);
public String jsonExtractNested(String json, String... expression) {
String path = String.join(".", expression);
return String.format("JSONExtractString(%s, '%s')", json, path);
}

// Statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,13 @@ public String coalesce(String expression, String defaultValue) {
}

@Override
public String jsonExtract(String column, String property) {
return String.format("json_unquote(json_extract(%s, '$.%s'))", column, property);
public String jsonExtract(String json, String property) {
return String.format("json_unquote(json_extract(%s, '$.%s'))", json, property);
}

@Override
public String jsonExtract(String tablePrefix, String column, String jsonPath) {
return String.format(
"json_unquote(json_extract(%s.%s, '$.%s'))", tablePrefix, column, jsonPath);
}

@Override
public String jsonExtractNested(String column, String... jsonPath) {
String path = "$." + String.join(".", jsonPath);
public String jsonExtractNested(String column, String... expression) {
String path = "$." + String.join(".", expression);
return String.format("json_unquote(json_extract(%s, '%s'))", column, path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,12 @@ public String coalesce(String expression, String defaultValue) {

@Override
public String jsonExtract(String column, String property) {
return column + " ->> '" + property + "'";
return String.format("%s ->> '%s'", column, property);
}

@Override
public String jsonExtract(String tablePrefix, String column, String jsonPath) {
return String.format("%s.%s ->> '%s'", tablePrefix, column, jsonPath);
}

@Override
public String jsonExtractNested(String column, String... jsonPath) {
return String.format("%s #>> '{%s}'", column, String.join(", ", jsonPath));
public String jsonExtractNested(String column, String... expression) {
return String.format("%s #>> '{%s}'", column, String.join(", ", expression));
}

// Statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,57 +246,46 @@ public interface SqlBuilder {
/**
* Creates a SQL concatenation function that combines multiple columns or expressions.
*
* @param columns the column names or expressions to concatenate
* @return the SQL function for concatenation
* @param columns the column names or expressions to concatenate.
* @return the SQL function for concatenation.
*/
String concat(String... columns);

/**
* Creates a SQL trim function that removes leading and trailing spaces from an expression.
*
* @param expression the expression to trim
* @return the SQL function for trimming
* @param expression the expression to trim.
* @return the SQL function for trimming.
*/
String trim(String expression);

/**
* Creates a SQL COALESCE function that returns the first non-null expression from the provided
* expressions. If the first expression is null, it returns the default expression.
*
* @param expression the expression to check for null
* @param defaultValue the value to return if the first expression is null
* @return the SQL function for coalescing
* @param expression the expression to check for null.
* @param defaultValue the value to return if the first expression is null.
* @return the SQL function for coalescing.
*/
String coalesce(String expression, String defaultValue);

/**
* Extracts a value from a JSON column using a specified property path.
*
* @param column the JSON column name to extract from
* @param property the JSON property path to extract
* @return the SQL function for JSON value extraction
* @param json the JSON column name or value to extract from.
* @param property the JSON property path to extract.
* @return the SQL function for JSON value extraction.
*/
String jsonExtract(String column, String property);

/**
* Extracts a value from a JSON column using a specified JSON path expression, with support for
* table prefix qualification.
*
* @param tablePrefix the prefix/alias of the table containing the JSON column
* @param column the JSON column name to extract from
* @param jsonPath the JSON path expression to extract the value
* @return the SQL function for JSON value extraction
*/
String jsonExtract(String tablePrefix, String column, String jsonPath);
String jsonExtract(String json, String property);

/**
* Extracts a nested value from a JSON column.
*
* @param column the name of the JSON column from which to extract the value.
* @param jsonPath the hierarchical path to the nested value, represented as a sequence of keys.
* @param json the JSON column name or value to extract from.
* @param expression the hierarchical path expression to the nested value.
* @return a SQL expression to extract the specified nested value from the JSON column.
*/
String jsonExtractNested(String column, String... jsonPath);
String jsonExtractNested(String json, String... expression);

// Statements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ void verifyNonConfidentialTeasAreSkipped() {
when(sqlBuilder.qualifyTable(anyString()))
.thenAnswer(inv -> SqlUtils.quote(inv.getArgument(0)));

when(sqlBuilder.jsonExtract(anyString(), anyString(), anyString())).thenReturn("jsonExtract");
when(sqlBuilder.jsonExtract(anyString(), anyString())).thenReturn("jsonExtract");

when(sqlBuilder.coalesce(anyString(), anyString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ void testRegexpMatch() {
assertEquals("match(id, '[a-z]\\w+\\d{3}')", sqlBuilder.regexpMatch("id", "'[a-z]\\w+\\d{3}'"));
}

@Test
void testJsonExtract() {
assertEquals(
"JSONExtractString(value, 'D7m8vpzxHDJ')", sqlBuilder.jsonExtract("value", "D7m8vpzxHDJ"));
}

@Test
void testJsonExtractNested() {
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ void testRegexpMatch() {
assertEquals("id regexp '[a-z]\\w+\\d{3}'", sqlBuilder.regexpMatch("id", "'[a-z]\\w+\\d{3}'"));
}

@Test
void testJsonExtract() {
assertEquals(
"json_unquote(json_extract(value, '$.D7m8vpzxHDJ'))",
sqlBuilder.jsonExtract("value", "D7m8vpzxHDJ"));
}

@Test
void testJsonExtractNested() {
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ void testRegexpMatch() {
assertEquals("id ~* '[a-z]\\w+\\d{3}'", sqlBuilder.regexpMatch("id", "'[a-z]\\w+\\d{3}'"));
}

@Test
void testJsonExtract() {
assertEquals("value ->> 'D7m8vpzxHDJ'", sqlBuilder.jsonExtract("value", "D7m8vpzxHDJ"));
}

@Test
void testJsonExtractNested() {
assertEquals(
Expand Down

0 comments on commit 9e7738c

Please sign in to comment.