Search in sources :

Example 26 with SqlNode

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());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 27 with SqlNode

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());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 28 with SqlNode

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);
}
Also used : SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 29 with SqlNode

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);
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) TestProcessor(org.seasar.doma.internal.apt.TestProcessor) LinkedHashMap(java.util.LinkedHashMap) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 30 with SqlNode

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());
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) TestProcessor(org.seasar.doma.internal.apt.TestProcessor) LinkedHashMap(java.util.LinkedHashMap) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Aggregations

SqlNode (org.seasar.doma.jdbc.SqlNode)176 Test (org.junit.jupiter.api.Test)145 PreparedSql (org.seasar.doma.jdbc.PreparedSql)102 SqlParser (org.seasar.doma.internal.jdbc.sql.SqlParser)90 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)62 NodePreparedSqlBuilder (org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder)62 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)48 Value (org.seasar.doma.internal.expr.Value)44 LinkedHashMap (java.util.LinkedHashMap)25 ExecutableElement (javax.lang.model.element.ExecutableElement)25 TestProcessor (org.seasar.doma.internal.apt.TestProcessor)25 JdbcException (org.seasar.doma.jdbc.JdbcException)20 List (java.util.List)17 BigDecimal (java.math.BigDecimal)13 ArrayList (java.util.ArrayList)12 FragmentNode (org.seasar.doma.internal.jdbc.sql.node.FragmentNode)11 SelectStatementNode (org.seasar.doma.internal.jdbc.sql.node.SelectStatementNode)11 AptException (org.seasar.doma.internal.apt.AptException)8 OrderByClauseNode (org.seasar.doma.internal.jdbc.sql.node.OrderByClauseNode)8 WordNode (org.seasar.doma.internal.jdbc.sql.node.WordNode)6