Search in sources :

Example 1 with Token

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

the class OrderByFilter method apply.

private void apply(Map<String, String> columns, AstQuery query, AstOrderBy orderBy) {
    for (Map.Entry<String, String> column : columns.entrySet()) {
        int num = 0;
        for (AstDerivedColumn derColumn : query.tree().select(AstDerivedColumn.class)) {
            if (column.getKey().equals(derColumn.getColumn()) || column.getKey().equals(derColumn.getAlias())) {
                num = derColumn.jjtGetParent().indexOf(derColumn) + 1;
                break;
            }
        }
        if (num == 0)
            throw new IllegalArgumentException("Unknown column " + column.getKey() + " in order clause");
        String dir = column.getValue();
        if (!dir.equalsIgnoreCase("ASC") && !dir.equalsIgnoreCase("DESC"))
            throw new IllegalArgumentException("Unknown direction " + dir + ". Was expecting ASC or DESC");
        AstOrderingElement elem = new AstOrderingElement(0);
        elem.addChild(AstNumericConstant.of(num));
        elem.setDirectionToken(new Token(0, dir));
        orderBy.addChild(elem);
    }
}
Also used : AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) Token(com.developmentontheedge.sql.model.Token) Map(java.util.Map)

Example 2 with Token

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

Aggregations

AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)2 Token (com.developmentontheedge.sql.model.Token)2 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)1 Formatter (com.developmentontheedge.sql.format.Formatter)1 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)1 AstLimit (com.developmentontheedge.sql.model.AstLimit)1 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)1 AstSelect (com.developmentontheedge.sql.model.AstSelect)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1