Search in sources :

Example 1 with Operator

use of org.pentaho.platform.dataaccess.metadata.model.Operator in project data-access by pentaho.

the class ConditionTest method testGetConditionWithParametersForAllDataTypes.

@Test
public void testGetConditionWithParametersForAllDataTypes() {
    Operator[] operatorsType1Array = { Operator.GREATER_THAN, Operator.LESS_THAN, Operator.EQUAL, Operator.GREATOR_OR_EQUAL, Operator.LESS_OR_EQUAL };
    for (Operator operator : operatorsType1Array) {
        when(icondition.getOperator()).thenReturn(operator.toString());
        for (DataType dataType : DataType.values()) {
            if (dataType.getName().equals(DataType.STRING.getName()) && operator.equals(Operator.EQUAL)) {
                // Exception for DataType STRING
                Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] = [param:" + paramName + "]"));
            } else if (dataType.getName().equals(DataType.DATE.getName())) {
                Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "DATEVALUE([param:" + values[0] + "])"));
            } else {
                Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "[param:" + paramName + "]"));
            }
        }
    }
    for (DataType dataType : DataType.values()) {
        when(icondition.getOperator()).thenReturn(Operator.EXACTLY_MATCHES.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] = \"DATEVALUE([param:" + values[0] + "])\""));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] = " + "[param:" + paramName + "]"));
        }
        when(icondition.getOperator()).thenReturn(Operator.CONTAINS.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("CONTAINS([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("CONTAINS([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "])"));
        }
        when(icondition.getOperator()).thenReturn(Operator.DOES_NOT_CONTAIN.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("NOT(CONTAINS([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\"))"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("NOT(CONTAINS([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "]))"));
        }
        when(icondition.getOperator()).thenReturn(Operator.BEGINS_WITH.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("BEGINSWITH([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("BEGINSWITH([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "])"));
        }
        when(icondition.getOperator()).thenReturn(Operator.ENDS_WITH.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("ENDSWITH([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("ENDSWITH([" + categoryName + "." + columnName + "];" + "[param:" + paramName + "])"));
        }
        when(icondition.getOperator()).thenReturn(Operator.IS_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("ISNA([" + categoryName + "." + columnName + "])"));
        when(icondition.getOperator()).thenReturn(Operator.IS_NOT_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("NOT(ISNA([" + categoryName + "." + columnName + "]))"));
    }
}
Also used : Operator(org.pentaho.platform.dataaccess.metadata.model.Operator) DataType(org.pentaho.metadata.model.concept.types.DataType) Test(org.junit.Test)

Example 2 with Operator

use of org.pentaho.platform.dataaccess.metadata.model.Operator in project data-access by pentaho.

the class OperatorTest method testFormatConditionForParameterizedSingleValues.

@Test
public void testFormatConditionForParameterizedSingleValues() {
    String columnName = "column_name";
    String paramName = "param_name";
    String[] values = { "value1" };
    Operator[] operatorsArray = { Operator.GREATER_THAN, Operator.LESS_THAN, Operator.EQUAL, Operator.GREATOR_OR_EQUAL, Operator.LESS_OR_EQUAL };
    // check with different number of values passed as argument
    for (Operator operator : operatorsArray) {
        Assert.assertThat(operator.formatCondition(columnName, paramName, values, true), is(columnName + " " + operator.toString() + "[param:" + paramName + "]"));
    }
    Assert.assertThat(Operator.EXACTLY_MATCHES.formatCondition(columnName, paramName, values, true), is(columnName + " = " + "[param:" + paramName + "]"));
    Assert.assertThat(Operator.CONTAINS.formatCondition(columnName, paramName, values, true), is("CONTAINS(" + columnName + ";" + "[param:" + paramName + "])"));
    Assert.assertThat(Operator.DOES_NOT_CONTAIN.formatCondition(columnName, paramName, values, true), is("NOT(CONTAINS(" + columnName + ";" + "[param:" + paramName + "]))"));
    Assert.assertThat(Operator.BEGINS_WITH.formatCondition(columnName, paramName, values, true), is("BEGINSWITH(" + columnName + ";" + "[param:" + paramName + "])"));
    Assert.assertThat(Operator.ENDS_WITH.formatCondition(columnName, paramName, values, true), is("ENDSWITH(" + columnName + ";" + "[param:" + paramName + "])"));
    Assert.assertThat(Operator.IS_NULL.formatCondition(columnName, paramName, values, true), is("ISNA(" + columnName + ")"));
    Assert.assertThat(Operator.IS_NOT_NULL.formatCondition(columnName, paramName, values, true), is("NOT(ISNA(" + columnName + "))"));
}
Also used : Operator(org.pentaho.platform.dataaccess.metadata.model.Operator) Test(org.junit.Test)

