Search in sources :

Example 31 with SimpleNode

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

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

Example 33 with SimpleNode

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

the class GenericDbmsTransformer method recursiveProcessing.

private void recursiveProcessing(SimpleNode node) {
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        SimpleNode child = node.child(i);
        if (child instanceof AstFunNode) {
            transformFunction((AstFunNode) child);
        }
        if (child instanceof AstSelect) {
            transformSelect((AstSelect) child);
        }
        if (child instanceof AstIdentifierConstant) {
            transformIdentifier((AstIdentifierConstant) child);
        }
        if (child instanceof AstStringConstant) {
            transformString((AstStringConstant) child);
        }
        if (child instanceof AstCast) {
            transformCastExpression((AstCast) child);
        }
        if (child instanceof AstExtract) {
            transformExtractExpression((AstExtract) child);
        }
        if (child instanceof AstPosition) {
            transformPosition((AstPosition) child);
        }
        if (child instanceof AstInterval) {
            transformInterval((AstInterval) child);
        }
        if (child instanceof AstWith) {
            transformWith((AstWith) child);
        }
        if (child instanceof AstExcept) {
            transformExcept((AstExcept) child);
        }
        recursiveProcessing(child);
    }
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstCast(com.developmentontheedge.sql.model.AstCast) AstPosition(com.developmentontheedge.sql.model.AstPosition) AstWith(com.developmentontheedge.sql.model.AstWith) AstExtract(com.developmentontheedge.sql.model.AstExtract) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstExcept(com.developmentontheedge.sql.model.AstExcept) AstInterval(com.developmentontheedge.sql.model.AstInterval) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 34 with SimpleNode

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

the class GenericDbmsTransformer method expandDbArguments.

private void expandDbArguments(SimpleNode node, Dbms dbms) {
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        SimpleNode child = node.child(i);
        expandDbArguments(child, dbms);
        if (child instanceof AstFunNode && DbSpecificFunction.needsTransformation(dbms).test(((AstFunNode) child).getFunction()))
            expandDbFunction((AstFunNode) child, dbms);
    }
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 35 with SimpleNode

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

the class GenericDbmsTransformer method transformCastExpression.

protected void transformCastExpression(AstCast cast) {
    SimpleNode diff = cast.child(0);
    // Detect date_trunc("MONTH", x) + (1 MONTH)::interval - (1 DAY)::interval and extract x
    Predicate<SimpleNode> truncMonth = AstFunNode.isFunction("date_trunc", AstStringConstant.isString("MONTH"), x -> true);
    Predicate<SimpleNode> truncMonthPlusMonth = AstFunNode.isFunction("+", truncMonth, AstInterval.isInterval("1 MONTH"));
    Predicate<SimpleNode> truncMonthPlusMonthMinusDay = AstFunNode.isFunction("-", truncMonthPlusMonth, AstInterval.isInterval("1 DAY"));
    if ("DATE".equals(cast.getDataType()) && truncMonthPlusMonthMinusDay.test(diff))
        transformLastDayPostgres(cast, diff.child(0).child(0).child(1));
    else
        transformCast(cast);
}
Also used : SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Aggregations

SimpleNode (com.developmentontheedge.sql.model.SimpleNode)39 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)12 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)11 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)10 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)10 Function (com.developmentontheedge.sql.model.Function)9 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)9 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)7 AstFrom (com.developmentontheedge.sql.model.AstFrom)5 AstInterval (com.developmentontheedge.sql.model.AstInterval)5 AstSelect (com.developmentontheedge.sql.model.AstSelect)5 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)5 AstCase (com.developmentontheedge.sql.model.AstCase)4 AstCaseElse (com.developmentontheedge.sql.model.AstCaseElse)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)4 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)4 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)4 AstCast (com.developmentontheedge.sql.model.AstCast)3 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)3 AstLimit (com.developmentontheedge.sql.model.AstLimit)3