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