Search in sources :

Example 1 with Function

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

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

the class SqlServerTransformer method transformLeastGreatest.

@Override
protected void transformLeastGreatest(AstFunNode node) {
    String name = node.getFunction().getName();
    AstIdentifierConstant v = new AstIdentifierConstant("V");
    Function func;
    if ("LEAST".equals(name))
        func = parserContext.getFunction("min");
    else
        func = parserContext.getFunction("max");
    AstValues values = new AstValues(0);
    for (SimpleNode child : node.children()) values.addChild(new AstParenthesis(child));
    AstTableRef tableRef = new AstTableRef(new AstParenthesis(values), new AstIdentifierConstant(name), new AstColumnList(v));
    node.replaceWith(new AstParenthesis(new AstSelect(new AstSelectList(func.node(v)), new AstFrom(tableRef))));
}
Also used : AstValues(com.developmentontheedge.sql.model.AstValues) AstSelect(com.developmentontheedge.sql.model.AstSelect) Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstColumnList(com.developmentontheedge.sql.model.AstColumnList) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 3 with Function

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

the class SqlServerTransformer method transformLpad.

@Override
protected void transformLpad(AstFunNode node) {
    Function opConcat = DefaultParserContext.FUNC_PLUS;
    SimpleNode str = node.child(0);
    SimpleNode fill = node.child(2);
    SimpleNode size = node.child(1);
    SimpleNode rsize = size.clone();
    node.replaceWith(RIGHT.node(opConcat.node(REPLICATE.node(fill, size), str), rsize));
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 4 with Function

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

the class SqlServerTransformer method transformLastDay.

protected void transformLastDay(SimpleNode node, SimpleNode date) {
    Function opConcat = DefaultParserContext.FUNC_PLUS;
    SimpleNode dateDiff = DATEDIFF.node(new AstIdentifierConstant("MONTH"), AstNumericConstant.of(0), date);
    SimpleNode dateAdd = DATEADD.node(new AstIdentifierConstant("MONTH"), opConcat.node(dateDiff, AstNumericConstant.of(1)), AstNumericConstant.of(0));
    SimpleNode expr = DATEADD.node(new AstIdentifierConstant("DAY"), AstNumericConstant.of(-1), dateAdd);
    node.replaceWith(CONVERT.node(new AstIdentifierConstant("DATE"), expr, AstNumericConstant.of(104)));
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 5 with Function

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

Aggregations

Function (com.developmentontheedge.sql.model.Function)9 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)9 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)8 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)6 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)5 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)4 AstInterval (com.developmentontheedge.sql.model.AstInterval)3 AstCast (com.developmentontheedge.sql.model.AstCast)2 AstFirstDayOf (com.developmentontheedge.sql.model.AstFirstDayOf)2 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)2 AstColumnList (com.developmentontheedge.sql.model.AstColumnList)1 AstDateAdd (com.developmentontheedge.sql.model.AstDateAdd)1 AstFrom (com.developmentontheedge.sql.model.AstFrom)1 AstSelect (com.developmentontheedge.sql.model.AstSelect)1 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)1 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)1 AstValues (com.developmentontheedge.sql.model.AstValues)1