Example 3 with Operator

use of org.pentaho.platform.dataaccess.metadata.model.Operator in project data-access by pentaho.

the class OperatorTest method testFormatConditionForParameterizedMultipleValues.

@Test
public void testFormatConditionForParameterizedMultipleValues() {
    String columnName = "column_name";
    String paramName = "param_name";
    String[] values = { "\"value1\"", "value2", "value3" };
    Operator[] operatorsArray = { Operator.GREATER_THAN, Operator.LESS_THAN, Operator.EQUAL, Operator.GREATOR_OR_EQUAL, Operator.LESS_OR_EQUAL };
    // check with different number of values passed as argument
    for (Operator operator : operatorsArray) {
        Assert.assertThat(operator.formatCondition(columnName, paramName, values, true), is(columnName + " " + operator.toString() + "[param:" + paramName + "]"));
    }
    Assert.assertThat(Operator.EXACTLY_MATCHES.formatCondition(columnName, paramName, values, true), is(columnName + " = " + "[param:" + paramName + "]"));
    Assert.assertThat(Operator.CONTAINS.formatCondition(columnName, paramName, values, true), is("CONTAINS(" + columnName + ";" + "[param:" + paramName + "])"));
    Assert.assertThat(Operator.DOES_NOT_CONTAIN.formatCondition(columnName, paramName, values, true), is("NOT(CONTAINS(" + columnName + ";" + "[param:" + paramName + "]))"));
    Assert.assertThat(Operator.BEGINS_WITH.formatCondition(columnName, paramName, values, true), is("BEGINSWITH(" + columnName + ";" + "[param:" + paramName + "])"));
    Assert.assertThat(Operator.ENDS_WITH.formatCondition(columnName, paramName, values, true), is("ENDSWITH(" + columnName + ";" + "[param:" + paramName + "])"));
    Assert.assertThat(Operator.IS_NULL.formatCondition(columnName, paramName, values, true), is("ISNA(" + columnName + ")"));
    Assert.assertThat(Operator.IS_NOT_NULL.formatCondition(columnName, paramName, values, true), is("NOT(ISNA(" + columnName + "))"));
}
Also used : Operator(org.pentaho.platform.dataaccess.metadata.model.Operator) Test(org.junit.Test)

Example 4 with Operator

use of org.pentaho.platform.dataaccess.metadata.model.Operator in project data-access by pentaho.

the class Condition method getCondition.

public String getCondition(String type, String paramName) {
    String[] val = getValue().clone();
    /*
    if(val == null && defaultValue != null) {
      val = defaultValue;
    }
*/
    Operator theOperator = Operator.parse(getOperator());
    if (type.equalsIgnoreCase(DataType.STRING.getName()) && theOperator == Operator.EQUAL) {
        theOperator = Operator.EXACTLY_MATCHES;
    }
    if (type.equalsIgnoreCase(DataType.STRING.getName())) {
        for (int idx = 0; idx < val.length; idx++) {
            // $NON-NLS-1$ //$NON-NLS-2$
            val[idx] = "\"" + val[idx] + "\"";
        }
    }
    boolean enforceParameters = paramName != null;
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$;
    String columnName = "[" + getCategory() + "." + getColumn() + "]";
    // Date is a special case where we craft a formula function.
    if (type.equals(DataType.DATE.getName())) {
        if (enforceParameters) {
            // the value needs to happen here instead of letting the Operator class handle it.
            for (int idx = 0; idx < val.length; idx++) {
                val[idx] = "DATEVALUE(" + "[param:" + getValue()[idx].replaceAll("[\\{\\}]", "") + "]" + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
                ")";
            }
            return theOperator.formatCondition(columnName, paramName, val, false);
        } else {
            for (int idx = 0; idx < val.length; idx++) {
                // $NON-NLS-1$ //$NON-NLS-2$
                val[idx] = "DATEVALUE(\"" + val[idx] + "\")";
            }
        }
    }
    return theOperator.formatCondition(columnName, paramName, val, enforceParameters);
}
Also used : Operator(org.pentaho.platform.dataaccess.metadata.model.Operator)

