Skip to content

Commit

Permalink
feat: bind the parameter during the parsing of the query
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Sep 6, 2019
1 parent 0faa74e commit a753d02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import java.time.format.DateTimeFormatter;
import java.util.Date;

class CommonQueryBinder {
final class CommonQueryBinder {

public static final String ISO_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS";
static final String ISO_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS";

private CommonQueryBinder() {
}

static String replace(String query, String oldChars, Object value) {
return query.replace(oldChars, escape(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

class MongoParserVisitor extends HqlParserBaseVisitor<String> {
private Map<String, String> replacementMap;
private Map<String, Object> parameterMaps;

public MongoParserVisitor(Map<String, String> replacementMap) {
public MongoParserVisitor(Map<String, String> replacementMap, Map<String, Object> parameterMaps) {
this.replacementMap = replacementMap;
this.parameterMaps = parameterMaps;
}

@Override
Expand Down Expand Up @@ -84,7 +86,13 @@ public String visitLiteralExpression(HqlParser.LiteralExpressionContext ctx) {
@Override
public String visitParameterExpression(HqlParser.ParameterExpressionContext ctx) {
// this will match parameters used by PanacheQL : '?1' for index based or ':key' for named one.
return ctx.getText();
if (parameterMaps.containsKey(ctx.getText())) {
Object value = parameterMaps.get(ctx.getText());
return CommonQueryBinder.escape(value);
} else {
// we return the parameter to avoid an exception but the query will be invalid
return ctx.getText();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ public static String bindQuery(Class<?> clazz, String query, Object[] params) {
}

//classic query
String bindQuery = prepareQuery(query, replacementMap);
Map<String, Object> parameterMaps = new HashMap<>();
for (int i = 1; i <= params.length; i++) {
String bindParamsKey = "?" + i;
bindQuery = CommonQueryBinder.replace(bindQuery, bindParamsKey, params[i - 1]);
parameterMaps.put(bindParamsKey, params[i - 1]);
}

return bindQuery;
return prepareQuery(query, replacementMap, parameterMaps);
}

public static String bindQuery(Class<?> clazz, String query, Map<String, Object> params) {
Map<String, String> replacementMap = extractReplacementMap(clazz);

String bindQuery = prepareQuery(query, replacementMap);
Map<String, Object> parameterMaps = new HashMap<>();
for (Map.Entry entry : params.entrySet()) {
String bindParamsKey = ":" + entry.getKey();
bindQuery = CommonQueryBinder.replace(bindQuery, bindParamsKey, entry.getValue());
parameterMaps.put(bindParamsKey, entry.getValue());
}

return bindQuery;
return prepareQuery(query, replacementMap, parameterMaps);
}

private static String replaceField(String field, Map<String, String> replacementMap) {
Expand Down Expand Up @@ -72,14 +72,12 @@ private static Map<String, String> extractReplacementMap(Class<?> clazz) {
return replacementMap;
}

private static String prepareQuery(String query, Map<String, String> replacementMap) {
private static String prepareQuery(String query, Map<String, String> replacementMap, Map<String, Object> parameterMaps) {
HqlLexer lexer = new HqlLexer(CharStreams.fromString(query));
CommonTokenStream tokens = new CommonTokenStream(lexer);
HqlParser parser = new HqlParser(tokens);
HqlParser.PredicateContext predicate = parser.predicate();
HqlParserBaseVisitor<String> visitor = new MongoParserVisitor(replacementMap);
// implementaton idea: instead of using a visitor of String we can use a visitor of Document to avoid parsing
// the resulting mongo query
HqlParserBaseVisitor<String> visitor = new MongoParserVisitor(replacementMap, parameterMaps);
return "{" + predicate.accept(visitor) + "}";
}
}

0 comments on commit a753d02

Please sign in to comment.