use of com.developmentontheedge.sql.model.AstTableRef in project be5 by DevelopmentOnTheEdge.
the class OrderByFilter method apply.
public void apply(AstQuery query, Map<String, String> columns) {
AstOrderBy orderBy = new AstOrderBy();
apply(columns, query, orderBy);
query.children().select(AstSelect.class).forEach(AstSelect::dropOrder);
AstSelect select;
if (query.jjtGetNumChildren() == 1)
select = (AstSelect) query.child(0);
else {
AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
query.replaceWith(new AstQuery(select));
}
select.addChild(orderBy);
}
use of com.developmentontheedge.sql.model.AstTableRef 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.AstTableRef in project be5 by DevelopmentOnTheEdge.
the class DB2Transformer method transformSelect.
@Override
protected void transformSelect(AstSelect select) {
AstLimit limit = select.getLimit();
if (limit != null) {
if (limit.getOffset() == 0)
limit.setShape("FETCH FIRST", "ROWS ONLY");
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.AstTableRef in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method applyParameterTag.
private void applyParameterTag(SimpleNode newTree, int i, String key, Function<String, String> varResolver) {
String value;
SimpleNode replacement;
AstBeParameterTag paramNode = (AstBeParameterTag) newTree.child(i);
String multiple = paramNode.getMultiple();
boolean tableRefAddend = newTree instanceof AstTableRef && i == 1;
if (multiple == null) {
value = context.getParameter(paramNode.getName());
Map<String, String> propertyMap = getSubQueryPropertyMap(key, varResolver);
if (value == null && propertyMap.containsKey(paramNode.getName())) {
value = propertyMap.get(paramNode.getName());
}
replacement = applyParameters(paramNode, value, tableRefAddend);
} else {
if (newTree instanceof AstInValueList) {
List<String> values = context.getListParameter(paramNode.getName());
paramNode.replaceWith(StreamEx.of(values).map(val -> applyParameters(paramNode, val, tableRefAddend)).toArray(SimpleNode[]::new));
return;
}
if (!(newTree instanceof AstInPredicate))
throw new IllegalArgumentException("Parameter Multiple can only be put inside InPredicate");
AstInValueList list = new AstInValueList(SqlParserTreeConstants.JJTINVALUELIST);
List<String> values = context.getListParameter(paramNode.getName());
if (values != null) {
for (String val : values) {
list.addChild(applyParameters(paramNode, val, tableRefAddend));
}
}
replacement = list;
}
replacement.inheritFrom(paramNode);
if (tableRefAddend) {
AstTableRef tableRef = (AstTableRef) newTree;
tableRef.setTable(tableRef.getTable() + replacement.format());
tableRef.removeChild(i);
} else {
newTree.jjtAddChild(replacement, i);
}
}
Aggregations