Search in sources :

Example 6 with AstIdentifierConstant

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

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

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

Example 9 with AstIdentifierConstant

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

the class Be5QueryExecutor method countFromQuery.

private void countFromQuery(AstQuery query) {
    AstSelect select = Ast.selectCount().from(AstTableRef.as(new AstParenthesis(query.clone()), new AstIdentifierConstant("data", true)));
    query.replaceWith(new AstQuery(select));
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Example 10 with AstIdentifierConstant

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

the class LimitsApplier method transformQuery.

public boolean transformQuery(AstQuery query) {
    if (query.jjtGetNumChildren() == 1) {
        AstSelect select = (AstSelect) query.child(0);
        if (select.getLimit() != null)
            return false;
        AstLimit limit = new AstLimit();
        limit.setLimit(offset, count);
        select.addChild(limit);
    } else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        AstSelect select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        AstLimit limit = new AstLimit();
        limit.setLimit(offset, count);
        select.addChild(limit);
        query.replaceWith(new AstQuery(select));
    }
    // TODO: support offset, union, etc.
    return true;
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Aggregations

AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)19 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)11 AstSelect (com.developmentontheedge.sql.model.AstSelect)10 AstFrom (com.developmentontheedge.sql.model.AstFrom)8 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)8 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)8 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)6 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)5 AstQuery (com.developmentontheedge.sql.model.AstQuery)5 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)5 AstWhere (com.developmentontheedge.sql.model.AstWhere)5 AstLimit (com.developmentontheedge.sql.model.AstLimit)4 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)2 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)2 AstWindowSpecification (com.developmentontheedge.sql.model.AstWindowSpecification)2 Function (com.developmentontheedge.sql.model.Function)2 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)2