Search in sources :

Example 1 with AstLimit

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

the class PostgreSqlTransformer method transformSelect.

@Override
protected void transformSelect(AstSelect select) {
    AstLimit limit = select.getLimit();
    if (limit != null && limit.getOffset() != 0)
        limit.setShape("LIMIT", "OFFSET " + String.valueOf(limit.getOffset()));
    super.transformSelect(select);
}
Also used : AstLimit(com.developmentontheedge.sql.model.AstLimit)

Example 2 with AstLimit

use of com.developmentontheedge.sql.model.AstLimit 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();
        }
    }
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstLimit(com.developmentontheedge.sql.model.AstLimit) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Token(com.developmentontheedge.sql.model.Token)

Example 3 with AstLimit

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

the class LimitsApplier method transformQuery.

public boolean transformQuery(AstQuery query) {
    if (query.jjtGetNumChildren() == 1) {
        AstSelect select = (AstSelect) query.child(0);
        if (select.getLimit() != null)
            return false;
        AstLimit limit = new AstLimit();
        limit.setLimit(offset, count);
        select.addChild(limit);
    } else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        AstSelect select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        AstLimit limit = new AstLimit();
        limit.setLimit(offset, count);
        select.addChild(limit);
        query.replaceWith(new AstQuery(select));
    }
    // TODO: support offset, union, etc.
    return true;
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstLimit(com.developmentontheedge.sql.model.AstLimit) 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 4 with AstLimit

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

the class OracleTransformer method transformSelect.

@Override
protected void transformSelect(AstSelect select) {
    super.transformSelect(select);
    AstLimit limit = select.getLimit();
    if (limit != null) {
        select.dropLimit();
        SimpleNode parent = select.jjtGetParent();
        int idx = parent.indexOf(select);
        AstTableRef tableRef = new AstTableRef(select);
        AstFrom from = new AstFrom(tableRef);
        AstWhere where = new AstWhere();
        AstFunNode less = parserContext.getFunction("<=").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getLimit() + limit.getOffset()));
        where.addChild(less);
        if (limit.getOffset() == 0) {
            AstSelect newSelect = new AstSelect(new AstSelectList(), from, where);
            parent.jjtAddChild(newSelect, idx);
        } else {
            AstSelectList list = new AstSelectList();
            list.addChild(new AstFieldReference(new AstIdentifierConstant("tmp"), new AstIdentifierConstant("*,")));
            list.addChild(new AstIdentifierConstant("ROWNUM rn"));
            tableRef.addChild(new AstIdentifierConstant("tmp"));
            AstSelect innerSelect = new AstSelect(list, from, where);
            AstWhere outerWhere = new AstWhere();
            AstFunNode more = parserContext.getFunction(">").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getOffset()));
            outerWhere.addChild(more);
            AstSelect outerSelect = new AstSelect(select.getSelectList(), new AstFrom(new AstTableRef(innerSelect)), outerWhere);
            parent.jjtAddChild(outerSelect, idx);
        }
    }
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 5 with AstLimit

use of com.developmentontheedge.sql.model.AstLimit 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)

Aggregations

AstLimit (com.developmentontheedge.sql.model.AstLimit)6 AstSelect (com.developmentontheedge.sql.model.AstSelect)5 AstFrom (com.developmentontheedge.sql.model.AstFrom)4 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)4 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)4 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)3 AstWhere (com.developmentontheedge.sql.model.AstWhere)3 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)2 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)2 AstWindowSpecification (com.developmentontheedge.sql.model.AstWindowSpecification)2 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)1 Formatter (com.developmentontheedge.sql.format.Formatter)1 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)1 AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)1 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)1 AstQuery (com.developmentontheedge.sql.model.AstQuery)1