use of com.developmentontheedge.sql.model.Token in project be5 by DevelopmentOnTheEdge.
the class OrderByFilter method apply.
private void apply(Map<String, String> columns, AstQuery query, AstOrderBy orderBy) {
for (Map.Entry<String, String> column : columns.entrySet()) {
int num = 0;
for (AstDerivedColumn derColumn : query.tree().select(AstDerivedColumn.class)) {
if (column.getKey().equals(derColumn.getColumn()) || column.getKey().equals(derColumn.getAlias())) {
num = derColumn.jjtGetParent().indexOf(derColumn) + 1;
break;
}
}
if (num == 0)
throw new IllegalArgumentException("Unknown column " + column.getKey() + " in order clause");
String dir = column.getValue();
if (!dir.equalsIgnoreCase("ASC") && !dir.equalsIgnoreCase("DESC"))
throw new IllegalArgumentException("Unknown direction " + dir + ". Was expecting ASC or DESC");
AstOrderingElement elem = new AstOrderingElement(0);
elem.addChild(AstNumericConstant.of(num));
elem.setDirectionToken(new Token(0, dir));
orderBy.addChild(elem);
}
}
use of com.developmentontheedge.sql.model.Token in project be5 by DevelopmentOnTheEdge.
the class Be5QueryExecutor method applySort.
private void applySort(DebugQueryLogger dql, AstStart ast) {
if (sortColumn >= 0) {
try {
DynamicProperty[] schema = getSchema(new Formatter().format(ast, context, parserContext));
int sortCol = getQuerySortingColumn(schema);
if (sortCol > 0) {
AstSelect sel = (AstSelect) ast.getQuery().jjtGetChild(ast.getQuery().jjtGetNumChildren() - 1);
AstOrderBy orderBy = sel.getOrderBy();
if (orderBy == null) {
orderBy = new AstOrderBy();
sel.addChild(orderBy);
AstLimit astLimit = sel.children().select(AstLimit.class).findFirst().orElse(null);
if (astLimit != null) {
sel.removeChild(astLimit);
sel.addChild(astLimit);
}
}
AstOrderingElement oe = new AstOrderingElement(AstNumericConstant.of(sortCol));
if (sortDesc) {
oe.setDirectionToken(new Token(0, "DESC"));
}
orderBy.addChild(oe);
orderBy.moveToFront(oe);
}
dql.log("With sort", ast);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Aggregations