Search in sources :

Example 1 with AstStringConstant

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

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

the class MySqlTransformer method transformDateTrunc.

@Override
protected void transformDateTrunc(AstFunNode node) {
    AstStringConstant child = (AstStringConstant) node.child(0);
    String dateformat = child.getValue().equalsIgnoreCase("'MONTH'") ? "%Y-%m-01" : "%Y-01-01";
    node.replaceWith(parserContext.getFunction("date_format").node(node.child(1), new AstStringConstant(dateformat)));
}
Also used : AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant)

Example 3 with AstStringConstant

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

the class ContextApplier method applySessionTag.

private void applySessionTag(AstBeSessionTag child) {
    String name = child.getName();
    Object value = context.getSessionVariable(name);
    if (value == null) {
        if (child.getDefault() != null) {
            value = SqlTypeUtils.parseValue(child.getDefault(), child.getType());
        } else {
            value = "";
        }
    }
    SimpleNode replacement;
    // TODO: support refColumn; smart quoting - in server module
    if (child.jjtGetParent() instanceof AstStringConstant) {
        replacement = new AstStringPart(value.toString());
    } else {
        if (SqlTypeUtils.isNumber(value.getClass())) {
            replacement = AstNumericConstant.of((Number) value);
        } else {
            replacement = new AstStringConstant(value.toString());
        }
    }
    child.replaceWith(replacement);
}
Also used : AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 4 with AstStringConstant

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

the class SqlServerTransformer method transformDateTrunc.

@Override
protected void transformDateTrunc(AstFunNode node) {
    AstStringConstant child = (AstStringConstant) node.child(0);
    String dateformat = child.getValueUnescaped();
    AstIdentifierConstant datepart = new AstIdentifierConstant(dateformat);
    AstNumericConstant date = AstNumericConstant.of(0);
    node.replaceWith(DATEADD.node(datepart, DATEDIFF.node(datepart, date, node.child(1)), date));
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstNumericConstant(com.developmentontheedge.sql.model.AstNumericConstant) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant)

Example 5 with AstStringConstant

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

the class SqlServerTransformer method transformDateFormat.

@Override
protected void transformDateFormat(AstFunNode node, DateFormat df) {
    Number length, codePage;
    switch(df) {
        case FORMAT_DATE:
            length = 10;
            codePage = 120;
            break;
        case FORMAT_DATETIME:
            length = 19;
            codePage = 120;
            break;
        case FORMAT_DATE_RUS:
            length = 10;
            codePage = 104;
            break;
        case FORMAT_DATE_RUS_SHORT:
            length = 8;
            codePage = 4;
            break;
        case FORMAT_MONTHYEAR:
            length = 4;
            codePage = 120;
            break;
        case FORMAT_FMDAYMONTH:
        case FORMAT_DAYMONTH:
            length = 5;
            codePage = 4;
            break;
        case FORMAT_HOURMINUTE:
            length = 5;
            codePage = 114;
            break;
        case FORMAT_YYYYMMDD:
            length = 8;
            codePage = 112;
            break;
        case FORMAT_YYYYMM:
            length = 6;
            codePage = 112;
            break;
        case FORMAT_DAY_OF_WEEK:
            node.replaceWith(DATEPART.node(new AstIdentifierConstant("dw"), node.child(0)));
            return;
        default:
            SimpleNode rtrim = RTRIM.node(parserContext.getFunction(df.name()).node(node.child(0)));
            SimpleNode padded = DefaultParserContext.FUNC_PLUS.node(new AstStringConstant("0"), rtrim);
            node.replaceWith(RIGHT.node(padded, AstNumericConstant.of(df == DateFormat.YEAR ? 4 : 2)));
            return;
    }
    SimpleNode replacement = CONVERT.node(VARCHAR.node(AstNumericConstant.of(length)), node.child(0), AstNumericConstant.of(codePage));
    if (DateFormat.FORMAT_MONTHYEAR.equals(df))
        replacement = DefaultParserContext.FUNC_PLUS.node(DATENAME.node(new AstIdentifierConstant("month"), node.child(0)), new AstStringConstant(" "), replacement);
    node.replaceWith(replacement);
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Aggregations

AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)15 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)10 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)5 Function (com.developmentontheedge.sql.model.Function)4 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)4 AstCast (com.developmentontheedge.sql.model.AstCast)3 AstInterval (com.developmentontheedge.sql.model.AstInterval)3 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)3 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)2 AstBeListPlaceHolder (com.developmentontheedge.sql.model.AstBeListPlaceHolder)1 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)1 AstBeSqlVar (com.developmentontheedge.sql.model.AstBeSqlVar)1 AstExcept (com.developmentontheedge.sql.model.AstExcept)1 AstExtract (com.developmentontheedge.sql.model.AstExtract)1 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)1 AstFirstDayOf (com.developmentontheedge.sql.model.AstFirstDayOf)1 AstInValueList (com.developmentontheedge.sql.model.AstInValueList)1 AstNumericConstant (com.developmentontheedge.sql.model.AstNumericConstant)1 AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)1