Search in sources :

Example 1 with AstCast

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

the class MySqlTransformer method transformCastOracle.

@Override
protected void transformCastOracle(AstFunNode node) {
    String dataType;
    String name = node.getFunction().getName();
    if (name.equalsIgnoreCase("to_char"))
        dataType = "CHAR";
    else if (name.equalsIgnoreCase("to_number"))
        dataType = "SIGNED";
    else if (name.equalsIgnoreCase("to_date"))
        dataType = "DATE";
    else if (name.equalsIgnoreCase("to_key"))
        dataType = "UNSIGNED";
    else
        throw new IllegalArgumentException("name = " + name);
    node.replaceWith(new AstCast(node.child(0), dataType));
}
Also used : AstCast(com.developmentontheedge.sql.model.AstCast)

Example 2 with AstCast

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

the class PostgreSqlTransformer method transformCastOracle.

@Override
protected void transformCastOracle(AstFunNode node) {
    String dataType;
    String name = node.getFunction().getName();
    if (name.equalsIgnoreCase("to_char"))
        dataType = "VARCHAR";
    else if (name.equalsIgnoreCase("to_number") || name.equalsIgnoreCase("to_key"))
        dataType = "BIGINT";
    else
        throw new IllegalArgumentException("name = " + name);
    node.replaceWith(new AstCast(node.child(0), dataType));
}
Also used : AstCast(com.developmentontheedge.sql.model.AstCast)

Example 3 with AstCast

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

the class OracleTransformer method getDateTimeDiff.

@Override
protected SimpleNode getDateTimeDiff(SimpleNode startDate, SimpleNode endDate, String format) {
    Function trunc = parserContext.getFunction("trunc");
    AstParenthesis dateDifference = new AstParenthesis(DefaultParserContext.FUNC_MINUS.node(new AstCast(endDate, "DATE"), new AstCast(startDate, "DATE")));
    PredefinedFunction opTimes = DefaultParserContext.FUNC_TIMES;
    switch(format) {
        case "SECOND":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24 * 60 * 60)));
        case "MINUTE":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24 * 60)));
        case "HOUR":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24)));
        case "DAY":
            return trunc.node(dateDifference);
        case "MONTH":
            return MONTHS_BETWEEN.node(trunc.node(endDate, new AstStringConstant(format)), trunc.node(startDate, new AstStringConstant(format)));
        case "YEAR":
            PredefinedFunction opDivide = DefaultParserContext.FUNC_DIVIDE;
            return parserContext.getFunction("floor").node(opDivide.node(MONTHS_BETWEEN.node(endDate, startDate), AstNumericConstant.of(12)));
        default:
            throw new IllegalStateException("Unsupported value for datepart in TIMESTAMPDIFF: " + format);
    }
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstCast(com.developmentontheedge.sql.model.AstCast) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction)

Example 4 with AstCast

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

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

AstCast (com.developmentontheedge.sql.model.AstCast)5 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)3 AstInterval (com.developmentontheedge.sql.model.AstInterval)2 Function (com.developmentontheedge.sql.model.Function)2 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)2 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)2 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 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)1 AstPosition (com.developmentontheedge.sql.model.AstPosition)1 AstSelect (com.developmentontheedge.sql.model.AstSelect)1 AstWith (com.developmentontheedge.sql.model.AstWith)1