Search in sources :

Example 21 with SimpleNode

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

the class OracleTransformer method transformSelect.

@Override
protected void transformSelect(AstSelect select) {
    super.transformSelect(select);
    AstLimit limit = select.getLimit();
    if (limit != null) {
        select.dropLimit();
        SimpleNode parent = select.jjtGetParent();
        int idx = parent.indexOf(select);
        AstTableRef tableRef = new AstTableRef(select);
        AstFrom from = new AstFrom(tableRef);
        AstWhere where = new AstWhere();
        AstFunNode less = parserContext.getFunction("<=").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getLimit() + limit.getOffset()));
        where.addChild(less);
        if (limit.getOffset() == 0) {
            AstSelect newSelect = new AstSelect(new AstSelectList(), from, where);
            parent.jjtAddChild(newSelect, idx);
        } else {
            AstSelectList list = new AstSelectList();
            list.addChild(new AstFieldReference(new AstIdentifierConstant("tmp"), new AstIdentifierConstant("*,")));
            list.addChild(new AstIdentifierConstant("ROWNUM rn"));
            tableRef.addChild(new AstIdentifierConstant("tmp"));
            AstSelect innerSelect = new AstSelect(list, from, where);
            AstWhere outerWhere = new AstWhere();
            AstFunNode more = parserContext.getFunction(">").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getOffset()));
            outerWhere.addChild(more);
            AstSelect outerSelect = new AstSelect(select.getSelectList(), new AstFrom(new AstTableRef(innerSelect)), outerWhere);
            parent.jjtAddChild(outerSelect, idx);
        }
    }
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 22 with SimpleNode

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

the class OracleTransformer method transformDateAdd.

@Override
protected void transformDateAdd(AstFunNode node) {
    SimpleNode date = node.child(0);
    SimpleNode number = node.child(1);
    String name = node.getFunction().getName();
    if (name.equalsIgnoreCase("add_days"))
        node.replaceWith(new AstParenthesis(DefaultParserContext.FUNC_PLUS.node(new AstParenthesis(date), new AstParenthesis(number))));
    if (name.equalsIgnoreCase("add_millis"))
        node.replaceWith(new AstParenthesis(DefaultParserContext.FUNC_PLUS.node(date, new AstParenthesis(DefaultParserContext.FUNC_DIVIDE.node(number, AstNumericConstant.of(86400000))))));
}
Also used : AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 23 with SimpleNode

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

the class PostgreSqlTransformer method transformLastDay.

@Override
protected void transformLastDay(AstFunNode node) {
    Function opPlus = DefaultParserContext.FUNC_PLUS;
    Function opMinus = DefaultParserContext.FUNC_MINUS;
    SimpleNode dateTrunc = parserContext.getFunction("date_trunc").node(new AstStringConstant("MONTH"), node.child(0));
    AstInterval monthInterval = new AstInterval(new AstStringConstant("1 MONTH"));
    AstInterval dayInterval = new AstInterval(new AstStringConstant("1 DAY"));
    SimpleNode expr = opPlus.node(dateTrunc, opMinus.node(monthInterval, dayInterval));
    node.replaceWith(new AstCast(expr, "DATE"));
}
Also used : PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) Function(com.developmentontheedge.sql.model.Function) AstCast(com.developmentontheedge.sql.model.AstCast) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstInterval(com.developmentontheedge.sql.model.AstInterval) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 24 with SimpleNode

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

the class PostgreSqlTransformer method transformDateAdd.

@Override
protected void transformDateAdd(AstFunNode node) {
    Function opPlus = DefaultParserContext.FUNC_PLUS;
    Function opTimes = DefaultParserContext.FUNC_TIMES;
    SimpleNode date = node.child(0);
    SimpleNode number = new AstParenthesis(node.child(1));
    String name = node.getFunction().getName();
    String type = name.equalsIgnoreCase("add_months") ? "1 MONTH" : name.equalsIgnoreCase("add_days") ? "1 DAY" : "1 MILLISECOND";
    AstInterval interval = new AstInterval(new AstStringConstant(type));
    node.replaceWith(new AstParenthesis(opPlus.node(date, opTimes.node(interval, number))));
}
Also used : PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) Function(com.developmentontheedge.sql.model.Function) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) AstInterval(com.developmentontheedge.sql.model.AstInterval) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 25 with SimpleNode

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

the class Simplifier method simplifyNot.

private static void simplifyNot(AstBooleanNot node) {
    node.children().toList().forEach(Simplifier::simplifyBoolean);
    if (node.jjtGetNumChildren() == 1) {
        SimpleNode child = node.child(0);
        String childStr = child.format().trim();
        if (childStr.equals("TRUE"))
            child.replaceWith(new AstIdentifierConstant("FALSE"));
        else if (childStr.equals("FALSE"))
            child.replaceWith(new AstIdentifierConstant("TRUE"));
    }
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) 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