Skip to content

Commit

Permalink
Fix string cast for QueryDSL predicates (fix #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Mar 30, 2016
1 parent c6250ab commit ed6d755
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.mysema.query.BooleanBuilder;
import com.mysema.query.support.Expressions;
import com.mysema.query.types.Ops;
import com.mysema.query.types.expr.StringExpression;
import com.mysema.query.types.path.PathBuilder;

public class DataTablesUtils {
Expand Down Expand Up @@ -124,18 +125,16 @@ public static com.mysema.query.types.Predicate getPredicate(PathBuilder<?> entit
}
predicate = predicate.and(entity.getBoolean(column.getData()).in(booleanValues));
} else {
predicate.and(
Expressions.stringOperation(Ops.STRING_CAST, entity.get(column.getData())).in(values));
predicate.and(getStringExpression(entity, column.getData()).in(values));
}
} else {
// the filter contains only one value, add a 'WHERE .. LIKE'
// clause
if (isBoolean(filterValue)) {
predicate = predicate.and(entity.getBoolean(column.getData()).eq(Boolean.valueOf(filterValue)));
} else {
predicate = predicate.and(
Expressions.stringOperation(Ops.STRING_CAST, entity.get(column.getData().toLowerCase()))
.like(getLikeFilterValue(filterValue), ESCAPE_CHAR));
predicate = predicate.and(getStringExpression(entity, column.getData()).lower()
.like(getLikeFilterValue(filterValue), ESCAPE_CHAR));
}
}
}
Expand All @@ -148,9 +147,8 @@ public static com.mysema.query.types.Predicate getPredicate(PathBuilder<?> entit
// add a 'WHERE .. LIKE' clause on each searchable column
for (ColumnParameter column : input.getColumns()) {
if (column.getSearchable()) {
matchOneColumnPredicate = matchOneColumnPredicate
.or(Expressions.stringOperation(Ops.STRING_CAST, entity.get(column.getData().toLowerCase()))
.like(getLikeFilterValue(globalFilterValue), ESCAPE_CHAR));
matchOneColumnPredicate = matchOneColumnPredicate.or(getStringExpression(entity, column.getData())
.lower().like(getLikeFilterValue(globalFilterValue), ESCAPE_CHAR));
}
}
predicate = predicate.and(matchOneColumnPredicate);
Expand Down Expand Up @@ -203,4 +201,9 @@ private static Expression<String> getExpression(Root<?> root, String columnData)
private static String getLikeFilterValue(String filterValue) {
return "%" + filterValue.toLowerCase().replaceAll("%", "\\\\" + "%").replaceAll("_", "\\\\" + "_") + "%";
}

private static StringExpression getStringExpression(PathBuilder<?> entity, String columnData) {
return Expressions.stringOperation(Ops.STRING_CAST, entity.get(columnData));
}

}

0 comments on commit ed6d755

Please sign in to comment.