From 0b3f93c1c6110c12e04420ad99d96ecc4be6fb8f Mon Sep 17 00:00:00 2001 From: zbeacon Date: Fri, 24 Jul 2020 10:45:19 +0300 Subject: [PATCH] Fix for String filter on empty string received. --- .../dao/sql/query/EntityKeyMapping.java | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java b/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java index dc2aa3dafc..c6ab10d52f 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java @@ -460,32 +460,59 @@ public class EntityKeyMapping { } switch (stringFilterPredicate.getOperation()) { case EQUAL: - stringOperationQuery = String.format("%s = :%s", operationField, paramName); + stringOperationQuery = String.format("%s = :%s) or (%s is null and :%s = '')", operationField, paramName, operationField, paramName); break; case NOT_EQUAL: - stringOperationQuery = String.format("%s != :%s", operationField, paramName); + stringOperationQuery = String.format("%s != :%s) or (%s is null and :%s != '')", operationField, paramName, operationField, paramName); break; case STARTS_WITH: value += "%"; - stringOperationQuery = String.format("%s like :%s", operationField, paramName); + stringOperationQuery = String.format("%s like :%s) or (%s is null and :%s = '%%')", operationField, paramName, operationField, paramName); break; case ENDS_WITH: value = "%" + value; - stringOperationQuery = String.format("%s like :%s", operationField, paramName); + stringOperationQuery = String.format("%s like :%s) or (%s is null and :%s = '%%')", operationField, paramName, operationField, paramName); break; case CONTAINS: value = "%" + value + "%"; - stringOperationQuery = String.format("%s like :%s", operationField, paramName); + stringOperationQuery = String.format("%s like :%s) or (%s is null and :%s = '%%%%')", operationField, paramName, operationField, paramName); break; case NOT_CONTAINS: value = "%" + value + "%"; - stringOperationQuery = String.format("%s not like :%s", operationField, paramName); + stringOperationQuery = String.format("%s not like :%s) or (%s is null and :%s != '%%%%')", operationField, paramName, operationField, paramName); break; } ctx.addStringParameter(paramName, value); - return String.format("(%s is not null and %s)", field, stringOperationQuery); + return String.format("((%s is not null and %s)", field, stringOperationQuery); } + +// case EQUAL: +// stringOperationQuery = String.format("%s = :%s) or (%s is null and :%s = '')", operationField, paramName, operationField, paramName); +// break; +// case NOT_EQUAL: +// stringOperationQuery = String.format("%s != :%s) or (%s is null and :%s != '')", operationField, paramName, operationField, paramName); +// break; +// case STARTS_WITH: +// value += "%"; +// stringOperationQuery = String.format("%s like :%s) or (%s is null and :%s = '')", operationField, paramName, operationField, paramName); +// break; +// case ENDS_WITH: +// value = "%" + value; +// stringOperationQuery = String.format("%s like :%s) or (%s is null and :%s = '')", operationField, paramName, operationField, paramName); +// break; +// case CONTAINS: +// value = "%" + value + "%"; +// stringOperationQuery = String.format("%s like :%s) or (%s is null and :%s = '')", operationField, paramName, operationField, paramName); +// break; +// case NOT_CONTAINS: +// value = "%" + value + "%"; +// stringOperationQuery = String.format("%s not like :%s) or (%s is null and :%s != '')", operationField, paramName, operationField, paramName); +// break; +//} +// ctx.addStringParameter(paramName, value); +// return String.format("((%s is not null and %s)", field, stringOperationQuery); + private String buildNumericPredicateQuery(QueryContext ctx, String field, NumericFilterPredicate numericFilterPredicate) { String paramName = getNextParameterName(field); ctx.addDoubleParameter(paramName, numericFilterPredicate.getValue().getValue());