diff --git a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java index b250fc8b750..ae1ec6803ed 100644 --- a/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java +++ b/extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/translation/JsonFieldTranslator.java @@ -44,15 +44,7 @@ public String getLeftOperand(List path, Class type) { statementBuilder.append(" ->> '%s'".formatted(path.get(length - 1))); var statement = statementBuilder.toString(); - if (type.equals(Boolean.class)) { - return format("(%s)::boolean", statement); - } - - if (type.equals(Integer.class)) { - return format("(%s)::integer", statement); - } - - return statement; + return createStatementForType(type, statement); } @Override @@ -71,4 +63,31 @@ public WhereClause toWhereClause(List path, Criterion criterion, SqlOp ); } + private String createStatementForType(Class type, String statement) { + if (type.equals(Boolean.class)) { + return format("(%s)::boolean", statement); + } + + if (type.equals(Integer.class)) { + return format("(%s)::integer", statement); + } + + if (type.equals(Double.class)) { + return format("(%s)::double", statement); + } + + if (type.equals(Float.class)) { + return format("(%s)::float", statement); + } + + if (type.equals(Long.class)) { + return format("(%s)::long", statement); + } + + if (type.equals(Short.class)) { + return format("(%s)::short", statement); + } + + return statement; + } } diff --git a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java index a22750f078e..5f39d4a5bf2 100644 --- a/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java +++ b/extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/translation/JsonFieldTranslatorTest.java @@ -97,6 +97,50 @@ void shouldParseWhereClause_whenRightOperandIsInteger() { assertThat(result.parameters()).containsExactly(100); } + @Test + void shouldParseWhereClause_whenRightOperandIsDouble() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", 100.0); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::double = ?"); + assertThat(result.parameters()).containsExactly(100.0); + } + + @Test + void shouldParseWhereClause_whenRightOperandIsFloat() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", 100.0F); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::float = ?"); + assertThat(result.parameters()).containsExactly(100.0F); + } + + @Test + void shouldParseWhereClause_whenRightOperandIsLong() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", 100L); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::long = ?"); + assertThat(result.parameters()).containsExactly(100L); + } + + @Test + void shouldParseWhereClause_whenRightOperandIsShort() { + var operator = new SqlOperator("=", Object.class); + var criterion = criterion("json.field", "=", (short) 1); + + var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator); + + assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::short = ?"); + assertThat(result.parameters()).containsExactly((short) 1); + } + @Test void shouldConvertToJsonB_whenOperatorIsContains() { var operator = new SqlOperator("??", Object.class);