Search in sources :

Example 6 with SqlParser

use of org.seasar.doma.internal.jdbc.sql.SqlParser in project doma by domaframework.

the class StandardPagingTransformerTest method testOffsetLimit_forNode.

@Test
public void testOffsetLimit_forNode() {
    String expected = "select * from ( select temp_.*, row_number() over( order by temp_.name1, temp_.name2, temp_.id ) as doma_rownumber_ from ( select emp.id from emp ) as temp_ ) as temp2_ where doma_rownumber_ > 5 and doma_rownumber_ <= 15";
    StandardPagingTransformer transformer = new StandardPagingTransformer(5, 10);
    SqlParser parser = new SqlParser("select emp.id from emp order by /*%for e: values*/emp.name/*#e*/, /*%end*/emp.id");
    SqlNode sqlNode = transformer.transform(parser.parse());
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("values", new Value(List.class, Arrays.asList(1, 2)));
    NodePreparedSqlBuilder sqlBuilder = new NodePreparedSqlBuilder(new MockConfig(), SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED);
    PreparedSql sql = sqlBuilder.build(sqlNode, Function.identity());
    assertEquals(expected, sql.getRawSql());
}
Also used : SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) Value(org.seasar.doma.internal.expr.Value) List(java.util.List) NodePreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) Test(org.junit.jupiter.api.Test)

Example 7 with SqlParser

use of org.seasar.doma.internal.jdbc.sql.SqlParser in project doma by domaframework.

the class StandardPagingTransformerTest method testOrderByClauseUnspecified.

@Test
public void testOrderByClauseUnspecified() {
    StandardPagingTransformer transformer = new StandardPagingTransformer(5, 10);
    SqlParser parser = new SqlParser("select * from emp");
    try {
        transformer.transform(parser.parse());
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(Message.DOMA2201, expected.getMessageResource());
    }
}
Also used : SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) Test(org.junit.jupiter.api.Test)

Example 8 with SqlParser

use of org.seasar.doma.internal.jdbc.sql.SqlParser in project doma by domaframework.

the class StandardPagingTransformerTest method testOffsetLimit.

@Test
public void testOffsetLimit() {
    String expected = "select * from ( select temp_.*, row_number() over( order by temp_.id ) as doma_rownumber_ from ( select emp.id from emp ) as temp_ ) as temp2_ where doma_rownumber_ > 5 and doma_rownumber_ <= 15";
    StandardPagingTransformer transformer = new StandardPagingTransformer(5, 10);
    SqlParser parser = new SqlParser("select emp.id from emp order by emp.id");
    SqlNode sqlNode = transformer.transform(parser.parse());
    NodePreparedSqlBuilder sqlBuilder = new NodePreparedSqlBuilder(new MockConfig(), SqlKind.SELECT, "dummyPath");
    PreparedSql sql = sqlBuilder.build(sqlNode, Function.identity());
    assertEquals(expected, sql.getRawSql());
}
Also used : SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) NodePreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Test(org.junit.jupiter.api.Test)

Example 9 with SqlParser

use of org.seasar.doma.internal.jdbc.sql.SqlParser 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 10 with SqlParser

use of org.seasar.doma.internal.jdbc.sql.SqlParser 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

SqlParser (org.seasar.doma.internal.jdbc.sql.SqlParser)107 Test (org.junit.jupiter.api.Test)105 SqlNode (org.seasar.doma.jdbc.SqlNode)90 NodePreparedSqlBuilder (org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder)67 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)64 PreparedSql (org.seasar.doma.jdbc.PreparedSql)59 LinkedHashMap (java.util.LinkedHashMap)25 ExecutableElement (javax.lang.model.element.ExecutableElement)25 TestProcessor (org.seasar.doma.internal.apt.TestProcessor)25 List (java.util.List)7 AptException (org.seasar.doma.internal.apt.AptException)6 JdbcException (org.seasar.doma.jdbc.JdbcException)6 SelectOptions (org.seasar.doma.jdbc.SelectOptions)6 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)3 Value (org.seasar.doma.internal.expr.Value)3 Iterator (java.util.Iterator)1