Search in sources :

Example 6 with Where_stmtContext

use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext in project kripton by xcesco.

the class ModifyBeanHelper method extractSQLForJavaDoc.

private String extractSQLForJavaDoc(final SQLiteModelMethod method) {
    final One<Boolean> usedInWhere = new One<>(false);
    String sqlForJavaDoc = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onColumnNameToUpdate(String columnName) {
            return currentEntity.findPropertyByName(columnName).columnName;
        }

        @Override
        public String onColumnName(String columnName) {
            return currentEntity.findPropertyByName(columnName).columnName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            if (!usedInWhere.value0) {
                if (bindParameterName.contains(".")) {
                    String[] a = bindParameterName.split("\\.");
                    if (a.length == 2) {
                        bindParameterName = a[1];
                    }
                }
                return ":" + bindParameterName;
            } else {
                return null;
            }
        }

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

        @Override
        public void onWhereStatementEnd(Where_stmtContext ctx) {
            usedInWhere.value0 = false;
        }
    });
    return sqlForJavaDoc;
}
Also used : Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) One(com.abubusoft.kripton.common.One)

Example 7 with Where_stmtContext

use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext in project kripton by xcesco.

the class SqlBuilderHelper method generateLog.

/**
 * <p>
 * Generate log for INSERT operations
 * </p>
 *
 * @param method
 * @param methodBuilder
 */
public static void generateLog(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    // log is enabled
    if (daoDefinition.isLogEnabled()) {
        // generate log section - BEGIN
        methodBuilder.addComment("log section BEGIN");
        methodBuilder.beginControlFlow("if (_context.isLogEnabled())");
        methodBuilder.addCode("// log for insert -- BEGIN \n");
        JQLChecker checker = JQLChecker.getInstance();
        final One<Boolean> inWhere = new One<Boolean>(false);
        String sql = checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {

            @Override
            public String onBindParameter(String bindParameterName) {
                if (inWhere.value0) {
                    return "?";
                }
                return null;
            }

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

            @Override
            public void onWhereStatementEnd(Where_stmtContext ctx) {
                super.onWhereStatementEnd(ctx);
                inWhere.value0 = false;
            }
        });
        if (method.jql.containsSelectOperation) {
            // log
            // manage log
            methodBuilder.addCode("\n");
            methodBuilder.addStatement("$T.info($S)", Logger.class, sql);
        } else {
            sql = checker.replaceVariableStatements(method, sql, new JQLReplaceVariableStatementListenerImpl() {

                @Override
                public String onColumnNameSet(String statement) {
                    return "%s";
                }

                @Override
                public String onColumnValueSet(String statement) {
                    return "%s";
                }
            });
            methodBuilder.addStatement("$T _columnNameBuffer=new $T()", StringBuffer.class, StringBuffer.class);
            methodBuilder.addStatement("$T _columnValueBuffer=new $T()", StringBuffer.class, StringBuffer.class);
            methodBuilder.addStatement("String _columnSeparator=$S", "");
            SqlBuilderHelper.forEachColumnInContentValue(methodBuilder, method, "_contentValues.keys()", false, new OnColumnListener() {

                @Override
                public void onColumnCheck(MethodSpec.Builder methodBuilder, String columNameVariable) {
                    methodBuilder.addStatement("_columnNameBuffer.append(_columnSeparator+$L)", columNameVariable);
                    methodBuilder.addStatement("_columnValueBuffer.append(_columnSeparator+$S+$L)", ":", columNameVariable);
                    methodBuilder.addStatement("_columnSeparator=$S", ", ");
                }
            });
            methodBuilder.addStatement("$T.info($S, _columnNameBuffer.toString(), _columnValueBuffer.toString())", Logger.class, sql);
        }
        generateLogForContentValues(method, methodBuilder);
        methodBuilder.addCode("// log for insert -- END \n\n");
        SqlBuilderHelper.generateLogForWhereParameters(method, methodBuilder);
        // generate log section - END
        methodBuilder.endControlFlow();
        methodBuilder.addComment("log section END");
    }
}
Also used : JQLReplaceVariableStatementListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl) Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) MethodSpec(com.squareup.javapoet.MethodSpec) One(com.abubusoft.kripton.common.One) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)

Example 8 with Where_stmtContext

use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext in project kripton by xcesco.

the class GenericSQLHelper method generateGenericExecSQL.

/**
 * @param methodBuilder
 * @param method
 * @param daoDefinition
 * @param schema
 */
