use of com.developmentontheedge.sql.model.AstSelect 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.AstSelect in project be5 by DevelopmentOnTheEdge.
the class GenericDbmsTransformer method recursiveProcessing.
private void recursiveProcessing(SimpleNode node) {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
SimpleNode child = node.child(i);
if (child instanceof AstFunNode) {
transformFunction((AstFunNode) child);
}
if (child instanceof AstSelect) {
transformSelect((AstSelect) child);
}
if (child instanceof AstIdentifierConstant) {
transformIdentifier((AstIdentifierConstant) child);
}
if (child instanceof AstStringConstant) {
transformString((AstStringConstant) child);
}
if (child instanceof AstCast) {
transformCastExpression((AstCast) child);
}
if (child instanceof AstExtract) {
transformExtractExpression((AstExtract) child);
}
if (child instanceof AstPosition) {
transformPosition((AstPosition) child);
}
if (child instanceof AstInterval) {
transformInterval((AstInterval) child);
}
if (child instanceof AstWith) {
transformWith((AstWith) child);
}
if (child instanceof AstExcept) {
transformExcept((AstExcept) child);
}
recursiveProcessing(child);
}
}
use of com.developmentontheedge.sql.model.AstSelect in project be5 by DevelopmentOnTheEdge.
the class ColumnsApplier method keepOnlyOutColumns.
public void keepOnlyOutColumns(AstBeSqlSubQuery subQuery) {
List<String> outColumns = Arrays.asList(subQuery.getOutColumns().split(","));
AstSelect select = (AstSelect) subQuery.getQuery().child(0);
AstSelectList selectList = select.getSelectList();
if (selectList.isAllColumns()) {
throw new IllegalStateException("All columns not support " + selectList.getNodeContent());
} else {
for (int i = selectList.jjtGetNumChildren() - 1; i >= 0; i--) {
AstDerivedColumn derivedColumn = (AstDerivedColumn) selectList.jjtGetChild(i);
if (!outColumns.contains(derivedColumn.getAlias().replace("\"", ""))) {
derivedColumn.remove();
}
}
}
}
Aggregations