Search in sources :

Example 11 with Expression

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

the class ConcatFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    Expression a = function.getParameters().get(0);
    Expression b = function.getParameters().get(1);
    List<Condition> crits = new ArrayList<Condition>();
    Literal nullValue = langFactory.createLiteral(null, TypeFacility.RUNTIME_TYPES.STRING);
    if (isNull(a)) {
        return Arrays.asList(nullValue);
    } else if (!isNotNull(a)) {
        crits.add(langFactory.createIsNullCriteria(a, false));
    }
    if (isNull(b)) {
        return Arrays.asList(nullValue);
    } else if (!isNotNull(b)) {
        crits.add(langFactory.createIsNullCriteria(b, false));
    }
    Condition crit = null;
    if (crits.isEmpty()) {
        return null;
    } else if (crits.size() == 1) {
        crit = crits.get(0);
    } else {
        crit = langFactory.createAndOr(Operator.OR, crits.get(0), crits.get(1));
    }
    List<SearchedWhenClause> cases = Arrays.asList(langFactory.createSearchedWhenCondition(crit, nullValue));
    return Arrays.asList(langFactory.createSearchedCaseExpression(cases, function, TypeFacility.RUNTIME_TYPES.STRING));
}
Also used : Condition(org.teiid.language.Condition) SearchedWhenClause(org.teiid.language.SearchedWhenClause) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList)

Example 12 with Expression

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

the class MonthOrDayNameFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    List<Expression> args = function.getParameters();
    Function func = // $NON-NLS-1$
    langFactory.createFunction(// $NON-NLS-1$
    "TO_CHAR", Arrays.asList(args.get(0), langFactory.createLiteral(format, TypeFacility.RUNTIME_TYPES.STRING)), TypeFacility.RUNTIME_TYPES.STRING);
    // For some reason, these values have trailing spaces
    Function trimFunc = langFactory.createFunction(SourceSystemFunctions.RTRIM, Arrays.asList(func), TypeFacility.RUNTIME_TYPES.STRING);
    return Arrays.asList(trimFunc);
}
Also used : Function(org.teiid.language.Function) Expression(org.teiid.language.Expression)

Example 13 with Expression

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

the class JPQLUpdateExecution method handleInsert.

private Object handleInsert(Insert insert) throws TranslatorException {
    try {
        String entityClassName = insert.getTable().getMetadataObject().getProperty(JPAMetadataProcessor.ENTITYCLASS, false);
        Object entity = ReflectionHelper.create(entityClassName, null, this.executionContext.getCommandContext().getVDBClassLoader());
        List<ColumnReference> columns = insert.getColumns();
        List<Expression> values = ((ExpressionValueSource) insert.getValueSource()).getValues();
        if (columns.size() != values.size()) {
            throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14007));
        }
        for (int i = 0; i < columns.size(); i++) {
            Column column = columns.get(i).getMetadataObject();
            Object value = values.get(i);
            // do not add the derived columns
            String name = column.getProperty(JPAMetadataProcessor.KEY_ASSOSIATED_WITH_FOREIGN_TABLE, false);
            if (name == null) {
                if (value instanceof Literal) {
                    Literal literalValue = (Literal) value;
                    PropertiesUtils.setBeanProperty(entity, column.getName(), literalValue.getValue());
                } else {
                    PropertiesUtils.setBeanProperty(entity, column.getName(), value);
                }
            }
        }
        return entity;
    } catch (TeiidException e) {
        throw new TranslatorException(e);
    }
}
Also used : Expression(org.teiid.language.Expression) Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference) ExpressionValueSource(org.teiid.language.ExpressionValueSource) TeiidException(org.teiid.core.TeiidException)

Example 14 with Expression

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

the class CoherenceUpdateExecution method updateChildObject.

