diff --git a/README.md b/README.md index 380c7b4e..e5481843 100644 --- a/README.md +++ b/README.md @@ -36,19 +36,21 @@ Instant REST API over your existing or new database in minutes. #### Supported Operators -| Operator | Postgresql | Description | Supported | -|----------|----------------|-----------------------|-----------| -| == | = | equals | [X] | -| > | > | greater than | [X] | -| =gt= | > | greater than | [X] | -| >= | >= | greater than or equal | [X] | -| =gt= | > | greater than or equal | [X] | -| < | < | less than | [X] | -| =lt= | < | less than | [X] | -| <= | <= | less than or equal | [X] | -| =le= | <= | less than or equal | [X] | -| =in= | <= | in | [X] | -| =out= | <= | not in | [X] | +| Operator | Postgresql | Description | Supported | +|----------|------------|-----------------------|-----------| +| == | = | equals | [X] | +| > | > | greater than | [X] | +| =gt= | > | greater than | [X] | +| >= | >= | greater than or equal | [X] | +| =gt= | > | greater than or equal | [X] | +| < | < | less than | [X] | +| =lt= | < | less than | [X] | +| <= | <= | less than or equal | [X] | +| =le= | <= | less than or equal | [X] | +| =in= | <= | in | [X] | +| =out= | <= | not in | [X] | +| =like= | like | like | [X] | + ### MySQL (Planned) diff --git a/src/main/java/com/homihq/db2rest/rsql/FilterBuilderService.java b/src/main/java/com/homihq/db2rest/rsql/FilterBuilderService.java index 416a346e..ac5c045d 100644 --- a/src/main/java/com/homihq/db2rest/rsql/FilterBuilderService.java +++ b/src/main/java/com/homihq/db2rest/rsql/FilterBuilderService.java @@ -1,6 +1,7 @@ package com.homihq.db2rest.rsql; import com.homihq.db2rest.rsql.operators.CustomRSQLOperators; +import com.homihq.db2rest.rsql.operators.handler.jooq.JooqCustomRSQLOperators; import com.homihq.db2rest.rsql.parser.JooqRSQLVisitor; import com.homihq.db2rest.schema.SchemaService; import cz.jirutka.rsql.parser.RSQLParser; @@ -32,7 +33,7 @@ public String getWhereClause(String tableName, String rSql) { public void getWhereClause(SelectJoinStep selectFromStep, String tableName, String rSql) { Table table = schemaService.getTableByName(tableName).orElseThrow(); - Node rootNode = new RSQLParser().parse(rSql); + Node rootNode = new RSQLParser(JooqCustomRSQLOperators.customOperators()).parse(rSql); log.info("root node - {}", rootNode); diff --git a/src/main/java/com/homihq/db2rest/rsql/operators/handler/LikeOperatorHandler.java b/src/main/java/com/homihq/db2rest/rsql/operators/handler/LikeOperatorHandler.java deleted file mode 100644 index a1da3555..00000000 --- a/src/main/java/com/homihq/db2rest/rsql/operators/handler/LikeOperatorHandler.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.homihq.db2rest.rsql.operators.handler; - -public class LikeOperatorHandler implements OperatorHandler { - - private static final String OPERATOR = " like "; - - @Override - public String handle(String columnName, String value, Class type) { - return columnName + OPERATOR + "'%" + value + "%'"; - } - -} diff --git a/src/main/java/com/homihq/db2rest/rsql/operators/handler/RSQLOperatorHandlers.java b/src/main/java/com/homihq/db2rest/rsql/operators/handler/RSQLOperatorHandlers.java index 37dfee12..76ef4e8c 100644 --- a/src/main/java/com/homihq/db2rest/rsql/operators/handler/RSQLOperatorHandlers.java +++ b/src/main/java/com/homihq/db2rest/rsql/operators/handler/RSQLOperatorHandlers.java @@ -15,7 +15,7 @@ public class RSQLOperatorHandlers { static { - OPERATOR_HANDLER_MAP.put(LIKE.getSymbol(), new LikeOperatorHandler()); + OPERATOR_HANDLER_MAP.put(START_WITH.getSymbol(), new StartWithOperatorHandler()); OPERATOR_HANDLER_MAP.put(END_WITH.getSymbol(), new EndWithOperatorHandler()); diff --git a/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqCustomRSQLOperators.java b/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqCustomRSQLOperators.java new file mode 100644 index 00000000..a0be71fe --- /dev/null +++ b/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqCustomRSQLOperators.java @@ -0,0 +1,21 @@ +package com.homihq.db2rest.rsql.operators.handler.jooq; + +import cz.jirutka.rsql.parser.ast.ComparisonOperator; +import cz.jirutka.rsql.parser.ast.RSQLOperators; + +import java.util.Arrays; +import java.util.Set; + +public class JooqCustomRSQLOperators extends RSQLOperators { + + public static final ComparisonOperator LIKE = new ComparisonOperator("=like=", false); + public static final ComparisonOperator START_WITH = new ComparisonOperator("=startWith=", false); + public static final ComparisonOperator END_WITH = new ComparisonOperator("=endWith=", false); + + public static Set customOperators() { + Set comparisonOperators = defaultOperators(); + comparisonOperators.addAll(Arrays.asList(LIKE, START_WITH, END_WITH)); + return comparisonOperators; + } + +} diff --git a/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqLikeOperatorHandler.java b/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqLikeOperatorHandler.java new file mode 100644 index 00000000..a84118ae --- /dev/null +++ b/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqLikeOperatorHandler.java @@ -0,0 +1,21 @@ +package com.homihq.db2rest.rsql.operators.handler.jooq; + +import com.homihq.db2rest.rsql.operators.handler.OperatorHandler; +import org.jooq.Condition; + +import static org.jooq.impl.DSL.field; +import static org.jooq.impl.DSL.val; + +public class JooqLikeOperatorHandler implements JooqOperatorHandler { + + private static final String OPERATOR = " like "; + + @Override + public Condition handle(String columnName, String value, Class type) { + //return columnName + OPERATOR + "'%" + value + "%'"; + return + field(columnName) + .like(value); + } + +} diff --git a/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqRSQLOperatorHandlers.java b/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqRSQLOperatorHandlers.java index 9b9c558e..44729493 100644 --- a/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqRSQLOperatorHandlers.java +++ b/src/main/java/com/homihq/db2rest/rsql/operators/handler/jooq/JooqRSQLOperatorHandlers.java @@ -18,6 +18,7 @@ public class JooqRSQLOperatorHandlers { OPERATOR_HANDLER_MAP.put(LESS_THAN.getSymbol(), new JooqLessThanOperatorHandler()); OPERATOR_HANDLER_MAP.put(LESS_THAN_OR_EQUAL.getSymbol(), new JooqLessThanEqualToOperatorHandler()); OPERATOR_HANDLER_MAP.put(NOT_EQUAL.getSymbol(), new JooqNotEqualToOperatorHandler()); + OPERATOR_HANDLER_MAP.put(LIKE.getSymbol(), new JooqLikeOperatorHandler()); } public static JooqOperatorHandler getOperatorHandler(String symbol) {