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

Updated MongoDB/Panache parser to latest ORM parser #38751

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,6 +3,13 @@
import java.util.Map;

import io.quarkus.panacheql.internal.HqlParser;
import io.quarkus.panacheql.internal.HqlParser.ComparisonPredicateContext;
import io.quarkus.panacheql.internal.HqlParser.GroupedExpressionContext;
import io.quarkus.panacheql.internal.HqlParser.GroupedPredicateContext;
import io.quarkus.panacheql.internal.HqlParser.NamedParameterContext;
import io.quarkus.panacheql.internal.HqlParser.ParameterContext;
import io.quarkus.panacheql.internal.HqlParser.PositionalParameterContext;
import io.quarkus.panacheql.internal.HqlParser.StandardFunctionContext;
import io.quarkus.panacheql.internal.HqlParserBaseVisitor;

class MongoParserVisitor extends HqlParserBaseVisitor<String> {
Expand Down Expand Up @@ -38,18 +45,28 @@ public String visitOrPredicate(HqlParser.OrPredicateContext ctx) {
}

@Override
public String visitEqualityPredicate(HqlParser.EqualityPredicateContext ctx) {
return ctx.expression(0).accept(this) + ":" + ctx.expression(1).accept(this);
}

@Override
public String visitInequalityPredicate(HqlParser.InequalityPredicateContext ctx) {
return ctx.expression(0).accept(this) + ":{'$ne':" + ctx.expression(1).accept(this) + "}";
}

@Override
public String visitLessThanOrEqualPredicate(HqlParser.LessThanOrEqualPredicateContext ctx) {
return ctx.expression(0).accept(this) + ":{'$lte':" + ctx.expression(1).accept(this) + "}";
public String visitComparisonPredicate(ComparisonPredicateContext ctx) {
String lhs = ctx.expression(0).accept(this);
String rhs = ctx.expression(1).accept(this);
if (ctx.comparisonOperator().EQUAL() != null) {
return lhs + ":" + rhs;
}
if (ctx.comparisonOperator().NOT_EQUAL() != null) {
return lhs + ":{'$ne':" + rhs + "}";
}
if (ctx.comparisonOperator().GREATER() != null) {
return lhs + ":{'$gt':" + rhs + "}";
}
if (ctx.comparisonOperator().GREATER_EQUAL() != null) {
return lhs + ":{'$gte':" + rhs + "}";
}
if (ctx.comparisonOperator().LESS() != null) {
return lhs + ":{'$lt':" + rhs + "}";
}
if (ctx.comparisonOperator().LESS_EQUAL() != null) {
return lhs + ":{'$lte':" + rhs + "}";
}
return super.visitComparisonPredicate(ctx);
}

@Override
Expand All @@ -64,33 +81,47 @@ public String visitLikePredicate(HqlParser.LikePredicateContext ctx) {
}

@Override
public String visitGreaterThanPredicate(HqlParser.GreaterThanPredicateContext ctx) {
return ctx.expression(0).accept(this) + ":{'$gt':" + ctx.expression(1).accept(this) + "}";
public String visitIsNullPredicate(HqlParser.IsNullPredicateContext ctx) {
boolean exists = ctx.NOT() != null;
return ctx.expression().accept(this) + ":{'$exists':" + exists + "}";
}

@Override
public String visitLiteralExpression(HqlParser.LiteralExpressionContext ctx) {
String text = ctx.getText();
// FIXME: this only really supports text literals
if (ctx.literal().STRING_LITERAL() != null) {
text = text.substring(1, text.length() - 1);
}
return CommonQueryBinder.escape(text);
}

@Override
public String visitLessThanPredicate(HqlParser.LessThanPredicateContext ctx) {
return ctx.expression(0).accept(this) + ":{'$lt':" + ctx.expression(1).accept(this) + "}";
public String visitNamedParameter(NamedParameterContext ctx) {
return visitParameter(ctx);
}

@Override
public String visitGreaterThanOrEqualPredicate(HqlParser.GreaterThanOrEqualPredicateContext ctx) {
return ctx.expression(0).accept(this) + ":{'$gte':" + ctx.expression(1).accept(this) + "}";
public String visitPositionalParameter(PositionalParameterContext ctx) {
return visitParameter(ctx);
}

@Override
public String visitIsNullPredicate(HqlParser.IsNullPredicateContext ctx) {
boolean exists = ctx.NOT() != null;
return ctx.expression().accept(this) + ":{'$exists':" + exists + "}";
public String visitParameterExpression(HqlParser.ParameterExpressionContext ctx) {
return visitParameter(ctx.parameter());
}

@Override
public String visitLiteralExpression(HqlParser.LiteralExpressionContext ctx) {
return CommonQueryBinder.escape(ctx.getText());
public String visitGroupedExpression(GroupedExpressionContext ctx) {
return ctx.expression().accept(this);
}

@Override
public String visitParameterExpression(HqlParser.ParameterExpressionContext ctx) {
public String visitGroupedPredicate(GroupedPredicateContext ctx) {
return ctx.predicate().accept(this);
}

private String visitParameter(ParameterContext ctx) {
// this will match parameters used by PanacheQL : '?1' for index based or ':key' for named one.
if (parameterMaps.containsKey(ctx.getText())) {
Object value = parameterMaps.get(ctx.getText());
Expand All @@ -102,9 +133,20 @@ public String visitParameterExpression(HqlParser.ParameterExpressionContext ctx)
}

@Override
public String visitPathExpression(HqlParser.PathExpressionContext ctx) {
public String visitGeneralPathExpression(HqlParser.GeneralPathExpressionContext ctx) {
String identifier = unquote(ctx.getText());
// this is the name of the field, we apply replacement and escape with '
return "'" + replacementMap.getOrDefault(ctx.getText(), ctx.getText()) + "'";
return "'" + replacementMap.getOrDefault(identifier, identifier) + "'";
}

/**
* Removes backticks for quoted identifiers
*/
private String unquote(String text) {
if (text.startsWith("`") && text.endsWith("`") && text.length() >= 2) {
return text.substring(1, text.length() - 1);
}
return text;
}

@Override
Expand All @@ -115,4 +157,10 @@ public String visitInPredicate(HqlParser.InPredicateContext ctx) {
.append("]}");
return sb.toString();
}

// Turn new date functions such as instant into regular fields, to not break existing queries
@Override
public String visitStandardFunction(StandardFunctionContext ctx) {
return "'" + ctx.getText() + "'";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public void testBindShorthandFilter() {
//test field replacement
query = operations.bindFilter(DemoObj.class, "property", new Object[] { "a value" });
assertEquals("{'value':'a value'}", query);

// keywords (quoted)
query = operations.bindFilter(Object.class, "`instant` = ?1", new Object[] { "a value" });
assertEquals("{'instant':'a value'}", query);

// keywords (unquoted)
query = operations.bindFilter(Object.class, "instant = ?1", new Object[] { "a value" });
assertEquals("{'instant':'a value'}", query);
}

private Object toDate(LocalDateTime of) {
Expand Down Expand Up @@ -285,12 +293,12 @@ public void testBindEnhancedFilterByIndex() {
assertEquals("{'field':{'$in':['f1', 'f2']},'isOk':true}", query);

query = operations.bindFilter(DemoObj.class,
"field in ?1 and property = ?2 or property = ?3",
"field in ?1 and (property = ?2 or property = ?3)",
new Object[] { list, "jpg", "gif" });
assertEquals("{'field':{'$in':['f1', 'f2']},'$or':[{'value':'jpg'},{'value':'gif'}]}", query);

query = operations.bindFilter(DemoObj.class,
"field in ?1 and isOk = ?2 and property = ?3 or property = ?4",
"field in ?1 and isOk = ?2 and (property = ?3 or property = ?4)",
new Object[] { list, true, "jpg", "gif" });
assertEquals("{'field':{'$in':['f1', 'f2']},'isOk':true,'$or':[{'value':'jpg'},{'value':'gif'}]}", query);
}
Expand Down Expand Up @@ -361,12 +369,12 @@ public void testBindEnhancedFilterByName() {
assertEquals("{'field':{'$in':['f1', 'f2']},'isOk':true}", query);

query = operations.bindFilter(DemoObj.class,
"field in :fields and property = :p1 or property = :p2",
"field in :fields and (property = :p1 or property = :p2)",
Parameters.with("fields", list).and("p1", "jpg").and("p2", "gif").map());
assertEquals("{'field':{'$in':['f1', 'f2']},'$or':[{'value':'jpg'},{'value':'gif'}]}", query);

query = operations.bindFilter(DemoObj.class,
"field in :fields and isOk = :isOk and property = :p1 or property = :p2",
"field in :fields and isOk = :isOk and (property = :p1 or property = :p2)",
Parameters.with("fields", list)
.and("isOk", true)
.and("p1", "jpg")
Expand Down
Loading
Loading