use of org.seasar.doma.jdbc.SqlNode in project doma by domaframework.
the class SqlParserTest method testFor_array.
@Test
public void testFor_array() {
ExpressionEvaluator evaluator = new ExpressionEvaluator();
String[] array = new String[] { "aaa", "bbb", "ccc" };
evaluator.add("names", new Value(String[].class, array));
String testSql = "select * from aaa where /*%for n : names*/name = /*n*/'a' /*%if n_has_next */or /*%end*//*%end*/";
SqlParser parser = new SqlParser(testSql);
SqlNode sqlNode = parser.parse();
PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
assertEquals("select * from aaa where name = ? or name = ? or name = ?", sql.getRawSql());
assertEquals("select * from aaa where name = 'aaa' or name = 'bbb' or name = 'ccc'", sql.getFormattedSql());
assertEquals(3, sql.getParameters().size());
assertEquals("aaa", sql.getParameters().get(0).getWrapper().get());
assertEquals("bbb", sql.getParameters().get(1).getWrapper().get());
assertEquals("ccc", sql.getParameters().get(2).getWrapper().get());
}
use of org.seasar.doma.jdbc.SqlNode in project doma by domaframework.
the class SqlParserTest method testElseBlock.
@Test
public void testElseBlock() {
ExpressionEvaluator evaluator = new ExpressionEvaluator();
evaluator.add("name", new Value(String.class, "hoge"));
String testSql = "select * from aaa where /*%if name == null*/bbb is null\n/*%elseif name == \"\"*/\n/*%else*/ bbb = /*name*/'ccc'/*%end*/";
SqlParser parser = new SqlParser(testSql);
SqlNode sqlNode = parser.parse();
PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
assertEquals("select * from aaa where bbb = ?", sql.getRawSql());
assertEquals("select * from aaa where bbb = 'hoge'", sql.getFormattedSql());
assertEquals(1, sql.getParameters().size());
assertEquals("hoge", sql.getParameters().get(0).getWrapper().get());
}
use of org.seasar.doma.jdbc.SqlNode in project doma by domaframework.
the class SqlParserTest method testBindVariable_stringLiteral.
@Test
public void testBindVariable_stringLiteral() {
String testSql = "select * from aaa where ename = /*name*/'bbb'";
SqlParser parser = new SqlParser(testSql);
SqlNode node = parser.parse();
assertNotNull(node);
}
use of org.seasar.doma.jdbc.SqlNode in project doma by domaframework.
the class BatchSqlValidatorTest method testEmbeddedVariable.
@Test
void testEmbeddedVariable() throws Exception {
Class<?> target = BatchSqlValidationDao.class;
addCompilationUnit(target);
addProcessor(new TestProcessor() {
@Override
protected void run() {
ExecutableElement methodElement = createMethodElement(target, "testEmbeddedVariable", String.class);
LinkedHashMap<String, TypeMirror> parameterTypeMap = createParameterTypeMap(methodElement);
BatchSqlValidator validator = new BatchSqlValidator(ctx, methodElement, parameterTypeMap, "aaa/bbbDao/ccc.sql", false, false);
SqlParser parser = new SqlParser("select * from emp /*# orderBy */");
SqlNode sqlNode = parser.parse();
sqlNode.accept(validator, null);
}
});
compile();
assertTrue(getCompiledResult());
assertMessage(Message.DOMA4181);
}
use of org.seasar.doma.jdbc.SqlNode in project doma by domaframework.
the class BatchSqlValidatorTest method testIfAndEmbeddedVariable.
@Test
void testIfAndEmbeddedVariable() throws Exception {
Class<?> target = BatchSqlValidationDao.class;
addCompilationUnit(target);
addProcessor(new TestProcessor() {
@Override
protected void run() {
ExecutableElement methodElement = createMethodElement(target, "testIfAndEmbeddedVariable", String.class);
LinkedHashMap<String, TypeMirror> parameterTypeMap = createParameterTypeMap(methodElement);
BatchSqlValidator validator = new BatchSqlValidator(ctx, methodElement, parameterTypeMap, "aaa/bbbDao/ccc.sql", false, false);
SqlParser parser = new SqlParser("select * from emp where /*%if true*/ id = 1 /*%end */ /*# orderBy */");
SqlNode sqlNode = parser.parse();
sqlNode.accept(validator, null);
}
});
compile();
assertTrue(getCompiledResult());
assertEquals(2, getDiagnostics().size());
}
Aggregations