Search in sources :

Example 6 with ExpressionValueSource

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

the class ODataUpdateVisitor method visit.

@Override
public void visit(Insert obj) {
    // $NON-NLS-1$
    this.method = "POST";
    this.entity = obj.getTable().getMetadataObject();
    this.uri = this.entity.getName();
    final List<OProperty<?>> props = new ArrayList<OProperty<?>>();
    int elementCount = obj.getColumns().size();
    for (int i = 0; i < elementCount; i++) {
        Column column = obj.getColumns().get(i).getMetadataObject();
        List<Expression> values = ((ExpressionValueSource) obj.getValueSource()).getValues();
        OProperty<?> property = readProperty(column, values.get(i));
        props.add(property);
    }
    this.payload = props;
}
Also used : OProperty(org.odata4j.core.OProperty) Column(org.teiid.metadata.Column) Expression(org.teiid.language.Expression) ArrayList(java.util.ArrayList) ExpressionValueSource(org.teiid.language.ExpressionValueSource)

Example 7 with ExpressionValueSource

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

the class InfinispanUpdateVisitor method visit.

@Override
public void visit(Insert obj) {
    this.operationType = OperationType.INSERT;
    if (obj.isUpsert()) {
        this.operationType = OperationType.UPSERT;
    }
    visitNode(obj.getTable());
    Column pkColumn = getPrimaryKey();
    if (pkColumn == null) {
        this.exceptions.add(new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25013, getParentTable().getName())));
        return;
    }
    if (obj.getParameterValues() == null) {
        List<Expression> values = ((ExpressionValueSource) obj.getValueSource()).getValues();
        this.insertPayload = buildInsertPayload(obj, values);
        if (this.insertPayload != null) {
            this.identity = this.insertPayload.getIdentifier();
        }
    }
}
Also used : Column(org.teiid.metadata.Column) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) ExpressionValueSource(org.teiid.language.ExpressionValueSource)

Example 8 with ExpressionValueSource

use of org.teiid.language.ExpressionValueSource 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 9 with ExpressionValueSource

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

the class TestJDBCUpdateExecution method testPreparedBatchedUpdateFailed.

@Test
public void testPreparedBatchedUpdateFailed() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey) values (1)");
    Parameter param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    param.setValueIndex(0);
    List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
    values.set(0, param);
    command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement s = Mockito.mock(PreparedStatement.class);
    Mockito.stub(s.executeBatch()).toThrow(new BatchUpdateException(new int[] { 1, Statement.EXECUTE_FAILED }));
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    ResultSet r = Mockito.mock(ResultSet.class);
    ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
    Mockito.stub(r.getMetaData()).toReturn(rs);
    FakeExecutionContextImpl context = new FakeExecutionContextImpl();
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    try {
        updateExecution.execute();
        fail();
    } catch (TranslatorBatchException e) {
        int[] counts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1, -3 }, counts);
    }
    // test multiple batches
    connection = Mockito.mock(Connection.class);
    updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
    s = Mockito.mock(PreparedStatement.class);
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
    Mockito.stub(s.executeBatch()).toReturn(new int[] { 1 }).toThrow(new BatchUpdateException(new int[] { Statement.EXECUTE_FAILED }));
    updateExecution.setMaxPreparedInsertBatchSize(1);
    try {
        updateExecution.execute();
        fail();
    } catch (TranslatorBatchException e) {
        int[] counts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1, -3 }, counts);
    }
    // test only a single update count
    connection = Mockito.mock(Connection.class);
    updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
    s = Mockito.mock(PreparedStatement.class);
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
    Mockito.stub(s.executeBatch()).toThrow(new BatchUpdateException(new int[] { 1 }));
    try {
        updateExecution.execute();
        fail();
    } catch (TranslatorBatchException e) {
        int[] counts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1 }, counts);
    }
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) TranslatorBatchException(org.teiid.translator.TranslatorBatchException) ResultSetMetaData(java.sql.ResultSetMetaData) Expression(org.teiid.language.Expression) ResultSet(java.sql.ResultSet) Parameter(org.teiid.language.Parameter) ExpressionValueSource(org.teiid.language.ExpressionValueSource) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Example 10 with ExpressionValueSource

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

the class TestJDBCUpdateExecution method testInsertIteratorUpdate.

@Test
public void testInsertIteratorUpdate() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey, IntNum) values (1, 2)");
    Parameter param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    param.setValueIndex(0);
    List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
    values.set(0, param);
    param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    param.setValueIndex(1);
    values.set(1, param);
    command.setParameterValues(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(1, 2)).iterator());
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement p = Mockito.mock(PreparedStatement.class);
    Mockito.stub(p.executeBatch()).toReturn(new int[] { 1, 1 });
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey, IntNum) VALUES (?, ?)")).toReturn(p);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, new FakeExecutionContextImpl(), config);
    updateExecution.execute();
    Mockito.verify(p, Mockito.times(2)).addBatch();
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Expression(org.teiid.language.Expression) Connection(java.sql.Connection) Parameter(org.teiid.language.Parameter) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Aggregations

ExpressionValueSource (org.teiid.language.ExpressionValueSource)19 Expression (org.teiid.language.Expression)18 Insert (org.teiid.language.Insert)11 ColumnReference (org.teiid.language.ColumnReference)10 Test (org.junit.Test)8 Literal (org.teiid.language.Literal)7 Parameter (org.teiid.language.Parameter)7 ArrayList (java.util.ArrayList)6 Column (org.teiid.metadata.Column)6 TranslatorException (org.teiid.translator.TranslatorException)6 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)5 Table (org.teiid.metadata.Table)5 ResultSet (java.sql.ResultSet)3 ResultSetMetaData (java.sql.ResultSetMetaData)3 Iterator (java.util.Iterator)3 List (java.util.List)3 NamedTable (org.teiid.language.NamedTable)3 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)3