Search in sources :

Example 1 with JQLContext

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

the class TestJqlChecker method testOK.

/**
 * <p>
 * OK
 * </p>
 *
 * @throws Throwable
 */
@Test
public void testOK() throws Throwable {
    String sql = "SELECT id, action, number, countryCode, contactName, contactId FROM phone_number WHERE number = ${bean.number} and number like ${bean.number} || '%' and #{" + JQLDynamicStatementType.DYNAMIC_WHERE + "}";
    log(sql);
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker jsqChecker = JQLChecker.getInstance();
    jsqChecker.analyze(dummyContext, jql, new JqlBaseListener() {

        @Override
        public void enterBind_parameter(Bind_parameterContext ctx) {
            TestJqlChecker.this.log("xx parameter name %s", ctx.bind_parameter_name().getText());
        }

        @Override
        public void enterBind_dynamic_sql(Bind_dynamic_sqlContext ctx) {
            TestJqlChecker.this.log("xx dynamic %s", ctx.bind_parameter_name().getText());
        }

        @Override
        public void enterColumn_name(Column_nameContext ctx) {
            super.enterColumn_name(ctx);
            log("column " + ctx.getText());
        }
    });
    jsqChecker.extractPlaceHoldersAsList(new JQLContext() {

        @Override
        public String getContextDescription() {
            return "test context";
        }
    }, jql.value);
    log("replaced " + jsqChecker.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;
        }
    }));
    log("aa");
}
Also used : 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) Bind_dynamic_sqlContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_dynamic_sqlContext) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Column_nameContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_nameContext) Bind_parameterContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 2 with JQLContext

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

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

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

Example 5 with JQLContext

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

the class BindTableGenerator method buldIndexes.

public static Pair<String, String> buldIndexes(final SQLiteEntity entity, boolean unique, int counter) {
    Pair<String, String> result = new Pair<>();
    result.value0 = "";
    result.value1 = "";
    ModelAnnotation annotationTable = entity.getAnnotation(BindTable.class);
    if (annotationTable == null)
        return result;
    List<String> indexes = null;
    String uniqueString;
    if (unique) {
        uniqueString = "UNIQUE ";
        indexes = annotationTable.getAttributeAsArray(AnnotationAttributeType.UNIQUE_INDEXES);
    } else {
        uniqueString = "";
        indexes = annotationTable.getAttributeAsArray(AnnotationAttributeType.INDEXES);
    }
    if (indexes == null || indexes.size() == 0)
        return result;
    // CREATE INDEX index_name ON tab_name (column1, column2)
    // Matcher matcher = patternIndex.matcher(rawIndexes);
    List<String> listCreateIndex = new ArrayList<>();
    List<String> listDropIndex = new ArrayList<>();
    for (String index : indexes) {
        String createIndex = String.format(" CREATE %sINDEX idx_%s_%s on %s (%s)", uniqueString, entity.getTableName(), counter++, entity.getTableName(), index);
        String dropIndex = String.format(" DROP INDEX IF EXISTS idx_%s_%s", entity.getTableName(), counter);
        final One<Integer> fieldCounter = new One<Integer>(0);
        createIndex = JQLChecker.getInstance().replace(new JQLContext() {

            @Override
            public String getContextDescription() {
                return "While table definition generation for entity " + entity.getName();
            }
        }, createIndex, new JQLReplacerListenerImpl(null) {

            @Override
            public String onColumnName(String columnName) {
                fieldCounter.value0++;
                SQLProperty property = entity.findPropertyByName(columnName);
                AssertKripton.assertTrue(property != null, "class '%s' in @%s(indexes) use unknown property '%s'", entity.getName(), BindTable.class.getSimpleName(), columnName);
                return property.columnName;
            }

            @Override
            public String onColumnFullyQualifiedName(String tableName, String columnName) {
                AssertKripton.fail("Inconsistent state");
                return null;
            }
        });
        AssertKripton.assertTrue(fieldCounter.value0 > 0, "class '%s' have @%s(indexes) with no well formed indexes", entity.getName(), BindTable.class.getSimpleName());
        listCreateIndex.add(createIndex);
        listDropIndex.add(dropIndex);
    }
    result.value0 = StringUtils.join(listCreateIndex, ";");
    result.value1 = StringUtils.join(listDropIndex, ";");
    return result;
}
Also used : JQLContext(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) BindTable(com.abubusoft.kripton.android.annotation.BindTable) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) Pair(com.abubusoft.kripton.common.Pair)

Aggregations

JQLContext (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext)5 BaseProcessorTest (base.BaseProcessorTest)4 JQL (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL)4 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 JQLPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder)3 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)3 List (java.util.List)3 One (com.abubusoft.kripton.common.One)2 JQLDynamicStatementType (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType)2 BindTable (com.abubusoft.kripton.android.annotation.BindTable)1 Pair (com.abubusoft.kripton.common.Pair)1 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)1 JQLReplaceVariableStatementListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl)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 Bind_parameterContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)1 Column_nameContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_nameContext)1 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)1