Search in sources :

Example 11 with AstFunNode

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

the class SqlServerTransformer method transformString.

@Override
protected void transformString(AstStringConstant string) {
    for (AstStringPart child : string.children().select(AstStringPart.class)) {
        String content = child.getContent().replace("\\'", "''");
        child.setContent(string.isEscape() ? content.replaceAll("\\\\([^bfnrt])", "$1") : content, true);
    }
    if (string.isEscape()) {
        string.setEscape(false);
        AstFunNode concat = DefaultParserContext.FUNC_PLUS.node();
        if (replacedEscapes(string, concat))
            string.replaceWith(concat);
    }
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstStringPart(com.developmentontheedge.sql.model.AstStringPart)

Example 12 with AstFunNode

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

Example 13 with AstFunNode

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

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

the class FilterApplier method addWhere.

private void addWhere(AstWhere where, Map<ColumnRef, Object> conditions) {
    if (where.jjtGetNumChildren() != 0) {
        if (!AstFunNode.isFunction(DefaultParserContext.AND_LIT).test(where.child(0))) {
            AstFunNode and = DefaultParserContext.FUNC_AND.node();
            for (SimpleNode child : where.children()) and.addChild(child instanceof AstBooleanExpression ? new AstParenthesis(child) : child);
            where.removeChildren();
            where.addChild(and);
        }
        setConditions(where.child(0), conditions);
    } else if (conditions.size() > 1) {
        where.addChild(DefaultParserContext.FUNC_AND.node());
        setConditions(where.child(0), conditions);
    } else
        setConditions(where, conditions);
}
Also used : AstBooleanExpression(com.developmentontheedge.sql.model.AstBooleanExpression) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 15 with AstFunNode

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

the class GenericDbmsTransformer method transformExtractExpression.

protected void transformExtractExpression(AstExtract extract) {
    String dateField = extract.getDateField().toUpperCase();
    SimpleNode child = extract.child(0);
    if (AstFunNode.isFunction("age").test(child)) {
        SimpleNode parent = extract.jjtGetParent();
        if ("YEAR".equals(dateField)) {
            SimpleNode grandParent = parent.jjtGetParent();
            SimpleNode toReplace = extract;
            if (AstFunNode.isFunction("*").test(parent) && AstFunNode.isFunction("+").test(grandParent) && AstExtract.isExtract("MONTH").test(grandParent.other(parent))) {
                dateField = "MONTH";
                toReplace = parent.jjtGetParent();
            }
            toReplace.replaceWith(getDateTimeDiff(child.child(1), child.child(0), dateField));
        } else if ("MONTH".equals(dateField) && AstFunNode.isFunction("+").test(parent) && AstFunNode.isFunction("*").test(parent.child(1)) && parent.child(1).children().anyMatch(AstExtract.isExtract("YEAR"))) {
            parent.jjtGetParent().replaceWith(getDateTimeDiff(child.child(1), child.child(0), dateField));
        }
    } else if (child instanceof AstParenthesis && AstFunNode.isFunction("-").test(child.child(0)) && ("DAY".equals(dateField) || "EPOCH".equals(dateField))) {
        AstFunNode date = (AstFunNode) child.child(0);
        extract.replaceWith(getDateTimeDiff(date.child(1), date.child(0), "EPOCH".equals(dateField) ? "SECOND" : "DAY"));
    } else
        transformExtract(extract);
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Aggregations

AstFunNode (com.developmentontheedge.sql.model.AstFunNode)17 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)10 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)6 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)4 AstFrom (com.developmentontheedge.sql.model.AstFrom)4 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)4 AstSelect (com.developmentontheedge.sql.model.AstSelect)4 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)3 AstLimit (com.developmentontheedge.sql.model.AstLimit)3 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)3 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)3 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)3 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)3 AstWhere (com.developmentontheedge.sql.model.AstWhere)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstCase (com.developmentontheedge.sql.model.AstCase)2 AstCaseElse (com.developmentontheedge.sql.model.AstCaseElse)2 AstCast (com.developmentontheedge.sql.model.AstCast)2 AstExcept (com.developmentontheedge.sql.model.AstExcept)2 AstExtract (com.developmentontheedge.sql.model.AstExtract)2