Search in sources :

Example 6 with JQLDynamicStatementType

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType in project kripton by xcesco.

the class TestJqlChecker method testBindParametersFromVariableStatements.

@Test
public void testBindParametersFromVariableStatements() {
    final One<String> where = new One<>();
    final One<String> orderBy = new One<>();
    final One<String> limit = new One<>();
    final One<String> offset = new One<>();
    final One<String> group = new One<>();
    final One<String> having = new One<>();
    String sql = "SELECT id, parentId, birthCity, birthDay, value, name, surname FROM Person WHERE name like ${nameTemp} || '%' GROUP BY id HAVING id=2 ORDER BY id,  #{" + JQLDynamicStatementType.DYNAMIC_ORDER_BY + "} LIMIT #{" + JQLDynamicStatementType.DYNAMIC_PAGE_SIZE + "} OFFSET #{" + JQLDynamicStatementType.DYNAMIC_PAGE_OFFSET + "}";
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker checker = JQLChecker.getInstance();
    // verify sql
    checker.verify(dummyContext, jql);
    String finalSql = checker.replaceVariableStatements(dummyContext, jql.value, new JQLReplaceVariableStatementListenerImpl() {

        @Override
        public String onWhere(String whereStatement) {
            where.value0 = whereStatement;
            return "";
        }

        @Override
        public String onOrderBy(String orderByStatement) {
            orderBy.value0 = orderByStatement;
            return "";
        }

        @Override
        public String onLimit(String statement) {
            limit.value0 = statement;
            return "";
        }

        @Override
        public String onOffset(String statement) {
            offset.value0 = statement;
            return "";
        }

        @Override
        public String onGroup(String statement) {
            group.value0 = statement;
            return "";
        }

        @Override
        public String onHaving(String statement) {
            having.value0 = statement;
            return "";
        }

        @Override
        public String onProjectedColumns(String statement) {
            return null;
        }
    });
    log(where.value0);
    log(orderBy.value0);
    log(group.value0);
    log(having.value0);
    log(offset.value0);
    log(limit.value0);
    log(finalSql);
    // assertEquals(where, " id = ${dummy} and a=${dummy2}");
    {
        List<JQLPlaceHolder> list = checker.extractFromVariableStatement(new JQLContext() {

            @Override
            public String getContextDescription() {
                return "Test context";
            }
        }, where.value0);
        for (JQLPlaceHolder item : list) {
            log(item.value);
        }
        String replacedSql = checker.replaceFromVariableStatement(new JQLContext() {

            @Override
            public String getContextDescription() {
                return "Test context";
            }
        }, where.value0, new JQLReplacerListenerImpl(null) {

            @Override
            public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
                return "*";
            }

            @Override
            public String onBindParameter(String bindParameterName) {
                return "?";
            }

            @Override
            public String onColumnFullyQualifiedName(String tableName, String columnName) {
                // TODO Auto-generated method stub
                return null;
            }
        });
        log(replacedSql);
    }
}
Also used : JQLReplaceVariableStatementListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl) JQLContext(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) JQLDynamicStatementType(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType) JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) List(java.util.List) JQLPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Aggregations

JQLDynamicStatementType (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType)6 JQL (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL)5 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)5 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)5 BaseProcessorTest (base.BaseProcessorTest)4 Test (org.junit.Test)4 JQLPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 One (com.abubusoft.kripton.common.One)2 JQLContext (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext)2 JQLReplaceVariableStatementListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl)2 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)2 BindSqlDelete (com.abubusoft.kripton.android.annotation.BindSqlDelete)1 BindSqlInsert (com.abubusoft.kripton.android.annotation.BindSqlInsert)1 StringUtils (com.abubusoft.kripton.common.StringUtils)1 Finder (com.abubusoft.kripton.processor.core.Finder)1 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)1 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)1 Bind_dynamic_sqlContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_dynamic_sqlContext)1