Example 5 with Operator

use of org.pentaho.platform.dataaccess.metadata.model.Operator in project data-access by pentaho.

the class ConditionTest method testGetConditionWithoutParametersForAllDataTypes.

@Test
public void testGetConditionWithoutParametersForAllDataTypes() {
    Operator[] operatorsType1Array = { Operator.GREATER_THAN, Operator.LESS_THAN, Operator.EQUAL, Operator.GREATOR_OR_EQUAL, Operator.LESS_OR_EQUAL };
    for (Operator operator : operatorsType1Array) {
        when(icondition.getOperator()).thenReturn(operator.toString());
        for (DataType dataType : DataType.values()) {
            if (dataType.getName().equals(DataType.STRING.getName())) {
                // Exception for DataType STRING
                if (operator.equals(Operator.EQUAL)) {
                    Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] = \"" + values[0] + "\""));
                } else {
                    Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "\"" + values[0] + "\""));
                }
            } else if (dataType.getName().equals(DataType.DATE.getName())) {
                Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] " + operator.toString() + "DATEVALUE(\"" + values[0] + "\")"));
            } else {
                Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] " + operator.toString() + values[0]));
            }
        }
    }
    for (DataType dataType : DataType.values()) {
        when(icondition.getOperator()).thenReturn(Operator.EXACTLY_MATCHES.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("[" + categoryName + "." + columnName + "] = \"DATEVALUE([param:" + values[0] + "])\""));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("[" + categoryName + "." + columnName + "] = \"" + values[0] + "\""));
        }
        when(icondition.getOperator()).thenReturn(Operator.CONTAINS.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("CONTAINS([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("CONTAINS([" + categoryName + "." + columnName + "];\"" + values[0] + "\")"));
        }
        when(icondition.getOperator()).thenReturn(Operator.DOES_NOT_CONTAIN.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("NOT(CONTAINS([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\"))"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("NOT(CONTAINS([" + categoryName + "." + columnName + "];\"" + values[0] + "\"))"));
        }
        when(icondition.getOperator()).thenReturn(Operator.BEGINS_WITH.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("BEGINSWITH([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("BEGINSWITH([" + categoryName + "." + columnName + "];\"" + values[0] + "\")"));
        }
        when(icondition.getOperator()).thenReturn(Operator.ENDS_WITH.toString());
        if (dataType.getName().equals(DataType.DATE.getName())) {
            Assert.assertThat(icondition.getCondition(dataType.getName(), paramName), is("ENDSWITH([" + categoryName + "." + columnName + "];\"DATEVALUE([param:" + values[0] + "])\")"));
        } else {
            Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("ENDSWITH([" + categoryName + "." + columnName + "];\"" + values[0] + "\")"));
        }
        when(icondition.getOperator()).thenReturn(Operator.IS_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("ISNA([" + categoryName + "." + columnName + "])"));
        when(icondition.getOperator()).thenReturn(Operator.IS_NOT_NULL.toString());
        Assert.assertThat(icondition.getCondition(dataType.getName(), null), is("NOT(ISNA([" + categoryName + "." + columnName + "]))"));
    }
}
Also used : Operator(org.pentaho.platform.dataaccess.metadata.model.Operator) DataType(org.pentaho.metadata.model.concept.types.DataType) Test(org.junit.Test)

Aggregations

Operator (org.pentaho.platform.dataaccess.metadata.model.Operator)7 Test (org.junit.Test)6 DataType (org.pentaho.metadata.model.concept.types.DataType)2