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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations