FQL improvements
This commit is contained in:
@@ -21,7 +21,7 @@ orderByExpression
|
||||
|
||||
// regular expressions - NOT regex
|
||||
regularExpression
|
||||
: (stringExpression | intExpression | booleanExpression | dateExpression) ;
|
||||
: (stringExpression | intExpression | booleanExpression | dateExpression | likeExpression) ;
|
||||
stringExpression
|
||||
: field=IDENTIFIER operator=STRING_OPERATOR value=STRING_VALUE ;
|
||||
intExpression
|
||||
@@ -32,6 +32,8 @@ booleanExpression
|
||||
dateExpression
|
||||
: field=IDENTIFIER operator=STRING_OPERATOR value=DATE_VALUE ;
|
||||
|
||||
likeExpression : field=IDENTIFIER LIKE value=STRING_VALUE ;
|
||||
|
||||
// BETWEEN expressions
|
||||
betweenExpression
|
||||
: (betweenStringExpression | betweenIntExpression ) ;
|
||||
@@ -70,6 +72,8 @@ fragment Y : ('Y' | 'y') ;
|
||||
fragment G : ('G' | 'g') ;
|
||||
fragment O : ('O' | 'o') ;
|
||||
fragment F : ('F' | 'f') ;
|
||||
fragment I : ('I' | 'i') ;
|
||||
fragment K : ('K' | 'k') ;
|
||||
fragment SPACE : ' ' ;
|
||||
|
||||
// Keywords
|
||||
@@ -81,6 +85,7 @@ DESC : D E S C ;
|
||||
ASC : A S C ;
|
||||
TRUE : T R U E ;
|
||||
FALSE : F A L S E ;
|
||||
LIKE : L I K E ;
|
||||
|
||||
// Constant values
|
||||
CURRENT : C U R R E N T ;
|
||||
@@ -96,7 +101,7 @@ L_PAREN : '(' ;
|
||||
R_PAREN : ')' ;
|
||||
IDENTIFIER : [a-zA-Z]+ ;
|
||||
INT_VALUE : [0-9]+ ;
|
||||
STRING_VALUE : '\'' [a-zA-Z0-9\-/ ]+ '\'' ;
|
||||
STRING_VALUE : '\'' [a-zA-Z0-9\-/.&%@$ ]+ '\'' ;
|
||||
DATE_VALUE : [0-9-]+ ;
|
||||
|
||||
NEWLINE : ('\r'? '\n' | '\r')+ ;
|
||||
|
||||
@@ -140,6 +140,16 @@ public class FQLVisitorImpl extends FQLBaseVisitor<Predicate> {
|
||||
.apply(fieldMapping, this.froms, this.criteriaBuilder, ctx.value.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate visitLikeExpression(FQLParser.LikeExpressionContext ctx) {
|
||||
final FieldMapping fieldMapping = FieldMapping.findByFieldName(ctx.field.getText());
|
||||
|
||||
fieldMapping.getJoinHandler().apply(this.froms, fieldMapping);
|
||||
|
||||
return fieldMapping.getFieldHandler()
|
||||
.apply(fieldMapping, this.froms, this.criteriaBuilder, ctx.value.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate visitIntExpression(FQLParser.IntExpressionContext ctx) {
|
||||
final FieldMapping fieldMapping = FieldMapping.findByFieldName(ctx.field.getText());
|
||||
|
||||
@@ -32,7 +32,10 @@ public enum FieldMapping {
|
||||
TAX_RELEVANT("taxRelevant", Transaction_.TAX_RELEVANT, Transaction.class, NoopJoinHandler.class,
|
||||
JoinKey.of(Transaction.class), null, BooleanHandler.class),
|
||||
|
||||
HAS_FILE("hasFile", File_.ID, File.class, FileJoinHandler.class, JoinKey.of(File.class), null, NotNullSyntheticHandler.class);
|
||||
HAS_FILE("hasFile", File_.ID, File.class, FileJoinHandler.class, JoinKey.of(File.class), null, NotNullSyntheticHandler.class),
|
||||
|
||||
DESCRIPTION("description", Transaction_.DESCRIPTION, Transaction.class, NoopJoinHandler.class, JoinKey.of(Transaction.class),
|
||||
null, LikeHandler.class);
|
||||
|
||||
/**
|
||||
* The name of the field as used in FQL
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package de.financer.fql.field_handler;
|
||||
|
||||
import de.financer.fql.FieldMapping;
|
||||
import de.financer.fql.join_handler.JoinKey;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.From;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.Map;
|
||||
|
||||
public class LikeHandler implements FieldHandler<String> {
|
||||
@Override
|
||||
public Predicate apply(FieldMapping fieldMapping, Map<JoinKey, From<?, ?>> froms, CriteriaBuilder criteriaBuilder, String value) {
|
||||
return criteriaBuilder
|
||||
.like(criteriaBuilder.lower(froms.get(fieldMapping.getJoinKey()).get(fieldMapping.getAttributeName())), removeQuotes(value).toLowerCase());
|
||||
}
|
||||
|
||||
private String removeQuotes(String value) {
|
||||
return value.replaceAll("'", "");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user