Search in sources :

Example 1 with SearchedCase

use of org.teiid.language.SearchedCase in project teiid by teiid.

the class SubstringFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    this.modify(function);
    Expression from = function.getParameters().get(1);
    Boolean isFromNegative = isNegative(from);
    Function length = new Function(SourceSystemFunctions.LENGTH, Arrays.asList(function.getParameters().get(0)), TypeFacility.RUNTIME_TYPES.INTEGER);
    if (function.getParameters().size() == 2 && (isFromNegative == null || isFromNegative)) {
        // couchbase does not handle default length with a negative from index
        function.getParameters().add(length);
    }
    if (function.getParameters().size() == 3) {
        // case when length > LENGTH(string) - start + 1 then LENGTH(string) - start + 1 case when length > 0 then length end
        Expression forLength = function.getParameters().get(2);
        List<SearchedWhenClause> clauses = new ArrayList<SearchedWhenClause>(2);
        Boolean isNegative = isNegative(forLength);
        Expression adjustedFrom = from;
        if (isFromNegative == null || isFromNegative) {
            adjustedFrom = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(from, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.LT), new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(length, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER), from), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
        }
        Expression maxLength = new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(length, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER);
        clauses.add(new SearchedWhenClause(new Comparison(forLength, maxLength, Operator.GT), maxLength));
        Expression defaultExpr = null;
        if (isNegative == null) {
            clauses.add(new SearchedWhenClause(new Comparison(forLength, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), forLength));
        } else if (isNegative) {
            // TODO: could be done in the rewriter
            return Arrays.asList(new Literal(null, TypeFacility.RUNTIME_TYPES.STRING));
        } else {
            defaultExpr = forLength;
        }
        SearchedCase sc = new SearchedCase(clauses, defaultExpr, TypeFacility.RUNTIME_TYPES.INTEGER);
        function.getParameters().set(2, sc);
    }
    Expression adjustedFrom = function.getParameters().get(1);
    if (isFromNegative == null) {
        // case when start > 0 then start - 1 else start end
        SearchedCase sc = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(adjustedFrom, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
        function.getParameters().set(1, sc);
    } else if (!isFromNegative) {
        function.getParameters().set(1, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER));
    }
    return null;
}
Also used : SearchedCase(org.teiid.language.SearchedCase) Function(org.teiid.language.Function) SearchedWhenClause(org.teiid.language.SearchedWhenClause) Expression(org.teiid.language.Expression) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList)

Example 2 with SearchedCase

use of org.teiid.language.SearchedCase in project teiid by teiid.

the class CoherenceVisitor method getExpressionString.

// GHH 20080326 - found that code to fall back on Name if NameInSource
// was null wasn't working properly, so replaced with tried and true
// code from another custom connector.
private String getExpressionString(Expression e) throws TranslatorException {
    String expressionName = null;
    // - the rest of this method is unchanged
    if (e instanceof ColumnReference) {
        Column mdIDElement = ((ColumnReference) e).getMetadataObject();
        expressionName = mdIDElement.getNameInSource();
        if (expressionName == null || expressionName.equals("")) {
            // $NON-NLS-1$
            expressionName = mdIDElement.getName();
        }
    } else if (e instanceof Literal) {
        // try {
        // if(((Literal)e).getType().equals(Class.forName(Timestamp.class.getName()))) {
        // LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Found an expression that uses timestamp; converting to LDAP string format."); //$NON-NLS-1$
        // Timestamp ts = (Timestamp)((Literal)e).getValue();
        // Date dt = new Date(ts.getTime());
        // //TODO: Fetch format if provided.
        // SimpleDateFormat sdf = new SimpleDateFormat(LDAPConnectorConstants.ldapTimestampFormat);
        // expressionName = sdf.format(dt);
        // LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Timestamp to stsring is: " + expressionName); //$NON-NLS-1$
        // }
        // else {
        // expressionName = ((Literal)e).getValue().toString();
        // }
        expressionName = ((Literal) e).getValue().toString();
    // } catch (ClassNotFoundException cce) {
    // final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.timestampClassNotFoundError"); //$NON-NLS-1$
    // throw new TranslatorException(cce, msg);
    // }
    // 
    } else {
        if (e instanceof AggregateFunction) {
            // $NON-NLS-1$
            LogManager.logError(LogConstants.CTX_CONNECTOR, "Received IAggregate, but it is not supported. Check capabilities.");
        } else if (e instanceof Function) {
            // $NON-NLS-1$
            LogManager.logError(LogConstants.CTX_CONNECTOR, "Received IFunction, but it is not supported. Check capabilties.");
        } else if (e instanceof ScalarSubquery) {
            // $NON-NLS-1$
            LogManager.logError(LogConstants.CTX_CONNECTOR, "Received IScalarSubquery, but it is not supported. Check capabilties.");
        } else if (e instanceof SearchedCase) {
            // $NON-NLS-1$
            LogManager.logError(LogConstants.CTX_CONNECTOR, "Received ISearchedCaseExpression, but it is not supported. Check capabilties.");
        }
        // $NON-NLS-1$
        final String msg = CoherencePlugin.Util.getString("CoherenceVisitory.unsupportedElementError", e.toString());
        throw new TranslatorException(msg);
    }
    expressionName = escapeReservedChars(expressionName);
    return expressionName;
}
Also used : SearchedCase(org.teiid.language.SearchedCase) Function(org.teiid.language.Function) AggregateFunction(org.teiid.language.AggregateFunction) ScalarSubquery(org.teiid.language.ScalarSubquery) Column(org.teiid.metadata.Column) DerivedColumn(org.teiid.language.DerivedColumn) Literal(org.teiid.language.Literal) AggregateFunction(org.teiid.language.AggregateFunction) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

