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