Search in sources :

Example 1 with AstParenthesis

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

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

the class FilterApplier method addFilter.

public void addFilter(AstQuery query, Map<ColumnRef, Object> conditions) {
    if (conditions.size() == 0)
        return;
    AstWhere where = new AstWhere();
    if (query.jjtGetNumChildren() == 1) {
        AstSelect select = (AstSelect) query.child(0);
        if (select.getWhere() != null)
            where = select.getWhere();
        else
            select.where(where);
    } else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        AstSelect select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        select.where(where);
        query.replaceWith(new AstQuery(select));
    }
    addWhere(where, conditions);
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Example 3 with AstParenthesis

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

the class FilterApplier method setFilter.

public void setFilter(AstStart ast, Map<ColumnRef, Object> conditions) {
    AstQuery query = ast.getQuery();
    dropOldConditions(query);
    if (conditions.size() == 0)
        return;
    AstWhere where = new AstWhere();
    addWhere(where, conditions);
    if (query.jjtGetNumChildren() == 1)
        ((AstSelect) query.child(0)).where(where);
    else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        AstSelect select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        select.where(where);
        query.replaceWith(new AstQuery(select));
    }
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Example 4 with AstParenthesis

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

the class GenericDbmsTransformer method transformInterval.

private void transformInterval(AstInterval interval) {
    SimpleNode mul = interval.jjtGetParent();
    if (AstFunNode.isFunction("*").test(mul)) {
        String type = interval.getLiteral();
        String fn;
        switch(type) {
            case "1 MONTH":
                fn = "ADD_MONTHS";
                break;
            case "1 DAYS":
            case "1 DAY":
                fn = "ADD_DAYS";
                break;
            case "1 MILLISECOND":
                fn = "ADD_MILLIS";
                break;
            default:
                throw new IllegalStateException("Unsupported interval format: " + interval.format());
        }
        SimpleNode content = mul.other(interval);
        if (content instanceof AstParenthesis)
            content = ((AstParenthesis) content).child(0);
        SimpleNode add = mul.jjtGetParent();
        if (!AstFunNode.isFunction("+").test(add))
            throw new IllegalStateException("Interval grandparent is " + add + ", expected addition");
        SimpleNode date = add.other(mul);
        AstFunNode addFunction = parserContext.getFunction(fn).node(date, content);
        date.pullUpPrefix();
        SimpleNode toReplace = add;
        if (toReplace.jjtGetParent() instanceof AstParenthesis)
            toReplace = toReplace.jjtGetParent();
        toReplace.replaceWith(addFunction);
        transformDateAdd(addFunction);
    }
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 5 with AstParenthesis

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

Aggregations

AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)17 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)10 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)7 Function (com.developmentontheedge.sql.model.Function)7 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)7 AstFrom (com.developmentontheedge.sql.model.AstFrom)6 AstSelect (com.developmentontheedge.sql.model.AstSelect)6 AstQuery (com.developmentontheedge.sql.model.AstQuery)5 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)5 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)5 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)4 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)4 AstInterval (com.developmentontheedge.sql.model.AstInterval)3 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)3 AstCast (com.developmentontheedge.sql.model.AstCast)2 AstDateAdd (com.developmentontheedge.sql.model.AstDateAdd)2 AstFirstDayOf (com.developmentontheedge.sql.model.AstFirstDayOf)2 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)2 AstWhere (com.developmentontheedge.sql.model.AstWhere)2 AstBooleanExpression (com.developmentontheedge.sql.model.AstBooleanExpression)1