Example 3 with SearchedCase

use of org.teiid.language.SearchedCase in project teiid by teiid.

the class SubstringFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    this.modify(function);
    if (function.getParameters().size() != 3) {
        return null;
    }
    // case when length > LENGTH(string) - start + 1 then LENGTH(string) - start + 1 case when length > 0 then length end
    Expression forLength = function.getParameters().get(2);
    List<SearchedWhenClause> clauses = new ArrayList<SearchedWhenClause>(2);
    Boolean isNegative = null;
    if (forLength instanceof Literal) {
        Literal l = (Literal) forLength;
        int value = (Integer) l.getValue();
        isNegative = value < 0;
    }
    Function length = new Function(SourceSystemFunctions.LENGTH, Arrays.asList(function.getParameters().get(0)), TypeFacility.RUNTIME_TYPES.INTEGER);
    Expression from = function.getParameters().get(1);
    SearchedCase adjustedFrom = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(from, length, Operator.GT), new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(length, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
    function.getParameters().set(1, adjustedFrom);
    Expression maxLength = new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(length, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER);
    clauses.add(new SearchedWhenClause(new Comparison(forLength, maxLength, Operator.GT), maxLength));
    Expression defaultExpr = null;
    if (isNegative == null) {
        clauses.add(new SearchedWhenClause(new Comparison(forLength, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), forLength));
    } else if (isNegative) {
        // TODO: could be done in the rewriter
        return Arrays.asList(new Literal(null, TypeFacility.RUNTIME_TYPES.STRING));
    } else {
        defaultExpr = forLength;
    }
    SearchedCase sc = new SearchedCase(clauses, defaultExpr, TypeFacility.RUNTIME_TYPES.INTEGER);
    function.getParameters().set(2, sc);
    return null;
}
Also used : SearchedCase(org.teiid.language.SearchedCase) Function(org.teiid.language.Function) SearchedWhenClause(org.teiid.language.SearchedWhenClause) Expression(org.teiid.language.Expression) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList)

Aggregations

Function (org.teiid.language.Function)3 Literal (org.teiid.language.Literal)3 SearchedCase (org.teiid.language.SearchedCase)3 ArrayList (java.util.ArrayList)2 Comparison (org.teiid.language.Comparison)2 Expression (org.teiid.language.Expression)2 SearchedWhenClause (org.teiid.language.SearchedWhenClause)2 AggregateFunction (org.teiid.language.AggregateFunction)1 ColumnReference (org.teiid.language.ColumnReference)1 DerivedColumn (org.teiid.language.DerivedColumn)1 ScalarSubquery (org.teiid.language.ScalarSubquery)1 Column (org.teiid.metadata.Column)1 TranslatorException (org.teiid.translator.TranslatorException)1