Search in sources :

Example 1 with AstInterval

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

the class MySqlTransformer method transformDateAdd.

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

Example 2 with AstInterval

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

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

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

Aggregations

AstInterval (com.developmentontheedge.sql.model.AstInterval)4 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)4 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)3 Function (com.developmentontheedge.sql.model.Function)3 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)3 AstCast (com.developmentontheedge.sql.model.AstCast)2 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)2 AstDateAdd (com.developmentontheedge.sql.model.AstDateAdd)1 AstExcept (com.developmentontheedge.sql.model.AstExcept)1 AstExtract (com.developmentontheedge.sql.model.AstExtract)1 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)1 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)1 AstPosition (com.developmentontheedge.sql.model.AstPosition)1 AstSelect (com.developmentontheedge.sql.model.AstSelect)1 AstWith (com.developmentontheedge.sql.model.AstWith)1