private void updateChildObject(Table t) throws TranslatorException {
    List<ForeignKey> fks = t.getForeignKeys();
    ForeignKey fk = fks.get(0);
    Table parentTable = fk.getParent();
    // the name of the method to obtain the collection is the nameInSource of the foreginKey
    String parentToChildMethod = fk.getNameInSource();
    if (parentToChildMethod == null) {
        // $NON-NLS-1$
        final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noNameInSourceForForeingKey", new Object[] { fk.getName() });
        throw new TranslatorException(msg);
    }
    // there must only be 1 column in the primary key
    String parentColName = visitor.getNameFromElement(fk.getPrimaryKey().getColumns().get(0));
    List<SetClause> updateList = ((Update) command).getChanges();
    Condition criteria = ((Update) command).getWhere();
    ColumnReference leftElement;
    Expression rightExpr;
    String nameLeftElement;
    Object valueRightExpr;
    // API).
    for (int i = 0; i < updateList.size(); i++) {
        SetClause setClause = updateList.get(i);
        // trust that connector API is right and left side
        // will always be an IElement
        leftElement = setClause.getSymbol();
        // call utility method to get NameInSource/Name for element
        nameLeftElement = visitor.getNameFromElement(leftElement.getMetadataObject());
        // get right expression - if it is not a literal we
        // can't handle that so throw an exception
        rightExpr = setClause.getValue();
        // if (!(rightExpr instanceof Literal)) {
        // final String msg = CoherencePlugin.Util.getString("LDAPUpdateExecution.valueNotLiteralError",nameLeftElement); //$NON-NLS-1$
        // throw new TranslatorException(msg);
        // }
        valueRightExpr = ((Literal) rightExpr).getValue();
    // add in the modification as a replacement - meaning
    // any existing value(s) for this attribute will
    // be replaced by the new value.  If the attribute
    // didn't exist, it will automatically be created
    // TODO - since null is a valid attribute
    // value, we don't do any special handling of it right
    // now.  But maybe null should mean to delete an
    // attribute?
    }
}
Also used : Condition(org.teiid.language.Condition) Table(org.teiid.metadata.Table) ForeignKey(org.teiid.metadata.ForeignKey) Update(org.teiid.language.Update) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) SetClause(org.teiid.language.SetClause) ColumnReference(org.teiid.language.ColumnReference)

Example 15 with Expression

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

the class CoherenceVisitor method visit.

public void visit(Comparison obj) {
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Parsing Comparison criteria.");
    try {
        Comparison.Operator op = ((Comparison) obj).getOperator();
        Expression lhs = ((Comparison) obj).getLeftExpression();
        Expression rhs = ((Comparison) obj).getRightExpression();
        String lhsString = getExpressionString(lhs);
        String rhsString = getExpressionString(rhs);
        if (lhsString == null || rhsString == null) {
            // $NON-NLS-1$
            final String msg = CoherencePlugin.Util.getString("CoherenceVisitor.missingComparisonExpression");
            exception = new TranslatorException(msg);
        }
        if (rhs instanceof Literal || lhs instanceof Literal) {
            if (rhs instanceof Literal) {
                Literal literal = (Literal) rhs;
                addCompareCriteria(lhsString, literal.getValue(), op, literal.getType());
            // filter = CoherenceFilterUtil.createCompareFilter(lhsString, literal.getValue(), op, literal.getType() );
            } else {
                Literal literal = (Literal) lhs;
                addCompareCriteria(rhsString, literal.getValue(), op, literal.getType());
            // filter = CoherenceFilterUtil.createCompareFilter(rhsString, literal.getValue(), op, literal.getType() );
            }
        }
    } catch (TranslatorException t) {
        exception = t;
    }
}
Also used : Comparison(org.teiid.language.Comparison) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) Operator(org.teiid.language.Comparison.Operator)

Aggregations

Expression (org.teiid.language.Expression)61 ArrayList (java.util.ArrayList)18 ExpressionValueSource (org.teiid.language.ExpressionValueSource)18 Literal (org.teiid.language.Literal)17 TranslatorException (org.teiid.translator.TranslatorException)16 ColumnReference (org.teiid.language.ColumnReference)14 Function (org.teiid.language.Function)13 Column (org.teiid.metadata.Column)12 Insert (org.teiid.language.Insert)11 List (java.util.List)10 Table (org.teiid.metadata.Table)9 BigInteger (java.math.BigInteger)7 Test (org.junit.Test)7 Parameter (org.teiid.language.Parameter)7 Iterator (java.util.Iterator)5 Comparison (org.teiid.language.Comparison)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)4 NamedTable (org.teiid.language.NamedTable)4