Search in sources :

Example 11 with ColumnReference

use of org.teiid.language.ColumnReference 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 12 with ColumnReference

use of org.teiid.language.ColumnReference 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 13 with ColumnReference

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

the class ParmHolder method createObject.

public Object createObject(List<ColumnReference> columnList, List<Expression> valueList, CoherenceVisitor visitor, Table t) throws TranslatorException {
    if (columnList.size() != valueList.size()) {
        throw new TranslatorException("Error:  columns.size and values.size are not the same.");
    }
    // create the new object that will either be added as a top level object or added to the parent container
    String tableName = visitor.getNameFromTable(t);
    Object newObject = createObjectFromMetadata(tableName);
    for (int i = 0; i < columnList.size(); i++) {
        final ColumnReference insertElement = columnList.get(i);
        if (!insertElement.getMetadataObject().isUpdatable())
            continue;
        final String elementName = visitor.getNameFromElement(insertElement.getMetadataObject());
        final Object value = valueList.get(i);
        Object val;
        if (value instanceof Literal) {
            Literal literalValue = (Literal) value;
            val = literalValue.getValue();
            // .toString();
            if (null != val && val instanceof String) {
                // !val.isEmpty()) {
                val = stripQutes((String) val);
            }
        } else {
            val = value;
        // .toString();
        }
        setValue(tableName, elementName, newObject, val, insertElement.getType());
    }
    return newObject;
}
Also used : Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

Example 14 with ColumnReference

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

the class TestOracleConvertModifier method testClobToString.

@Test
public void testClobToString() throws Exception {
    assertTrue(TRANSLATOR.supportsConvert(TypeFacility.RUNTIME_CODES.CLOB, TypeFacility.RUNTIME_CODES.STRING));
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTest(new ColumnReference(null, "x", null, DataTypeManager.DefaultDataClasses.CLOB), "string", "DBMS_LOB.substr(x, 4000)");
}
Also used : ColumnReference(org.teiid.language.ColumnReference) Test(org.junit.Test)

Example 15 with ColumnReference

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

the class TestExtractFunctionModifier method test11.

public void test11() throws Exception {
    // $NON-NLS-1$
    NamedTable group = LANG_FACTORY.createNamedTable("group", null, null);
    // $NON-NLS-1$
    ColumnReference elem = LANG_FACTORY.createColumnReference("col", group, null, TypeFacility.RUNTIME_TYPES.DATE);
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTestMod(elem, "EXTRACT(DAY FROM group.col)", "dayofmonth");
}
Also used : NamedTable(org.teiid.language.NamedTable) ColumnReference(org.teiid.language.ColumnReference)

Aggregations

ColumnReference (org.teiid.language.ColumnReference)35 Expression (org.teiid.language.Expression)14 NamedTable (org.teiid.language.NamedTable)13 Column (org.teiid.metadata.Column)12 ArrayList (java.util.ArrayList)11 TranslatorException (org.teiid.translator.TranslatorException)11 ExpressionValueSource (org.teiid.language.ExpressionValueSource)10 Literal (org.teiid.language.Literal)10 Table (org.teiid.metadata.Table)10 DerivedColumn (org.teiid.language.DerivedColumn)8 Test (org.junit.Test)7 Select (org.teiid.language.Select)6 Insert (org.teiid.language.Insert)5 List (java.util.List)4 Iterator (java.util.Iterator)3 Function (org.teiid.language.Function)3 Parameter (org.teiid.language.Parameter)3 SetQuery (org.teiid.language.SetQuery)3 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)3 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)3