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() + ")";
}
}
and
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);
}
}
The way we made this work is not ideal however:
- We have to keep our extensions in the same package, because the parent classes contain protected methods.
- We had to duplicate a lot of the code from the parent classes into our extensions to make it work
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.
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:
To support this use case while still being able to use your library, we created the following two classes:
and
The way we made this work is not ideal however:
Perhaps a better way can be created to support both use cases? We saw classes called
SqlFuntionandSqlFunctions. AddingTRIMthere 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.