Search in sources :

Example 1 with Columns_to_updateContext

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

the class JQLBuilder method buildJQLUpdate.

/**
 * <pre>
 * UPDATE bean01 SET text=${text} WHERE id=${id}
 * </pre>
 *
 * @param method
 * @param preparedJql
 * @return
 */
private static JQL buildJQLUpdate(final SQLiteModelMethod method, final JQL result, Map<JQLDynamicStatementType, String> dynamicReplace, String preparedJql) {
    final Class<? extends Annotation> annotation = BindSqlUpdate.class;
    if (StringUtils.hasText(preparedJql)) {
        result.value = preparedJql;
        // UPDATE can contains bind parameter in column values and select
        // statement
        final One<Boolean> inWhereCondition = new One<Boolean>(false);
        final One<Boolean> inColumnsToUpdate = new One<Boolean>(false);
        JQLChecker.getInstance().analyze(method, result, new JqlBaseListener() {

            @Override
            public void enterProjected_columns(Projected_columnsContext ctx) {
                if (inColumnsToUpdate.value0) {
                    result.containsSelectOperation = true;
                }
            }

            @Override
            public void enterConflict_algorithm(Conflict_algorithmContext ctx) {
                result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(ctx.getText().toUpperCase());
            }

            @Override
            public void enterWhere_stmt(Where_stmtContext ctx) {
                inWhereCondition.value0 = true;
            }

            @Override
            public void exitWhere_stmt_clauses(Where_stmt_clausesContext ctx) {
                inWhereCondition.value0 = false;
            }

            @Override
            public void enterBind_parameter(Bind_parameterContext ctx) {
                if (inWhereCondition.value0) {
                    result.bindParameterOnWhereStatementCounter++;
                } else {
                    result.bindParameterAsColumnValueCounter++;
                }
            }

            @Override
            public void enterColumns_to_update(Columns_to_updateContext ctx) {
                inColumnsToUpdate.value0 = true;
            }

            @Override
            public void exitColumns_to_update(Columns_to_updateContext ctx) {
                inColumnsToUpdate.value0 = false;
            }
        });
        JQLChecker.getInstance().replaceVariableStatements(method, preparedJql, new JQLReplaceVariableStatementListenerImpl() {

            @Override
            public String onWhere(String statement) {
                result.annotatedWhere = true;
                result.staticWhereConditions = true;
                return null;
            }
        });
        if (result.containsSelectOperation) {
            AssertKripton.assertTrueOrInvalidMethodSignException(method.getReturnClass().equals(TypeName.VOID), method, "defined JQL requires that method's return type is void");
        }
    } else {
        final SQLiteDaoDefinition dao = method.getParent();
        Set<String> fields;
        ModifyType modifyType = SqlModifyBuilder.detectModifyType(method, JQLType.UPDATE);
        if (modifyType == ModifyType.UPDATE_BEAN) {
            fields = extractFieldsFromAnnotation(method, annotation, false);
        } else {
            fields = extractFieldsFromMethodParameters(method, annotation);
        }
        AssertKripton.assertTrueOrInvalidMethodSignException(fields.size() > 0, method, "no field was specified for update");
        result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(AnnotationUtility.extractAsEnumerationValue(method.getElement(), annotation, AnnotationAttributeType.CONFLICT_ALGORITHM_TYPE));
        StringBuilder builder = new StringBuilder();
        builder.append(UPDATE_KEYWORD);
        builder.append(" " + result.conflictAlgorithmType.getSqlForInsert());
        // entity name
        builder.append(dao.getEntitySimplyClassName());
        // recreate fields
        final One<String> prefix = new One<>("");
        if (result.hasParamBean()) {
            prefix.value0 = result.paramBean + ".";
        }
        builder.append(" " + SET_KEYWORD + " ");
        builder.append(forEachFields(fields, new OnFieldListener() {

            @Override
            public String onField(String item) {
                return item + "=${" + prefix.value0 + item + "}";
            }
        }));
        builder.append(defineWhereStatement(method, result, annotation, dynamicReplace));
        result.value = builder.toString();
    }
    result.operationType = JQLType.UPDATE;
    result.dynamicReplace = dynamicReplace;
    return result;
}
Also used : Projected_columnsContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext) Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) Conflict_algorithmContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext) One(com.abubusoft.kripton.common.One) Columns_to_updateContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Columns_to_updateContext) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Bind_parameterContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext) BindSqlUpdate(com.abubusoft.kripton.android.annotation.BindSqlUpdate) Where_stmt_clausesContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmt_clausesContext) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) ModifyType(com.abubusoft.kripton.processor.sqlite.SqlModifyBuilder.ModifyType)

Example 2 with Columns_to_updateContext

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

the class JQLChecker method extractColumnsToInsertOrUpdate.

public Set<String> extractColumnsToInsertOrUpdate(final JQLContext jqlContext, String jqlValue, final Finder<SQLProperty> entity) {
    final Set<String> result = new LinkedHashSet<String>();
    final One<Boolean> selectionOn = new One<Boolean>(null);
    final One<Boolean> insertOn = new One<Boolean>(null);
    // Column_name_set is needed for insert
    // Columns_to_update is needed for update
    analyzeInternal(jqlContext, jqlValue, new JqlBaseListener() {

        @Override
        public void enterColumn_name_set(Column_name_setContext ctx) {
            if (insertOn.value0 == null) {
                insertOn.value0 = true;
            }
        }

        @Override
        public void exitColumn_name_set(Column_name_setContext ctx) {
            insertOn.value0 = false;
        }

        @Override
        public void enterColumns_to_update(Columns_to_updateContext ctx) {
            if (selectionOn.value0 == null) {
                selectionOn.value0 = true;
            }
        }

        @Override
        public void exitColumns_to_update(Columns_to_updateContext ctx) {
            selectionOn.value0 = false;
        }

        @Override
        public void enterColumn_name(Column_nameContext ctx) {
            // works for INSERTS
            if (insertOn.value0 != null && insertOn.value0 == true) {
                result.add(ctx.getText());
            }
        }

        @Override
        public void enterColumn_name_to_update(Column_name_to_updateContext ctx) {
            result.add(ctx.getText());
        }
    });
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Column_name_setContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext) Column_name_to_updateContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_to_updateContext) One(com.abubusoft.kripton.common.One) Columns_to_updateContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Columns_to_updateContext) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Column_nameContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_nameContext)

Aggregations

One (com.abubusoft.kripton.common.One)2 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)2 Columns_to_updateContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Columns_to_updateContext)2 BindSqlUpdate (com.abubusoft.kripton.android.annotation.BindSqlUpdate)1 ModifyType (com.abubusoft.kripton.processor.sqlite.SqlModifyBuilder.ModifyType)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 Column_name_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext)1 Column_name_to_updateContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_to_updateContext)1 Conflict_algorithmContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext)1 Projected_columnsContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext)1 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)1 Where_stmt_clausesContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmt_clausesContext)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 LinkedHashSet (java.util.LinkedHashSet)1