Search in sources :

Example 6 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class SqlBuilderHelper method generateWhereCondition.

/**
 * <p>
 * Generate where management code.
 * </p>
 *
 * <h2>pre condition</h2>
 * <dl>
 * <dt>_sqlDynamicWhere</dt>
 * <dd>dynamic where</dd>
 * </dl>
 *
 * <h2>post conditions</h2>
 * <dl>
 * <dt>_sqlWhereParams</dt>
 * <dd>ArraList</dd>
 * <dt>_sqlWhereStatement</dt>
 * <dd>String</dd>
 * </dl>
 *
 * @param methodBuilder
 * @param method
 * @param jql
 * @param jqlChecker
 * @param sqlWhereStatement
 */
public static void generateWhereCondition(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method, boolean sqlWhereParamsAlreadyDefined) {
    final JQL jql = method.jql;
    final JQLChecker jqlChecker = JQLChecker.getInstance();
    // we need always this
    if (!sqlWhereParamsAlreadyDefined) {
    // methodBuilder.addStatement("$T<String>
    // _sqlWhereParams=getWhereParamsArray()", ArrayList.class);
    }
    if (jql.isWhereConditions()) {
        // parameters extracted from query
        final One<String> whereStatement = new One<>();
        final One<Boolean> alreadyFoundWhereStatement = new One<>(false);
        // put in whereStatement value of where statement.
        jqlChecker.replaceVariableStatements(method, method.jql.value, new JQLReplaceVariableStatementListenerImpl() {

            @Override
            public String onWhere(String statement) {
                if (alreadyFoundWhereStatement.value0 == false) {
                    whereStatement.value0 = statement;
                    alreadyFoundWhereStatement.value0 = true;
                    return "";
                } else {
                    // DO NOTHING
                    return null;
                }
            }
        });
        methodBuilder.addCode("\n// manage WHERE arguments -- BEGIN\n");
        String sqlWhere = jqlChecker.replaceFromVariableStatement(method, whereStatement.value0, new JQLReplacerListenerImpl(method) {

            @Override
            public String onColumnName(String columnName) {
                SQLProperty tempProperty = currentEntity.get(columnName);
                AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
                return tempProperty.columnName;
            }

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

            @Override
            public String onBindParameter(String bindParameterName) {
                return "?";
            }
        });
        methodBuilder.addCode("\n// manage WHERE statement\n");
        String value = sqlWhere;
        String valueToReplace = jql.dynamicReplace.get(JQLDynamicStatementType.DYNAMIC_WHERE);
        if (method.jql.operationType == JQLType.SELECT) {
            // we have to include WHERE keywords
            if (jql.isStaticWhereConditions() && !jql.isDynamicWhereConditions()) {
                // case static statement and NO dynamic
                methodBuilder.addStatement("String _sqlWhereStatement=$S", value);
            } else if (jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
                methodBuilder.addStatement("String _sqlWhereStatement=$S+$T.ifNotEmptyAppend($L,\" $L \")", value.replace(valueToReplace, ""), StringUtils.class, "_sqlDynamicWhere", method.dynamicWherePrepend);
            } else if (!jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
                methodBuilder.addStatement("String _sqlWhereStatement=$T.ifNotEmptyAppend($L, \" $L \")", StringUtils.class, "_sqlDynamicWhere", JQLKeywords.WHERE_KEYWORD);
            }
        } else {
            // we DON'T have to include WHERE keywords
            value = value.replace(" " + JQLKeywords.WHERE_KEYWORD, "");
            if (jql.isStaticWhereConditions() && !jql.isDynamicWhereConditions()) {
                // case static statement and NO dynamic
                methodBuilder.addStatement("String _sqlWhereStatement=$S", value);
            } else if (jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
                methodBuilder.addStatement("String _sqlWhereStatement=$S+$T.ifNotEmptyAppend($L,\" $L \")", value.replace(valueToReplace, ""), StringUtils.class, "_sqlDynamicWhere", method.dynamicWherePrepend);
            } else if (!jql.isStaticWhereConditions() && jql.isDynamicWhereConditions()) {
                methodBuilder.addStatement("String _sqlWhereStatement=$T.ifNotEmptyAppend($L, \" \")", StringUtils.class, "_sqlDynamicWhere");
            }
        }
        methodBuilder.addStatement("_sqlBuilder.append($L)", "_sqlWhereStatement");
        methodBuilder.addCode("\n// manage WHERE arguments -- END\n");
    } else {
        // in every situation we need it
        methodBuilder.addStatement("String _sqlWhereStatement=\"\"");
    }
    // manage where arguments
    if (method.hasDynamicWhereConditions() && method.hasDynamicWhereArgs()) {
        // ASSERT: only with dynamic where conditions
        methodBuilder.beginControlFlow("if ($T.hasText(_sqlDynamicWhere) && _sqlDynamicWhereArgs!=null)", StringUtils.class);
        if (method.hasDynamicWhereConditions()) {
            methodBuilder.beginControlFlow("for (String _arg: _sqlDynamicWhereArgs)");
            // methodBuilder.addStatement("_sqlWhereParams.add(_arg)");
            methodBuilder.addStatement("_contentValues.addWhereArgs(_arg)");
            methodBuilder.endControlFlow();
        }
        methodBuilder.endControlFlow();
    }
}
Also used : JQLReplaceVariableStatementListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) One(com.abubusoft.kripton.common.One) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) JQLDynamicStatementType(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType) StringUtils(com.abubusoft.kripton.common.StringUtils) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty)

