Search in sources :

Example 1 with Column_name_setContext

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

the class JQLChecker method replaceVariableStatements.

/**
 * <p>
 * Given a sql, replace som component like where, order by, etc..
 *
 * <p>
 * Note that only first level of variable statements will be replaced.
 *
 * @param jql
 * @param listener
 * @return
 */
public String replaceVariableStatements(final JQLContext jqlContext, final String jql, final JQLReplaceVariableStatementListener listener) {
    final List<Triple<Token, Token, String>> replace = new ArrayList<>();
    final One<Integer> currentSelectLevel = new One<Integer>(-1);
    JqlBaseListener rewriterListener = new JqlBaseListener() {

        @Override
        public void enterSelect_core(Select_coreContext ctx) {
            currentSelectLevel.value0++;
        }

        @Override
        public void enterSelect_or_values(Select_or_valuesContext ctx) {
            currentSelectLevel.value0++;
        }

        @Override
        public void exitSelect_core(Select_coreContext ctx) {
            currentSelectLevel.value0--;
        }

        @Override
        public void exitSelect_or_values(Select_or_valuesContext ctx) {
            currentSelectLevel.value0--;
        }

        @Override
        public void enterProjected_columns(Projected_columnsContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onProjectedColumns(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterWhere_stmt(Where_stmtContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onWhere(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterOrder_stmt(Order_stmtContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onOrderBy(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterGroup_stmt(Group_stmtContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onGroup(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterHaving_stmt(Having_stmtContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onHaving(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterOffset_stmt(Offset_stmtContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onOffset(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterLimit_stmt(Limit_stmtContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onLimit(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterColumn_name_set(Column_name_setContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 2;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onColumnNameSet(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }

        @Override
        public void enterColumn_value_set(Column_value_setContext ctx) {
            // we work on level 0
            if (currentSelectLevel.value0 > 0)
                return;
            int start = ctx.getStart().getStartIndex() - 1;
            int stop = ctx.getStop().getStopIndex() + 2;
            if (start == stop)
                return;
            String statement = jql.substring(start, stop);
            String value = listener.onColumnValueSet(statement);
            if (value != null) {
                replace.add(new Triple<Token, Token, String>(ctx.start, ctx.stop, value));
            }
        }
    };
    return replaceInternal(jqlContext, jql, replace, rewriterListener);
}
Also used : Column_name_setContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext) Projected_columnsContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext) Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) Having_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Having_stmtContext) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Group_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Group_stmtContext) Select_coreContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Select_coreContext) Token(org.antlr.v4.runtime.Token) Order_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Order_stmtContext) Select_or_valuesContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Select_or_valuesContext) Triple(com.abubusoft.kripton.common.Triple) Column_value_setContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext) Limit_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Limit_stmtContext) Offset_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Offset_stmtContext)

Example 2 with Column_name_setContext

use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext 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)

Example 3 with Column_name_setContext

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

the class SqlBuilderHelper method orderContentValues.

public static List<Pair<String, TypeName>> orderContentValues(final SQLiteModelMethod method, final List<Pair<String, TypeName>> updateableParams) {
    final List<Pair<String, TypeName>> result = new ArrayList<Pair<String, TypeName>>();
    JQLChecker checker = JQLChecker.getInstance();
    final One<Boolean> inserMode = new One<Boolean>(false);
    checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        // used in update
        @Override
        public String onColumnNameToUpdate(String columnName) {
            String column = currentEntity.findPropertyByName(columnName).columnName;
            for (Pair<String, TypeName> item : updateableParams) {
                String paramNameInQuery = method.findParameterAliasByName(item.value0);
                if (paramNameInQuery.equalsIgnoreCase(columnName)) {
                    result.add(item);
                    break;
                }
            }
            return column;
        }

        // used in insert
        @Override
        public void onColumnNameSetBegin(Column_name_setContext ctx) {
            inserMode.value0 = true;
        }

        @Override
        public void onColumnNameSetEnd(Column_name_setContext ctx) {
            inserMode.value0 = false;
        }

        @Override
        public String onColumnName(String columnName) {
            if (!inserMode.value0)
                return columnName;
            String column = currentEntity.findPropertyByName(columnName).columnName;
            for (Pair<String, TypeName> item : updateableParams) {
                String paramNameInQuery = method.findParameterAliasByName(item.value0);
                if (paramNameInQuery.equalsIgnoreCase(columnName)) {
                    result.add(item);
                    break;
                }
            }
            return column;
        }
    });
    return result;
}
Also used : Column_name_setContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) Pair(com.abubusoft.kripton.common.Pair)

Aggregations

One (com.abubusoft.kripton.common.One)3 Column_name_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext)3 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)2 ArrayList (java.util.ArrayList)2 Pair (com.abubusoft.kripton.common.Pair)1 Triple (com.abubusoft.kripton.common.Triple)1 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)1 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)1 Column_nameContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_nameContext)1 Column_name_to_updateContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_to_updateContext)1 Column_value_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext)1 Columns_to_updateContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Columns_to_updateContext)1 Group_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Group_stmtContext)1 Having_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Having_stmtContext)1 Limit_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Limit_stmtContext)1 Offset_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Offset_stmtContext)1 Order_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Order_stmtContext)1 Projected_columnsContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext)1 Select_coreContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Select_coreContext)1 Select_or_valuesContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Select_or_valuesContext)1