Skip to content

Support the operator 'TRIM()' on both database fields and query parameters #3

Description

@Seblink

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:

  1. We have to keep our extensions in the same package, because the parent classes contain protected methods.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions