use of org.teiid.language.Insert in project teiid by teiid.
the class TestUpdates method testBulkUpdate.
@Test
public void testBulkUpdate() throws Exception {
CassandraExecutionFactory cef = new CassandraExecutionFactory();
String input = "insert into pm1.g1 (e1) values ('a')";
TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
Command command = util.parseCommand(input);
Insert insert = (Insert) command;
Parameter p = new Parameter();
p.setType(String.class);
p.setValueIndex(0);
((ExpressionValueSource) insert.getValueSource()).getValues().set(0, p);
insert.setParameterValues(Arrays.asList(Arrays.asList("a"), Arrays.asList("b")).iterator());
ExecutionContext ec = Mockito.mock(ExecutionContext.class);
RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
CassandraConnection connection = Mockito.mock(CassandraConnection.class);
ResultSetFuture rsf = Mockito.mock(ResultSetFuture.class);
Mockito.stub(rsf.isDone()).toReturn(true);
Mockito.stub(connection.executeBatch(Mockito.eq("INSERT INTO g1 (e1) VALUES (?)"), (List<Object[]>) Mockito.anyObject())).toReturn(rsf);
UpdateExecution execution = (UpdateExecution) cef.createExecution(command, ec, rm, connection);
execution.execute();
assertArrayEquals(new int[] { 2 }, execution.getUpdateCounts());
}
use of org.teiid.language.Insert in project teiid by teiid.
the class TestInsertImpl method testGetValues.
public void testGetValues() throws Exception {
// $NON-NLS-1$
Insert insert = example("a.b");
assertNotNull(insert.getValueSource());
assertEquals(4, ((ExpressionValueSource) insert.getValueSource()).getValues().size());
for (Iterator i = ((ExpressionValueSource) insert.getValueSource()).getValues().iterator(); i.hasNext(); ) {
assertTrue(i.next() instanceof Expression);
}
}
use of org.teiid.language.Insert in project teiid by teiid.
the class JDBCUpdateExecution method executeTranslatedCommand.
/**
* @param translatedComm
* @throws TranslatorException
* @since 4.3
*/
private void executeTranslatedCommand(TranslatedCommand translatedComm) throws TranslatorException {
// create statement or PreparedStatement and execute
String sql = translatedComm.getSql();
boolean commitType = false;
boolean succeeded = false;
try {
int updateCount = 0;
Class<?>[] keyColumnDataTypes = null;
String[] keyColumnNames = null;
if (command instanceof Insert && context.getCommandContext().isReturnAutoGeneratedKeys() && executionFactory.supportsGeneratedKeys(context, command)) {
Insert insert = (Insert) command;
NamedTable nt = insert.getTable();
if (nt.getMetadataObject() != null) {
KeyRecord key = nt.getMetadataObject().getPrimaryKey();
if (key != null) {
List<Column> cols = key.getColumns();
keyColumnDataTypes = new Class<?>[cols.size()];
keyColumnNames = new String[cols.size()];
for (int i = 0; i < cols.size(); i++) {
Column c = cols.get(i);
keyColumnDataTypes[i] = c.getJavaType();
// won't work in scenarios where the teiid name is changed or contains a .
keyColumnNames[i] = c.getName();
}
}
}
}
if (!translatedComm.isPrepared()) {
statement = getStatement();
// handle autoGeneratedKeys
if (keyColumnDataTypes != null) {
if (executionFactory.useColumnNamesForGeneratedKeys()) {
updateCount = statement.executeUpdate(sql, keyColumnNames);
} else {
updateCount = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
}
} else {
updateCount = statement.executeUpdate(sql);
}
result = new int[] { updateCount };
addStatementWarnings();
} else {
PreparedStatement pstatement = null;
if (statement != null) {
statement.close();
}
if (keyColumnDataTypes != null) {
if (executionFactory.useColumnNamesForGeneratedKeys()) {
pstatement = connection.prepareStatement(sql, keyColumnNames);
} else {
pstatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
}
} else {
pstatement = getPreparedStatement(sql);
}
statement = pstatement;
Iterator<? extends List<?>> vi = null;
if (command instanceof BulkCommand) {
BulkCommand batchCommand = (BulkCommand) command;
vi = batchCommand.getParameterValues();
}
int k = 0;
int batchStart = 0;
if (vi != null) {
try {
commitType = getAutoCommit(translatedComm);
if (commitType) {
connection.setAutoCommit(false);
}
int maxBatchSize = (command instanceof Insert) ? maxPreparedInsertBatchSize : Integer.MAX_VALUE;
boolean done = false;
outer: while (!done) {
for (int i = 0; i < maxBatchSize; i++) {
if (vi.hasNext()) {
List<?> values = vi.next();
bind(pstatement, translatedComm.getPreparedValues(), values);
k++;
} else {
if (i == 0) {
break outer;
}
done = true;
break;
}
}
int[] results = pstatement.executeBatch();
batchStart = k;
if (result == null) {
result = results;
} else {
int len = result.length;
result = Arrays.copyOf(result, len + results.length);
System.arraycopy(results, 0, result, len, results.length);
}
}
} catch (SQLException e) {
int size = k + 1;
if (result == null) {
result = new int[size];
} else {
result = Arrays.copyOf(result, size);
}
// if there is a BatchUpdateException, there are more update counts to accumulate
if (e instanceof BatchUpdateException) {
BatchUpdateException bue = (BatchUpdateException) e;
int[] batchResults = bue.getUpdateCounts();
for (int j = 0; j < batchResults.length; j++) {
result[batchStart + j] = batchResults[j];
}
size = batchStart + batchResults.length;
} else {
size = batchStart;
}
// resize the result and throw exception
throw new TranslatorBatchException(e, Arrays.copyOf(result, size));
}
} else {
bind(pstatement, translatedComm.getPreparedValues(), null);
updateCount = pstatement.executeUpdate();
result = new int[] { updateCount };
}
addStatementWarnings();
succeeded = true;
}
if (keyColumnDataTypes != null) {
try {
ResultSet keys = statement.getGeneratedKeys();
GeneratedKeys generatedKeys = context.getCommandContext().returnGeneratedKeys(keyColumnNames, keyColumnDataTypes);
// many databases only support returning a single generated value, but we'll still attempt to gather all
outer: while (keys.next()) {
List<Object> vals = new ArrayList<Object>(keyColumnDataTypes.length);
for (int i = 0; i < keyColumnDataTypes.length; i++) {
Object value = this.executionFactory.retrieveValue(keys, i + 1, keyColumnDataTypes[i]);
if (value != null && TypeFacility.getRuntimeType(value.getClass()) != keyColumnDataTypes[i]) {
// TODO we may need to let the engine to the final conversion
LogManager.logDetail(LogConstants.CTX_CONNECTOR, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11023, keyColumnDataTypes[i], keyColumnNames[i], value.getClass()));
continue outer;
}
vals.add(value);
}
generatedKeys.addKey(vals);
}
} catch (SQLException e) {
context.addWarning(e);
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Exception determining generated keys, no keys will be returned");
}
}
} catch (SQLException err) {
throw new JDBCExecutionException(JDBCPlugin.Event.TEIID11013, err, translatedComm);
} finally {
if (commitType) {
restoreAutoCommit(!succeeded, translatedComm);
}
}
}
use of org.teiid.language.Insert 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);
}
}
use of org.teiid.language.Insert in project teiid by teiid.
the class TestJDBCUpdateExecution method testAutoGeneretionKeys.
@Test
public void testAutoGeneretionKeys() throws Exception {
// $NON-NLS-1$
Insert command = (Insert) TranslationHelper.helpTranslate("create foreign table SmallA (IntKey integer primary key, IntNum integer)", "insert into SmallA (IntKey, IntNum) values (1, 2)");
Connection connection = Mockito.mock(Connection.class);
Statement s = Mockito.mock(Statement.class);
Mockito.stub(connection.createStatement()).toReturn(s);
JDBCExecutionFactory config = new JDBCExecutionFactory() {
@Override
public boolean supportsGeneratedKeys() {
return true;
}
@Override
public boolean useColumnNamesForGeneratedKeys() {
return true;
}
};
ResultSet r = Mockito.mock(ResultSet.class);
ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
Mockito.stub(r.getMetaData()).toReturn(rs);
Mockito.stub(s.getGeneratedKeys()).toReturn(r);
FakeExecutionContextImpl context = new FakeExecutionContextImpl();
((org.teiid.query.util.CommandContext) context.getCommandContext()).setReturnAutoGeneratedKeys(Collections.EMPTY_LIST);
JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, context, config);
updateExecution.execute();
Mockito.verify(s, Mockito.times(1)).getGeneratedKeys();
Mockito.verify(s, Mockito.times(1)).executeUpdate("INSERT INTO SmallA (IntKey, IntNum) VALUES (1, 2)", new String[] { "IntKey" });
config = new JDBCExecutionFactory() {
@Override
public boolean supportsGeneratedKeys() {
return true;
}
};
s = Mockito.mock(Statement.class);
Mockito.stub(connection.createStatement()).toReturn(s);
Mockito.stub(s.getGeneratedKeys()).toReturn(r);
updateExecution = new JDBCUpdateExecution(command, connection, context, config);
updateExecution.execute();
Mockito.verify(s, Mockito.times(1)).getGeneratedKeys();
Mockito.verify(s, Mockito.times(1)).executeUpdate("INSERT INTO SmallA (IntKey, IntNum) VALUES (1, 2)", Statement.RETURN_GENERATED_KEYS);
}
Aggregations