public static void generateGenericExecSQL(MethodSpec.Builder methodBuilder, final SQLiteModelMethod method) {
    final SQLiteDaoDefinition daoDefinition = method.getParent();
    boolean nullable;
    final List<String> paramsList = new ArrayList<String>();
    final List<String> contentValueList = new ArrayList<String>();
    final One<Boolean> columnsToUpdate = new One<Boolean>(true);
    String sql = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public void onWhereStatementBegin(Where_stmtContext ctx) {
            super.onWhereStatementBegin(ctx);
            columnsToUpdate.value0 = false;
        }

        @Override
        public void onWhereStatementEnd(Where_stmtContext ctx) {
            super.onWhereStatementEnd(ctx);
            columnsToUpdate.value0 = true;
        }

        @Override
        public String onColumnName(String columnName) {
            String resolvedName = currentSchema.findColumnNameByPropertyName(method, columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(resolvedName != null, method, columnName);
            return resolvedName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            String propertyName = method.findParameterAliasByName(bindParameterName);
            if (columnsToUpdate.value0) {
                contentValueList.add(propertyName);
            } else {
                paramsList.add(propertyName);
            }
            return "?";
        }
    });
    // update/insert columns
    final SQLiteEntity entity = daoDefinition.getEntity();
    for (String item : contentValueList) {
        // ASSERT: property is always in entity
        String propertyName = method.findParameterNameByAlias(item);
        TypeName paramType = method.findParameterTypeByAliasOrName(item);
        SQLProperty property = entity.get(item);
        if (propertyName == null)
            throw (new PropertyNotFoundException(method, propertyName, paramType));
        Pair<String, TypeName> methodParam = new Pair<String, TypeName>(propertyName, paramType);
        // check same type
        TypeUtility.checkTypeCompatibility(method, methodParam, property);
        if (method.isLogEnabled()) {
            methodBuilder.addCode("_contentValues.put($S, ", property.columnName);
        } else {
            methodBuilder.addCode("_contentValues.put(");
        }
        // it does not need to be converted in string
        SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, methodParam.value0, methodParam.value1, property);
        methodBuilder.addCode(");\n");
    // if (nullable) {
    // methodBuilder.nextControlFlow("else");
    // 
    // if (method.isLogEnabled()) {
    // methodBuilder.addStatement("_contentValues.putNull($S)", property.columnName);
    // } else {
    // methodBuilder.addStatement("_contentValues.putNull()");
    // }
    // 
    // methodBuilder.endControlFlow();
    // }
    }
    // where condition
    methodBuilder.addComment("build where condition");
    {
        // String separator = "";
        TypeName paramType;
        String realName;
        for (String item : paramsList) {
            methodBuilder.addCode("_contentValues.addWhereArgs(");
            paramType = method.findParameterTypeByAliasOrName(item);
            realName = method.findParameterNameByAlias(item);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(paramType != null, method, item);
            // code for query arguments
            nullable = TypeUtility.isNullable(paramType);
            if (nullable) {
                methodBuilder.addCode("($L==null?\"\":", realName);
            }
            // check for string conversion
            TypeUtility.beginStringConversion(methodBuilder, paramType);
            SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, realName, paramType, null);
            // check for string conversion
            TypeUtility.endStringConversion(methodBuilder, paramType);
            if (nullable) {
                methodBuilder.addCode(")");
            }
            methodBuilder.addCode(");\n");
        }
    }
    // log for where parames
    SqlBuilderHelper.generateLog(method, methodBuilder);
    // log
    methodBuilder.addCode("\n");
    methodBuilder.addStatement("database().execSQL($S, _contentValues.whereArgsAsArray())", sql);
}
Also used : Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) TypeName(com.squareup.javapoet.TypeName) PropertyNotFoundException(com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) Pair(com.abubusoft.kripton.common.Pair)

Aggregations

One (com.abubusoft.kripton.common.One)8 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)8 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)5 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)4 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)3 Projected_columnsContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext)3 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)3 ArrayList (java.util.ArrayList)3 Pair (com.abubusoft.kripton.common.Pair)2 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)2 Bind_parameterContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)2 Column_value_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext)2 Conflict_algorithmContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext)2 TypeName (com.squareup.javapoet.TypeName)2 BindSqlInsert (com.abubusoft.kripton.android.annotation.BindSqlInsert)1 BindSqlUpdate (com.abubusoft.kripton.android.annotation.BindSqlUpdate)1 Triple (com.abubusoft.kripton.common.Triple)1 InvalidMethodSignException (com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)1 PropertyNotFoundException (com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException)1 InsertType (com.abubusoft.kripton.processor.sqlite.SqlInsertBuilder.InsertType)1