Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support filter expressions for additional numberic types #4435

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ public String getLeftOperand(List<PathItem> 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 checkStatementByType(type, statement);
}

@Override
Expand All @@ -71,4 +63,35 @@ public WhereClause toWhereClause(List<PathItem> path, Criterion criterion, SqlOp
);
}

private String checkStatementByType(Class<?> type, String statement) {
paullatzelsperger marked this conversation as resolved.
Show resolved Hide resolved
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(Byte.class)) {
return format("(%s)::byte", statement);
}

if (type.equals(Short.class)) {
return format("(%s)::short", statement);
}

return statement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,61 @@ 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_whenRightOperandIsByte() {
var operator = new SqlOperator("=", Object.class);
var criterion = criterion("json.field", "=", (byte) 1);

var result = translator.toWhereClause(PathItem.parse("field"), criterion, operator);

assertThat(result.sql()).isEqualTo("(column_name ->> 'field')::byte = ?");
assertThat(result.parameters()).containsExactly((byte) 1);
}

@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);
Expand Down