Search in sources :

Example 1 with AstFrom

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

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

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

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

Example 5 with AstFrom

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

the class OracleTransformer method transformSelect.

@Override
protected void transformSelect(AstSelect select) {
    super.transformSelect(select);
    AstLimit limit = select.getLimit();
    if (limit != null) {
        select.dropLimit();
        SimpleNode parent = select.jjtGetParent();
        int idx = parent.indexOf(select);
        AstTableRef tableRef = new AstTableRef(select);
        AstFrom from = new AstFrom(tableRef);
        AstWhere where = new AstWhere();
        AstFunNode less = parserContext.getFunction("<=").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getLimit() + limit.getOffset()));
        where.addChild(less);
        if (limit.getOffset() == 0) {
            AstSelect newSelect = new AstSelect(new AstSelectList(), from, where);
            parent.jjtAddChild(newSelect, idx);
        } else {
            AstSelectList list = new AstSelectList();
            list.addChild(new AstFieldReference(new AstIdentifierConstant("tmp"), new AstIdentifierConstant("*,")));
            list.addChild(new AstIdentifierConstant("ROWNUM rn"));
            tableRef.addChild(new AstIdentifierConstant("tmp"));
            AstSelect innerSelect = new AstSelect(list, from, where);
            AstWhere outerWhere = new AstWhere();
            AstFunNode more = parserContext.getFunction(">").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getOffset()));
            outerWhere.addChild(more);
            AstSelect outerSelect = new AstSelect(select.getSelectList(), new AstFrom(new AstTableRef(innerSelect)), outerWhere);
            parent.jjtAddChild(outerSelect, idx);
        }
    }
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Aggregations

AstFrom (com.developmentontheedge.sql.model.AstFrom)9 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)8 AstSelect (com.developmentontheedge.sql.model.AstSelect)8 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)8 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)8 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)5 AstWhere (com.developmentontheedge.sql.model.AstWhere)5 AstLimit (com.developmentontheedge.sql.model.AstLimit)4 AstQuery (com.developmentontheedge.sql.model.AstQuery)4 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)3 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)2 AstWindowSpecification (com.developmentontheedge.sql.model.AstWindowSpecification)2 AstColumnList (com.developmentontheedge.sql.model.AstColumnList)1 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)1 AstValues (com.developmentontheedge.sql.model.AstValues)1 Function (com.developmentontheedge.sql.model.Function)1