Search in sources :

Example 1 with Select_or_valuesContext

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

Aggregations

One (com.abubusoft.kripton.common.One)1 Triple (com.abubusoft.kripton.common.Triple)1 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)1 Column_name_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext)1 Column_value_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext)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 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)1 ArrayList (java.util.ArrayList)1 Token (org.antlr.v4.runtime.Token)1