Search in sources :

Example 6 with JqlBaseListener

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

the class SQLiteSchemaVerifierHelper method extractCommands.

static List<String> extractCommands(SQLiteDatabase database, InputStream inputStream) {
    final List<String> result = new ArrayList<>();
    final String input = IOUtils.readText(inputStream);
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);
    ParserRuleContext parseContext = parser.parse();
    ParseTreeWalker walk = new ParseTreeWalker();
    walk.walk(new JqlBaseListener() {

        @Override
        public void enterSql_stmt(Sql_stmtContext ctx) {
            int start = ctx.getStart().getStartIndex();
            int stop = ctx.getStop().getStopIndex() + 1;
            if (start == stop)
                return;
            result.add(input.substring(start, stop));
        }
    }, parseContext);
    return result;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) JqlLexer(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlLexer) ArrayList(java.util.ArrayList) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) JqlParser(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser) Sql_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Sql_stmtContext) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 7 with JqlBaseListener

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

the class SQLiteUpdateTestHelper method readSQLFromFile.

public static List<String> readSQLFromFile(String sqlDefinitionFile) {
    try {
        final List<String> executionList = new ArrayList<>();
        File f = new File(sqlDefinitionFile);
        final String ddl = IOUtils.toString(new FileInputStream(f), Charset.forName("utf-8"));
        MigrationSQLChecker.getInstance().analyze(ddl, new JqlBaseListener() {

            public void enterSelect_stmt(Select_stmtContext ctx) {
            }

            public void enterSql_stmt(Sql_stmtContext ctx) {
                ;
                int start = ctx.getStart().getStartIndex();
                int stop = ctx.getStop().getStopIndex() + 1;
                if (start == stop)
                    return;
                String statement = ddl.substring(start, stop).trim();
                executionList.add(statement);
            }
        });
        return executionList;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) IOException(java.io.IOException) File(java.io.File) Sql_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Sql_stmtContext) FileInputStream(java.io.FileInputStream) Select_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Select_stmtContext)

Example 8 with JqlBaseListener

use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener 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 9 with JqlBaseListener

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

the class JQLChecker method extractPlaceHolders.

private <L extends Collection<JQLPlaceHolder>> L extractPlaceHolders(final JQLContext jqlContext, String jql, final L result) {
    final One<Boolean> valid = new One<>();
    valid.value0 = false;
    analyzeInternal(jqlContext, jql, new JqlBaseListener() {

        @Override
        public void enterBind_parameter(Bind_parameterContext ctx) {
            String value;
            if (ctx.bind_parameter_name() != null) {
                value = ctx.bind_parameter_name().getText();
            } else {
                value = ctx.getText();
            }
            result.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, value));
        }

        @Override
        public void enterBind_dynamic_sql(Bind_dynamic_sqlContext ctx) {
            result.add(new JQLPlaceHolder(JQLPlaceHolderType.DYNAMIC_SQL, ctx.bind_parameter_name().getText()));
        }
    });
    return result;
}
Also used : One(com.abubusoft.kripton.common.One) Bind_dynamic_sqlContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_dynamic_sqlContext) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Bind_parameterContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)

Example 10 with JqlBaseListener

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

the class JQLChecker method extractPlaceHoldersFromVariableStatement.

private <L extends Collection<JQLPlaceHolder>> L extractPlaceHoldersFromVariableStatement(final JQLContext jqlContext, String jql, final L result) {
    final One<Boolean> valid = new One<>();
    if (!StringUtils.hasText(jql))
        return result;
    valid.value0 = false;
    analyzeVariableStatementInternal(jqlContext, jql, new JqlBaseListener() {

        @Override
        public void enterBind_parameter(Bind_parameterContext ctx) {
            String parameter;
            if (ctx.bind_parameter_name() != null) {
                parameter = ctx.bind_parameter_name().getText();
            } else {
                parameter = ctx.getText();
            }
            result.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, parameter));
        }

        @Override
        public void enterBind_dynamic_sql(Bind_dynamic_sqlContext ctx) {
            result.add(new JQLPlaceHolder(JQLPlaceHolderType.DYNAMIC_SQL, ctx.bind_parameter_name().getText()));
        }
    });
    return result;
}
Also used : One(com.abubusoft.kripton.common.One) Bind_dynamic_sqlContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_dynamic_sqlContext) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Bind_parameterContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)

Aggregations

JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)12 One (com.abubusoft.kripton.common.One)7 Bind_parameterContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)7 Projected_columnsContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext)4 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)4 Bind_dynamic_sqlContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_dynamic_sqlContext)3 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)3 ArrayList (java.util.ArrayList)3 Column_nameContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_nameContext)2 Column_name_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext)2 Column_value_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext)2 Columns_to_updateContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Columns_to_updateContext)2 Conflict_algorithmContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext)2 Sql_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Sql_stmtContext)2 LinkedHashSet (java.util.LinkedHashSet)2 BaseProcessorTest (base.BaseProcessorTest)1 BindSqlDelete (com.abubusoft.kripton.android.annotation.BindSqlDelete)1 BindSqlInsert (com.abubusoft.kripton.android.annotation.BindSqlInsert)1 BindSqlSelect (com.abubusoft.kripton.android.annotation.BindSqlSelect)1 BindSqlUpdate (com.abubusoft.kripton.android.annotation.BindSqlUpdate)1