Search in sources :

Example 1 with JQLPlaceHolder

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

the class TestJqlChecker method testUpdate01.

@Test
public void testUpdate01() {
    String sql = "UPDATE channel SET uid=${ bean.field1}, owner_uid=${bean.field2}, update_time=${bean.field3}, name=${field4} WHERE id=${bean.field1}";
    String sqlForLog = "UPDATE channel SET uid=?, owner_uid=?, update_time=?, name=? WHERE id=?";
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker checker = JQLChecker.getInstance();
    // verify sql
    checker.verify(dummyContext, jql);
    // check bind parameters
    {
        List<JQLPlaceHolder> aspected = new ArrayList<>();
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field2"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field3"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field4"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
        List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
        checkCollectionExactly(actual, aspected);
    }
    // prepare for log
    String sqlLogResult = checker.replace(dummyContext, jql, new JQLReplacerListenerImpl(null) {

        @Override
        public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
            return String.format("\"+%s+\"", dynamicStatement);
        }

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

        @Override
        public String onColumnFullyQualifiedName(String tableName, String columnName) {
            // TODO Auto-generated method stub
            return null;
        }
    });
    assertEquals("sql for log generation failed", sqlForLog, sqlLogResult);
}
Also used : 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) ArrayList(java.util.ArrayList) List(java.util.List) JQLPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 2 with JQLPlaceHolder

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

the class TestJqlChecker method testDelet1.

@Test
public void testDelet1() {
    String sql = "DELETE FROM channel WHERE ownerUid=${field1} and ownerUid=${bean.field2} and ownerUid=${bean.field3} and ownerUid=${field5}";
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker checker = JQLChecker.getInstance();
    // verify sql
    checker.verify(new JQLContext() {

        @Override
        public String getContextDescription() {
            return "test context";
        }
    }, jql);
    // check bind parameters
    {
        List<JQLPlaceHolder> aspected = new ArrayList<>();
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field1"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field2"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field3"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field5"));
        List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
        checkCollectionExactly(actual, aspected);
    }
}
Also used : JQLContext(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) ArrayList(java.util.ArrayList) List(java.util.List) JQLPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 3 with JQLPlaceHolder

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

the class TestJqlChecker method testSelect01.

/**
 * extract projections from a select
 */
@Test
public void testSelect01() {
    // String sql="SELECT count(*) FROM channel WHERE
    // updateTime=${bean.updateTime}";
    String sql = "SELECT count(*) as alias1, field2, field3 as alias3, table1.field3 as alias3, table2.field4 as alias4 FROM channel WHERE updateTime=${ bean.field1 } and field=${ field2  } and #{" + JQLDynamicStatementType.DYNAMIC_WHERE + "}";
    String logSql = "SELECT count(*) as alias1, field2, field3 as alias3, table1.field3 as alias3, table2.field4 as alias4 FROM channel WHERE updateTime=? and field=? and \"+DYNAMIC_WHERE+\"";
    // String usedSql = "SELECT count(*) FROM channel WHERE
    // updateTime=${bean.updateTime}";
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker checker = JQLChecker.getInstance();
    // verify sql
    checker.verify(dummyContext, jql);
    Finder<SQLProperty> entityMock = new Finder<SQLProperty>() {

        @Override
        public String getSimpleName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public List<SQLProperty> getCollection() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SQLProperty findPropertyByName(String name) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getTableName() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    // check projections
    Set<JQLProjection> projections = checker.extractProjections(dummyContext, jql.value, entityMock);
    {
        LinkedHashSet<JQLProjection> aspected = new LinkedHashSet<>();
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COMPLEX).expression("count(*)").alias("alias1").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).column("field2").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).column("field3").alias("alias3").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).table("table1").column("field3").alias("alias3").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).table("table2").column("field4").alias("alias4").build());
        checkCollectionExactly(projections, aspected);
    }
    // check bind parameters
    {
        List<JQLPlaceHolder> aspected = new ArrayList<>();
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field2"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.DYNAMIC_SQL, JQLDynamicStatementType.DYNAMIC_WHERE.toString()));
        List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
        checkCollectionExactly(actual, aspected);
    }
    // prepare for log
    String sqlLogResult = checker.replace(dummyContext, jql, new JQLReplacerListenerImpl(null) {

        @Override
        public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
            return String.format("\"+%s+\"", dynamicStatement);
        }

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

        @Override
        public String onColumnFullyQualifiedName(String tableName, String columnName) {
            return null;
        }
    });
    assertEquals("sql for log generation failed", logSql, sqlLogResult);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) Finder(com.abubusoft.kripton.processor.core.Finder) JQLPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) JQLDynamicStatementType(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) ArrayList(java.util.ArrayList) List(java.util.List) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 4 with JQLPlaceHolder

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

the class TestJqlChecker method testInsert01.

@Test
public void testInsert01() {
    String sql = "INSERT INTO channel (uid, owner_uid, update_time, name, field) VALUES (${bean.field1}, ${bean.field2}, ${bean.field3}, ${bean.field4}, ${field5})";
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker checker = JQLChecker.getInstance();
    // verify sql
    checker.verify(new JQLContext() {

        @Override
        public String getContextDescription() {
            return "test context";
        }
    }, jql);
    // check bind parameters
    {
        List<JQLPlaceHolder> aspected = new ArrayList<>();
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field2"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field3"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field4"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field5"));
        List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
        checkCollectionExactly(actual, aspected);
    }
}
Also used : JQLContext(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) ArrayList(java.util.ArrayList) List(java.util.List) JQLPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 5 with JQLPlaceHolder

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder 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

JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)7 JQLPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder)7 BaseProcessorTest (base.BaseProcessorTest)5 JQL (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Test (org.junit.Test)5 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)4 JQLDynamicStatementType (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType)3 JQLContext (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext)3 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)3 LinkedHashSet (java.util.LinkedHashSet)3 One (com.abubusoft.kripton.common.One)2 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)2 JQLReplaceVariableStatementListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl)2 ContentUriPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder)2 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)2 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 Finder (com.abubusoft.kripton.processor.core.Finder)1