use of org.teiid.language.Parameter 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.Parameter in project teiid by teiid.
the class InsertExecutionImpl method buildBulkRowPayload.
protected List<com.sforce.async.SObject> buildBulkRowPayload(Insert insert, Iterator<? extends List<?>> it, int rowCount) throws TranslatorException {
List<com.sforce.async.SObject> rows = new ArrayList<com.sforce.async.SObject>();
List<ColumnReference> columns = insert.getColumns();
int boundCount = 0;
List<Expression> literalValues = ((ExpressionValueSource) insert.getValueSource()).getValues();
while (it.hasNext()) {
if (boundCount >= rowCount) {
break;
}
boundCount++;
List<?> values = it.next();
com.sforce.async.SObject sobj = new com.sforce.async.SObject();
for (int i = 0; i < columns.size(); i++) {
Expression ex = literalValues.get(i);
ColumnReference element = columns.get(i);
Column column = element.getMetadataObject();
Class<?> type = ex.getType();
Object value = null;
if (ex instanceof Parameter) {
value = values.get(((Parameter) ex).getValueIndex());
} else if (!(ex instanceof Literal)) {
throw new TranslatorException(SalesForcePlugin.Util.gs(SalesForcePlugin.Event.TEIID13007));
} else {
value = ((Literal) ex).getValue();
}
sobj.setField(column.getSourceName(), getStringValue(value, type));
}
rows.add(sobj);
}
return rows;
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class JDBCBaseExecution method bind.
/**
* Bind the values in the TranslatedCommand to the PreparedStatement
*/
protected void bind(PreparedStatement stmt, List<?> params, List<?> batchValues) throws SQLException {
for (int i = 0; i < params.size(); i++) {
Object paramValue = params.get(i);
Object value = null;
Class<?> paramType = null;
if (paramValue instanceof Literal) {
Literal litParam = (Literal) paramValue;
value = litParam.getValue();
paramType = litParam.getType();
} else if (paramValue instanceof Argument) {
Argument arg = (Argument) paramValue;
value = ((Literal) arg.getExpression()).getValue();
paramType = arg.getType();
} else {
Parameter param = (Parameter) paramValue;
if (batchValues == null) {
// $NON-NLS-1$
throw new AssertionError("Expected batchValues when using a Parameter");
}
value = batchValues.get(param.getValueIndex());
paramType = param.getType();
}
this.executionFactory.bindValue(stmt, value, paramType, i + 1);
}
if (batchValues != null) {
stmt.addBatch();
}
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestDependentJoins method helpTestPushdown.
private void helpTestPushdown(boolean supportsArrayType) {
// Create query
// $NON-NLS-1$
String sql = "SELECT pm1.g1.e1 FROM /*+ MAKEIND */ pm1.g1, pm2.g1 WHERE pm1.g1.e1 = pm2.g1.e1 AND pm1.g1.e2=pm2.g1.e2 order by pm1.g1.e1";
// Create expected results
List[] expected = new List[] { // $NON-NLS-1$
Arrays.asList(new Object[] { "a" }) };
// Construct data manager with data
HardcodedDataManager dataManager = new HardcodedDataManager(RealMetadataFactory.example1Cached());
dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1), Arrays.asList("b", 2) });
if (supportsArrayType) {
dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 WHERE (g_0.e1, g_0.e2) = (?, ?) ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1) });
} else {
dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 WHERE g_0.e1 = ? AND g_0.e2 = ? ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1) });
}
BasicSourceCapabilities bsc = TestOptimizer.getTypicalCapabilities();
bsc.setCapabilitySupport(Capability.DEPENDENT_JOIN, true);
bsc.setCapabilitySupport(Capability.ARRAY_TYPE, supportsArrayType);
bsc.setSourceProperty(Capability.MAX_DEPENDENT_PREDICATES, 1);
bsc.setSourceProperty(Capability.MAX_IN_CRITERIA_SIZE, 1);
DefaultCapabilitiesFinder dcf = new DefaultCapabilitiesFinder(bsc);
// Plan query
ProcessorPlan plan = TestProcessor.helpGetPlan(sql, RealMetadataFactory.example1Cached(), dcf);
TestOptimizer.checkDependentJoinCount(plan, 1);
// Run query
TestProcessor.helpProcess(plan, dataManager, expected);
Select s = (Select) dataManager.getPushdownCommands().get(1);
assertEquals(1, s.getDependentValues().size());
List<? extends List<?>> vals = s.getDependentValues().values().iterator().next();
assertEquals(2, vals.size());
if (supportsArrayType) {
Comparison comp = (Comparison) s.getWhere();
Parameter p = (Parameter) ((Array) comp.getRightExpression()).getExpressions().get(0);
assertEquals(0, p.getValueIndex());
assertNotNull(s.getDependentValues().get(p.getDependentValueId()));
}
}
use of org.teiid.language.Parameter in project teiid by teiid.
the class TestOracleTranslator method testDependentJoin.
@Test
public void testDependentJoin() throws Exception {
CommandBuilder commandBuilder = new CommandBuilder(getOracleSpecificMetadata());
Select command = (Select) commandBuilder.getCommand("select id from smalla where description = 'a'");
Parameter param = new Parameter();
param.setType(TypeFacility.RUNTIME_TYPES.STRING);
param.setDependentValueId("x");
param.setValueIndex(0);
Map<String, List<? extends List<?>>> dependentValues = new HashMap<String, List<? extends List<?>>>();
dependentValues.put("x", Arrays.asList(Arrays.asList("a"), Arrays.asList("b")));
command.setDependentValues(dependentValues);
((Comparison) command.getWhere()).setRightExpression(param);
Connection connection = Mockito.mock(Connection.class);
Statement statement = Mockito.mock(Statement.class);
Mockito.stub(connection.createStatement()).toReturn(statement);
PreparedStatement ps = Mockito.mock(PreparedStatement.class);
Mockito.stub(ps.executeBatch()).toReturn(new int[] { -2, -2 });
// $NON-NLS-1$
Mockito.stub(connection.prepareStatement("INSERT INTO TEIID_DKJ1 (COL1) VALUES (?)")).toReturn(ps);
// we won't bother to retrieve the results, but we expect the following join query
PreparedStatement ps1 = Mockito.mock(PreparedStatement.class);
// $NON-NLS-1$
Mockito.stub(connection.prepareStatement("SELECT SmallishA.ID FROM TEIID_DKJ1, SmallishA WHERE SmallishA.description = TEIID_DKJ1.COL1")).toReturn(ps1);
OracleExecutionFactory ef = new OracleExecutionFactory() {
public String getTemporaryTableName(String prefix) {
// don't use random for testing
return prefix;
}
};
ef.setDatabaseVersion(Version.DEFAULT_VERSION);
ef.start();
JDBCQueryExecution e = new JDBCQueryExecution(command, connection, new FakeExecutionContextImpl(), ef);
e.execute();
Mockito.verify(statement, Mockito.times(1)).execute("DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN EXECUTE IMMEDIATE 'create global temporary table TEIID_DKJ1 (COL1 varchar2(100 char)) on commit delete rows; END;");
Mockito.verify(ps, Mockito.times(1)).setObject(1, "a", Types.VARCHAR);
Mockito.verify(ps, Mockito.times(1)).setObject(1, "b", Types.VARCHAR);
Mockito.verify(ps, Mockito.times(2)).addBatch();
Mockito.verify(ps, Mockito.times(1)).executeBatch();
}
Aggregations