Search in sources :

Example 1 with AstOrderingElement

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

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

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

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

the class ContextApplier method applyParameters.

private SimpleNode applyParameters(AstBeParameterTag paramNode, String value, boolean tableRefAddend) {
    String defValue = paramNode.getDefValue() != null ? paramNode.getDefValue() : "";
    String prefix = paramNode.getPrefix() != null ? paramNode.getPrefix() : "";
    String postfix = paramNode.getPostfix() != null ? paramNode.getPostfix() : "";
    String regex = paramNode.getRegex();
    String repl = paramNode.getReplacement();
    String changeCase = paramNode.getCase();
    String type = paramNode.getType();
    boolean safeStr = "yes".equals(paramNode.getSafeStr()) && !tableRefAddend;
    if (value == null)
        value = "";
    if (value.equals(""))
        value = defValue;
    if (regex != null && repl != null)
        value = value.replaceAll(regex, repl);
    if ("upper".equals(changeCase))
        value = value.toUpperCase();
    if ("lower".equals(changeCase))
        value = value.toLowerCase();
    if ("capitalize".equals(changeCase)) {
        value = value.toLowerCase();
        if (value.length() > 0)
            value = value.substring(0, 1).toUpperCase() + value.substring(1);
    }
    value = prefix + value + postfix;
    SimpleNode constant;
    if (paramNode.jjtGetParent() instanceof AstStringConstant) {
        constant = new AstStringPart(value);
    } else if (type != null) {
        if (SqlTypeUtils.isNumber(type)) {
            if (!value.equals("")) {
                constant = AstNumericConstant.of((Number) SqlTypeUtils.parseValue(value, type));
            } else {
                constant = new AstIdentifierConstant(value);
            }
        } else {
            constant = new AstStringConstant(value);
        }
    } else if (!safeStr) {
        constant = new AstIdentifierConstant(value);
    } else if (paramNode.jjtGetParent() instanceof AstOrderingElement) {
        constant = new AstIdentifierConstant(value, false);
    } else {
        constant = new AstStringConstant(value);
    }
    return constant;
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Aggregations

AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)4 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)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 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)1 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)1 AstLimit (com.developmentontheedge.sql.model.AstLimit)1 AstOrderedSetAggregate (com.developmentontheedge.sql.model.AstOrderedSetAggregate)1 AstSelect (com.developmentontheedge.sql.model.AstSelect)1 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)1 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)1 AstWithinGroup (com.developmentontheedge.sql.model.AstWithinGroup)1 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1