use of com.developmentontheedge.sql.model.AstDerivedColumn 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.AstDerivedColumn in project be5 by DevelopmentOnTheEdge.
the class Be5QueryExecutor method hasColumnWithLabel.
private boolean hasColumnWithLabel(AstStart ast, String idColumnLabel) {
AstQuery query = ast.getQuery();
Optional<AstSelect> selectOpt = query.children().select(AstSelect.class).collect(MoreCollectors.onlyOne());
if (!selectOpt.isPresent())
return false;
AstSelect select = selectOpt.get();
return select.getSelectList().children().select(AstDerivedColumn.class).map(AstDerivedColumn::getAlias).nonNull().map(alias -> alias.replaceFirst("^\"(.+)\"$", "$1")).has(idColumnLabel);
}
use of com.developmentontheedge.sql.model.AstDerivedColumn in project be5 by DevelopmentOnTheEdge.
the class MySqlTransformer method transformIdentifier.
@Override
protected void transformIdentifier(AstIdentifierConstant identifier) {
if (identifier.getQuoteSymbol() == QuoteSymbol.DOUBLE_QUOTE) {
Node parent = identifier.jjtGetParent();
if (parent instanceof AstDerivedColumn)
return;
identifier.setQuoteSymbol(QuoteSymbol.BACKTICK);
}
if (identifier.getValue().equalsIgnoreCase("current_timestamp")) {
identifier.replaceWith(parserContext.getFunction("now").node());
}
}
use of com.developmentontheedge.sql.model.AstDerivedColumn in project be5 by DevelopmentOnTheEdge.
the class SqlServerTransformer method transformSelect.
@Override
protected void transformSelect(AstSelect select) {
AstLimit limit = select.getLimit();
if (limit != null) {
if (limit.getOffset() == 0) {
limit.setShape("TOP", null);
select.moveToFront(limit);
} else {
if (select.getOrderBy() == null)
throw new IllegalStateException("The ranking function \"ROW_NUMBER\" must have an ORDER BY clause");
select.dropLimit();
SimpleNode parent = select.jjtGetParent();
int idx = parent.indexOf(select);
AstIdentifierConstant tmp = new AstIdentifierConstant("tmp");
AstIdentifierConstant rn = new AstIdentifierConstant("rn");
AstSelectList list = select.getSelectList();
AstTableRef tableRef = new AstTableRef(select);
tableRef.setAsToken(true);
tableRef.addChild(tmp);
AstFrom from = new AstFrom(tableRef);
AstWhere where = new AstWhere();
AstBetweenPredicate between = new AstBetweenPredicate(new AstFieldReference(tmp, rn), AstNumericConstant.of(limit.getOffset()), AstNumericConstant.of(limit.getOffset() + limit.getLimit()));
where.addChild(between);
AstSelect newSelect = new AstSelect((AstSelectList) list.clone(), from, where);
AstFunNode func = parserContext.getFunction("row_number").node();
AstDerivedColumn derCol = new AstDerivedColumn(new AstWindowFunction(func, new AstWindowSpecification(select.getOrderBy())));
select.dropOrder();
derCol.setPrefixComma(true);
derCol.setAsToken(true);
derCol.addChild(rn);
if (list.isAllColumns()) {
for (AstTableRef tr : select.getFrom().tableRefs()) {
String tableName = tr.getAlias() == null ? tr.getTable() : tr.getAlias();
list.addChild(new AstFieldReference(new AstIdentifierConstant(tableName), new AstIdentifierConstant("*")));
}
}
list.addChild(derCol);
parent.jjtAddChild(newSelect, idx);
}
}
super.transformSelect(select);
}
use of com.developmentontheedge.sql.model.AstDerivedColumn in project be5 by DevelopmentOnTheEdge.
the class CountApplier method transform.
public void transform(ParserContext ctx, AstStart ast) {
AstQuery query = ast.getQuery();
query.children().select(AstSelect.class).forEach(AstSelect::dropOrder);
if (query.jjtGetNumChildren() > 1)
throw new UnsupportedOperationException("UNION queries are not supported for COUNT");
AstSelect select = query.children().select(AstSelect.class).findFirst().get();
AstSelectList selectList = select.getSelectList();
selectList.removeChildren();
AstCount countFn = new AstCount();
AstDerivedColumn countNode = new AstDerivedColumn(countFn, "CNT");
countNode.setAsToken(true);
selectList.addChild(countNode);
}
Aggregations