Search in sources :

Example 1 with SQLCondition

use of org.pentaho.di.core.sql.SQLCondition in project pdi-dataservice-server-plugin by pentaho.

the class ParameterPushdown method captureParameterValues.

@VisibleForTesting
protected Map<String, String> captureParameterValues(SQL sql) {
    Optional<SQLCondition> whereCondition = Optional.fromNullable(sql.getWhereCondition());
    Multimap<String, Condition> conditionMap = FluentIterable.from(whereCondition.asSet()).transformAndConcat(new Function<SQLCondition, Iterable<Condition>>() {

        @Override
        public Iterable<Condition> apply(SQLCondition sqlCondition) {
            Condition condition = sqlCondition.getCondition();
            condition.simplify();
            // Flatten first level of conditions
            if (!condition.isComposite()) {
                return Collections.singleton(condition);
            }
            // All child conditions must have allowable operators
            for (Condition child : condition.getChildren()) {
                if (!ALLOWED_OPERATORS.contains(child.getOperator())) {
                    return Collections.emptySet();
                }
            }
            return condition.getChildren();
        }
    }).filter(new Predicate<Condition>() {

        @Override
        public boolean apply(Condition condition) {
            // in the form of: WHERE A = 1
            return !condition.isComposite() && !condition.isNegated() && condition.getRightExact() != null && ALLOWED_FUNCTIONS.contains(condition.getFunction());
        }
    }).index(new Function<Condition, String>() {

        @Override
        public String apply(Condition condition) {
            // Group by field for easy look up
            return condition.getLeftValuename();
        }
    });
    Map<String, String> builder = Maps.newLinkedHashMap();
    for (Definition definition : definitions) {
        // There should be either 0 or 1 conditions for each field.
        for (Condition condition : conditionMap.get(definition.getFieldName())) {
            builder.put(definition.getParameter(), definition.format(condition));
        }
    }
    return ImmutableMap.copyOf(builder);
}
Also used : SQLCondition(org.pentaho.di.core.sql.SQLCondition) Condition(org.pentaho.di.core.Condition) SQLCondition(org.pentaho.di.core.sql.SQLCondition) FluentIterable(com.google.common.collect.FluentIterable) Predicate(com.google.common.base.Predicate) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Predicate (com.google.common.base.Predicate)1 FluentIterable (com.google.common.collect.FluentIterable)1 Condition (org.pentaho.di.core.Condition)1 SQLCondition (org.pentaho.di.core.sql.SQLCondition)1