We have a use case where we want to compare the trimmed contents of a database field with the trimmed contents of a provided value. In pseudo code:
WHERE TRIM({object:field}) = TRIM(?parameter1)
To support this use case while still being able to use your library, we created the following two classes:
package org.bitbucket.andriichukandrii.hybris.flexiblesearchbuilder;
import org.spockframework.util.Assert;
public class TrimmedField implements Field {
private final Field decorated;
public TrimmedField(Field decorated) {
Assert.notNull(decorated, "The parameter decorated can not be null.");
this.decorated = decorated;
}
@Override
public String getFieldName() {
return decorated.getFieldName();
}
@Override
public String toString() {
return "TRIM(" + decorated.toString() + ")";
}
}
package org.bitbucket.andriichukandrii.hybris.flexiblesearchbuilder;
import java.util.Collection;
import java.util.Map;
public class TrimmedParameterFieldCondition extends AbstractFieldCondition {
private final ParameterConditionType conditionType;
private final Object conditionParameter;
private String parameterCode;
public TrimmedParameterFieldCondition(Field field, ParameterConditionType conditionType, Object conditionParameter) {
super(field);
this.conditionType = conditionType;
this.conditionParameter = conditionParameter;
}
protected void appendQuery(StringBuilder sb) {
super.appendQuery(sb);
boolean collectionParameter = this.conditionParameter instanceof Collection;
sb.append(this.conditionType.getOperator());
if (collectionParameter) {
sb.append("(");
}
sb.append("TRIM(?").append(this.parameterCode).append(")");
if (collectionParameter) {
sb.append(")");
}
}
protected void addParameters(Map<String, Object> parameterMap) {
super.addParameters(parameterMap);
this.parameterCode = FlexibleSearchBuilderFieldUtils.createUniqueParameterCode(parameterMap, this.field.getFieldName());
parameterMap.put(this.parameterCode, this.conditionParameter);
}
}
Perhaps a better way can be created to support both use cases? We saw classes called SqlFuntion
and SqlFunctions
. Adding TRIM
there might be a good first step, but other places in the builder also need to be changed to accept and SqlFunction then. Changing some of the access modifiers to be less restrictive would also help users of the library to add extensions to it.