Search in sources :

Example 1 with SimpleNode

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

the class DB2Transformer method transformDateTrunc.

@Override
protected void transformDateTrunc(AstFunNode node) {
    Function opConcat = DefaultParserContext.FUNC_MINUS;
    SimpleNode date = node.child(1);
    String field = ((AstStringConstant) node.child(0)).getValue();
    AstFirstDayOf month = new AstFirstDayOf(new AstParenthesis(opConcat.node(DAY.node(date), AstNumericConstant.of(1))), "DAYS");
    if (field.equalsIgnoreCase("'MONTH'"))
        node.replaceWith(opConcat.node(date, month));
    else {
        AstFirstDayOf year = new AstFirstDayOf(new AstParenthesis(opConcat.node(MONTH.node(date), AstNumericConstant.of(1))), "MONTHS");
        node.replaceWith(opConcat.node(date, opConcat.node(year, month)));
    }
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstFirstDayOf(com.developmentontheedge.sql.model.AstFirstDayOf)

Example 2 with SimpleNode

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

the class GenericDbmsTransformer method transformDateTimeDiff.

protected void transformDateTimeDiff(AstFunNode node, String format) {
    SimpleNode startDate = node.child(node.jjtGetNumChildren() - 2);
    SimpleNode endDate = node.child(node.jjtGetNumChildren() - 1);
    node.replaceWith(getDateTimeDiff(startDate, endDate, format));
}
Also used : SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 3 with SimpleNode

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

the class GenericDbmsTransformer method expandDbFunction.

private void expandDbFunction(AstFunNode node, Dbms dbms) {
    SimpleNode replacement = ((DbSpecificFunction) node.getFunction()).getReplacement(node, dbms);
    expandDbArguments(replacement, dbms);
    node.replaceWith(replacement);
}
Also used : DbSpecificFunction(com.developmentontheedge.sql.model.DbSpecificFunction) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 4 with SimpleNode

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

the class GenericDbmsTransformer method transformInterval.

private void transformInterval(AstInterval interval) {
    SimpleNode mul = interval.jjtGetParent();
    if (AstFunNode.isFunction("*").test(mul)) {
        String type = interval.getLiteral();
        String fn;
        switch(type) {
            case "1 MONTH":
                fn = "ADD_MONTHS";
                break;
            case "1 DAYS":
            case "1 DAY":
                fn = "ADD_DAYS";
                break;
            case "1 MILLISECOND":
                fn = "ADD_MILLIS";
                break;
            default:
                throw new IllegalStateException("Unsupported interval format: " + interval.format());
        }
        SimpleNode content = mul.other(interval);
        if (content instanceof AstParenthesis)
            content = ((AstParenthesis) content).child(0);
        SimpleNode add = mul.jjtGetParent();
        if (!AstFunNode.isFunction("+").test(add))
            throw new IllegalStateException("Interval grandparent is " + add + ", expected addition");
        SimpleNode date = add.other(mul);
        AstFunNode addFunction = parserContext.getFunction(fn).node(date, content);
        date.pullUpPrefix();
        SimpleNode toReplace = add;
        if (toReplace.jjtGetParent() instanceof AstParenthesis)
            toReplace = toReplace.jjtGetParent();
        toReplace.replaceWith(addFunction);
        transformDateAdd(addFunction);
    }
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 5 with SimpleNode

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

the class MacroExpander method expandMacros.

public void expandMacros(SimpleNode node) {
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        SimpleNode child = node.child(i);
        expandMacros(child);
        if ((child instanceof AstFunNode) && ((AstFunNode) child).getFunction() instanceof BeMacroFunction) {
            transformMacroFunction((AstFunNode) child);
        }
    }
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) BeMacroFunction(com.developmentontheedge.sql.model.BeMacroFunction) 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