Example 7 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class SqlBuilderHelper method generateSQLForStaticQuery.

/**
 * <p>
 * Generate log for INSERT operations
 * </p>
 *
 * @param method
 * @param methodBuilder
 */
public static void generateSQLForStaticQuery(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
    methodBuilder.addComment("generate static SQL for statement");
    JQLChecker checker = JQLChecker.getInstance();
    // replace the table name, other pieces will be removed
    String sql = checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onColumnNameToUpdate(String columnName) {
            return onColumnName(columnName);
        }

        @Override
        public String onColumnName(String columnName) {
            SQLProperty tempProperty = method.getParent().getEntity().get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            return "?";
        }
    });
    methodBuilder.addStatement("String _sql=$S", sql);
}
Also used : JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty)

Example 8 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class SqlModifyBuilder method generateLogForModifiers.

/**
 * generate sql log
 *
 * @param method
 * @param methodBuilder
 * @param schema
 * @param entity
 * @param jqlChecker
 */
public static void generateLogForModifiers(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
    JQLChecker jqlChecker = JQLChecker.getInstance();
    final One<Boolean> usedInWhere = new One<Boolean>(false);
    methodBuilder.addCode("\n// display log\n");
    String sqlForLog = jqlChecker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onColumnNameToUpdate(String columnName) {
            // only entity's columns
            return currentEntity.findPropertyByName(columnName).columnName;
        }

        @Override
        public String onColumnName(String columnName) {
            // return entity.findByName(columnName).columnName;
            return currentSchema.findColumnNameByPropertyName(method, columnName);
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            if (usedInWhere.value0) {
                return "?";
            } else {
                String paramName = bindParameterName;
                if (paramName.contains(".")) {
                    String[] a = paramName.split("\\.");
                    if (a.length == 2) {
                        paramName = a[1];
                    }
                }
                SQLProperty property = currentEntity.findPropertyByName(paramName);
                AssertKripton.assertTrueOrUnknownPropertyInJQLException(property != null, method, bindParameterName);
                return ":" + property.columnName;
            }
        }

        @Override
        public void onWhereStatementBegin(Where_stmtContext ctx) {
            usedInWhere.value0 = true;
        }

        @Override
        public void onWhereStatementEnd(Where_stmtContext ctx) {
            usedInWhere.value0 = false;
        }
    });
    if (method.jql.dynamicReplace.containsKey(JQLDynamicStatementType.DYNAMIC_WHERE)) {
        methodBuilder.addStatement("$T.info($S, $L)", Logger.class, sqlForLog.replace(method.jql.dynamicReplace.get(JQLDynamicStatementType.DYNAMIC_WHERE), "%s"), "StringUtils.ifNotEmptyAppend(_sqlDynamicWhere,\" AND \")");
    } else {
        methodBuilder.addStatement("$T.info($S)", Logger.class, sqlForLog);
    }
}
Also used : Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) One(com.abubusoft.kripton.common.One) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty)

Example 9 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class TestUriChecker method testProjectColumn.

@Test
public void testProjectColumn() {
    String sql = "select count(*) as pippo ,fieldName1, composed.fieldName2 from table where id = ${bean.id}";
    JQL jql = new JQL();
    jql.value = sql;
    Finder<SQLProperty> finder = new Finder<SQLProperty>() {

        @Override
        public SQLProperty findPropertyByName(String name) {
            // properties.columnName = name;
            return null;
        }

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

        @Override
        public String getSimpleName() {
            return "table";
        }

        @Override
        public String getTableName() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    JQLChecker.getInstance().extractProjections(dummyContext, jql.value, finder);
}
Also used : JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) Finder(com.abubusoft.kripton.processor.core.Finder) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Example 10 with SQLProperty

use of com.abubusoft.kripton.processor.sqlite.model.SQLProperty in project kripton by xcesco.

the class TestJqlChecker method testProjectColumn.

@Test
public void testProjectColumn() {
    // SQLEntity entity=new SQLEntity(elementUtils, model, element);
    String sql = "select count(*) as pippo ,fieldName1, composed.fieldName2 from table where id = ${bean.id}";
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker.getInstance().extractProjections(dummyContext, jql.value, 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;
        }
    });
}
Also used : JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) ArrayList(java.util.ArrayList) List(java.util.List) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Aggregations

SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)41 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)23 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)22 TypeName (com.squareup.javapoet.TypeName)18 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)14 ArrayList (java.util.ArrayList)12 One (com.abubusoft.kripton.common.One)10 Pair (com.abubusoft.kripton.common.Pair)9 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)7 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)7 InvalidMethodSignException (com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)6 ModelProperty (com.abubusoft.kripton.processor.core.ModelProperty)5 LinkedHashSet (java.util.LinkedHashSet)5 SQLiteStatement (android.database.sqlite.SQLiteStatement)4 SQLTypeAdapterUtils (com.abubusoft.kripton.common.SQLTypeAdapterUtils)4 PropertyNotFoundException (com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException)4 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)4 BaseProcessorTest (base.BaseProcessorTest)3 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)3 JQL (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL)3