use of org.teiid.language.Expression in project teiid by teiid.
the class TeradataSQLConversionVisitor method visit.
@Override
public void visit(In obj) {
List<Expression> exprs = obj.getRightExpressions();
boolean decompose = false;
for (Expression expr : exprs) {
if (!(expr instanceof Literal)) {
decompose = true;
break;
}
}
if (decompose) {
List<Expression> literals = new ArrayList<Expression>();
Comparison.Operator opCode = obj.isNegated() ? Comparison.Operator.NE : Comparison.Operator.EQ;
if (exprs.size() > 1) {
Condition left = null;
for (Expression expr : obj.getRightExpressions()) {
if (expr instanceof Literal) {
literals.add(expr);
} else {
if (left == null) {
left = LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), expr);
} else {
left = LanguageFactory.INSTANCE.createAndOr(obj.isNegated() ? Operator.AND : Operator.OR, left, LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), expr));
}
}
}
if (!literals.isEmpty()) {
left = LanguageFactory.INSTANCE.createAndOr(obj.isNegated() ? Operator.AND : Operator.OR, left, new In(obj.getLeftExpression(), literals, obj.isNegated()));
}
buffer.append(Tokens.LPAREN);
super.visit((AndOr) left);
buffer.append(Tokens.RPAREN);
} else {
super.visit(LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), exprs.get(0)));
}
} else {
super.visit(obj);
}
}
use of org.teiid.language.Expression 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.Expression 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();
}
use of org.teiid.language.Expression in project teiid by teiid.
the class TestModFunctionModifier method testOneBigDecElemOneBigDecConst.
/**
* Test {@link ModFunctionModifier#modify(Function)} to validate a call to
* MOD(e1,y) using a {@link BigDecimal} element and a {@link BigDecimal}
* constant for parameters returns (e1 - (TRUNC((e1 / y), 0) * y)).
* {@link ModFunctionModifier} will be constructed without specifying a
* function name or a supported type list.
*
* @throws Exception
*/
public void testOneBigDecElemOneBigDecConst() throws Exception {
Expression[] args = new Expression[] { // $NON-NLS-1$
LANG_FACTORY.createColumnReference("e1", null, null, BigDecimal.class), LANG_FACTORY.createLiteral(new BigDecimal(6), BigDecimal.class) };
// default / default
// $NON-NLS-1$
helpTestMod(args, "(e1 - (sign(e1) * floor(abs((e1 / 6))) * abs(6)))");
}
use of org.teiid.language.Expression in project teiid by teiid.
the class TestModFunctionModifier method testTwoIntConst2.
/**
* Test {@link ModFunctionModifier#modify(Function)} to validate a call to
* MOD(x,y) using {@link Integer} constants for both parameters returns
* MOD(x,y). {@link ModFunctionModifier} will be constructed with a
* function name of "MOD" but without a supported type list.
*
* @throws Exception
*/
public void testTwoIntConst2() throws Exception {
Expression[] args = new Expression[] { LANG_FACTORY.createLiteral(new Integer(10), Integer.class), LANG_FACTORY.createLiteral(new Integer(6), Integer.class) };
// mod / default
// $NON-NLS-1$ //$NON-NLS-2$
helpTestMod("MOD", args, "MOD(10, 6)");
}
Aggregations