Search in sources :

Example 6 with AstFrom

use of com.developmentontheedge.sql.model.AstFrom 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);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Example 7 with AstFrom

use of com.developmentontheedge.sql.model.AstFrom 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);
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstWindowSpecification(com.developmentontheedge.sql.model.AstWindowSpecification) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstBetweenPredicate(com.developmentontheedge.sql.model.AstBetweenPredicate)

Example 8 with AstFrom

use of com.developmentontheedge.sql.model.AstFrom 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);
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstWindowSpecification(com.developmentontheedge.sql.model.AstWindowSpecification) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstBetweenPredicate(com.developmentontheedge.sql.model.AstBetweenPredicate)

Example 9 with AstFrom

use of com.developmentontheedge.sql.model.AstFrom in project be5 by DevelopmentOnTheEdge.

the class DB2Transformer method transformDualFrom.

@Override
protected AstFrom transformDualFrom(AstFrom from) {
    AstFrom dual = AstFrom.createDual();
    dual.tableRefs().forEach(t -> t.setTable("SYSIBM.SYSDUMMY1"));
    return dual;
}
Also used : AstFrom(com.developmentontheedge.sql.model.AstFrom)

Aggregations

AstFrom (com.developmentontheedge.sql.model.AstFrom)9 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)8 AstSelect (com.developmentontheedge.sql.model.AstSelect)8 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)8 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)8 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)5 AstWhere (com.developmentontheedge.sql.model.AstWhere)5 AstLimit (com.developmentontheedge.sql.model.AstLimit)4 AstQuery (com.developmentontheedge.sql.model.AstQuery)4 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)3 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)2 AstWindowSpecification (com.developmentontheedge.sql.model.AstWindowSpecification)2 AstColumnList (com.developmentontheedge.sql.model.AstColumnList)1 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)1 AstValues (com.developmentontheedge.sql.model.AstValues)1 Function (com.developmentontheedge.sql.model.Function)1