Search in sources :

Example 1 with AstOrderBy

use of com.developmentontheedge.sql.model.AstOrderBy 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 2 with AstOrderBy

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

the class OracleTransformer method transformStringAgg.

@Override
protected void transformStringAgg(AstFunNode node) {
    node.setFunction(LISTAGG);
    if (node.isDistinct())
        throw new IllegalStateException("DISTINCT clause is unsupported for " + node.getFunction().getName());
    AstOrderBy orderBy;
    if (node.child(node.jjtGetNumChildren() - 1) instanceof AstOrderBy) {
        orderBy = (AstOrderBy) node.child(node.jjtGetNumChildren() - 1);
        node.removeChild(node.jjtGetNumChildren() - 1);
    } else {
        orderBy = new AstOrderBy(new AstOrderingElement(node.child(0)));
    }
    node.replaceWith(new AstOrderedSetAggregate((AstFunNode) node.clone(), new AstWithinGroup(orderBy)));
}
Also used : AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstWithinGroup(com.developmentontheedge.sql.model.AstWithinGroup) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstOrderedSetAggregate(com.developmentontheedge.sql.model.AstOrderedSetAggregate)

Example 3 with AstOrderBy

use of com.developmentontheedge.sql.model.AstOrderBy 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 4 with AstOrderBy

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

the class DB2Transformer method transformStringAgg.

@Override
protected void transformStringAgg(AstFunNode node) {
    node.setFunction(LISTAGG);
    if (node.isDistinct())
        throw new IllegalStateException("DISTINCT clause is unsupported for " + node.getFunction().getName());
    if (node.child(node.jjtGetNumChildren() - 1) instanceof AstOrderBy) {
        AstOrderBy orderBy = (AstOrderBy) node.child(node.jjtGetNumChildren() - 1);
        node.removeChild(node.jjtGetNumChildren() - 1);
        node.replaceWith(new AstOrderedSetAggregate((AstFunNode) node.clone(), new AstWithinGroup(orderBy)));
    }
}
Also used : AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstWithinGroup(com.developmentontheedge.sql.model.AstWithinGroup) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstOrderedSetAggregate(com.developmentontheedge.sql.model.AstOrderedSetAggregate)

Aggregations

AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)4 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)2 AstOrderedSetAggregate (com.developmentontheedge.sql.model.AstOrderedSetAggregate)2 AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)2 AstSelect (com.developmentontheedge.sql.model.AstSelect)2 AstWithinGroup (com.developmentontheedge.sql.model.AstWithinGroup)2 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)1 Formatter (com.developmentontheedge.sql.format.Formatter)1 AstFrom (com.developmentontheedge.sql.model.AstFrom)1 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)1 AstLimit (com.developmentontheedge.sql.model.AstLimit)1 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)1 AstQuery (com.developmentontheedge.sql.model.AstQuery)1 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)1 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)1 Token (com.developmentontheedge.sql.model.Token)1 SQLException (java.sql.SQLException)1