use of com.developmentontheedge.sql.model.AstOrderingElement 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.AstOrderingElement 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();
}
}
}
use of com.developmentontheedge.sql.model.AstOrderingElement in project be5 by DevelopmentOnTheEdge.
the class OracleTransformer method transformStringAgg.
@Override
protected void transformStringAgg(AstFunNode node) {
node.setFunction(LISTAGG);
if (node.isDistinct())
throw new IllegalStateException("DISTINCT clause is unsupported for " + node.getFunction().getName());
AstOrderBy orderBy;
if (node.child(node.jjtGetNumChildren() - 1) instanceof AstOrderBy) {
orderBy = (AstOrderBy) node.child(node.jjtGetNumChildren() - 1);
node.removeChild(node.jjtGetNumChildren() - 1);
} else {
orderBy = new AstOrderBy(new AstOrderingElement(node.child(0)));
}
node.replaceWith(new AstOrderedSetAggregate((AstFunNode) node.clone(), new AstWithinGroup(orderBy)));
}
use of com.developmentontheedge.sql.model.AstOrderingElement in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method applyParameters.
private SimpleNode applyParameters(AstBeParameterTag paramNode, String value, boolean tableRefAddend) {
String defValue = paramNode.getDefValue() != null ? paramNode.getDefValue() : "";
String prefix = paramNode.getPrefix() != null ? paramNode.getPrefix() : "";
String postfix = paramNode.getPostfix() != null ? paramNode.getPostfix() : "";
String regex = paramNode.getRegex();
String repl = paramNode.getReplacement();
String changeCase = paramNode.getCase();
String type = paramNode.getType();
boolean safeStr = "yes".equals(paramNode.getSafeStr()) && !tableRefAddend;
if (value == null)
value = "";
if (value.equals(""))
value = defValue;
if (regex != null && repl != null)
value = value.replaceAll(regex, repl);
if ("upper".equals(changeCase))
value = value.toUpperCase();
if ("lower".equals(changeCase))
value = value.toLowerCase();
if ("capitalize".equals(changeCase)) {
value = value.toLowerCase();
if (value.length() > 0)
value = value.substring(0, 1).toUpperCase() + value.substring(1);
}
value = prefix + value + postfix;
SimpleNode constant;
if (paramNode.jjtGetParent() instanceof AstStringConstant) {
constant = new AstStringPart(value);
} else if (type != null) {
if (SqlTypeUtils.isNumber(type)) {
if (!value.equals("")) {
constant = AstNumericConstant.of((Number) SqlTypeUtils.parseValue(value, type));
} else {
constant = new AstIdentifierConstant(value);
}
} else {
constant = new AstStringConstant(value);
}
} else if (!safeStr) {
constant = new AstIdentifierConstant(value);
} else if (paramNode.jjtGetParent() instanceof AstOrderingElement) {
constant = new AstIdentifierConstant(value, false);
} else {
constant = new AstStringConstant(value);
}
return constant;
}
Aggregations