Giter VIP home page Giter VIP logo

avrilfanomar / flexiblesearchbuilder Goto Github PK

View Code? Open in Web Editor NEW
17.0 17.0 4.0 122 KB

Flexible search query builder is SAP Hybris Commerce extension (released as a library) that provides developer-friendly way to build flexible search queries. The aim of this extension is to write().flexibleSearchQueries().easily() in compile-time safe manner without a need to remember the syntax.

Java 100.00%
builder-pattern commerce-cloud flexible-search hybris hybris-commerce hybris-commerce-suite hybris-extension sap-commerce sap-hybris

flexiblesearchbuilder's People

Contributors

avrilfanomar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

flexiblesearchbuilder's Issues

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

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.

Order by DESC clause works incorrectly for multiple fields

There is a bug in the code when you try to order by multiple fields in a descending fashion. Only the last field is ordered descending, the other fields are ordered ascending.

The following code xx.orderByDesc( field1, field2 ).build(); results in the generation of the following query: ORDER BY { field1 },{ field2 } DESC. This is interpreted as ORDER BY { field1 } ASC ,{ field2 } DESC by the SQL specification because ASC is the default ordering.

One would expect that the generated code would be ORDER BY { field1 } DESC, { field2 } DESC based on the name of the method in the flexible search builder.

Ideally, the ordering would be revised so one can specificy multiple fields, and for every field separatly one can specify either ASC/DESC.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.