Search in sources :

Example 6 with AstQuery

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

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

the class OrderByFilter method apply.

public void apply(AstQuery query, Map<String, String> columns) {
    AstOrderBy orderBy = new AstOrderBy();
    apply(columns, query, orderBy);
    query.children().select(AstSelect.class).forEach(AstSelect::dropOrder);
    AstSelect select;
    if (query.jjtGetNumChildren() == 1)
        select = (AstSelect) query.child(0);
    else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        query.replaceWith(new AstQuery(select));
    }
    select.addChild(orderBy);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) 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 8 with AstQuery

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

the class CountApplier method transform.

public void transform(ParserContext ctx, AstStart ast) {
    AstQuery query = ast.getQuery();
    query.children().select(AstSelect.class).forEach(AstSelect::dropOrder);
    if (query.jjtGetNumChildren() > 1)
        throw new UnsupportedOperationException("UNION queries are not supported for COUNT");
    AstSelect select = query.children().select(AstSelect.class).findFirst().get();
    AstSelectList selectList = select.getSelectList();
    selectList.removeChildren();
    AstCount countFn = new AstCount();
    AstDerivedColumn countNode = new AstDerivedColumn(countFn, "CNT");
    countNode.setAsToken(true);
    selectList.addChild(countNode);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstCount(com.developmentontheedge.sql.model.AstCount) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn)

Example 9 with AstQuery

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

the class Formatter method format.

public String format(AstQuery start, Context context, ParserContext parserContext) {
    DbmsTransformer dbmsTransformer = context.getDbmsTransformer();
    dbmsTransformer.setParserContext(parserContext);
    AstQuery clone = start.clone();
    dbmsTransformer.transformQuery(clone);
    return clone.format();
}
Also used : AstQuery(com.developmentontheedge.sql.model.AstQuery)

Aggregations

AstQuery (com.developmentontheedge.sql.model.AstQuery)9 AstSelect (com.developmentontheedge.sql.model.AstSelect)8 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)6 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)6 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)5 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)5 AstFrom (com.developmentontheedge.sql.model.AstFrom)4 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)3 AstLimit (com.developmentontheedge.sql.model.AstLimit)2 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)2 AstWhere (com.developmentontheedge.sql.model.AstWhere)2 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)1 FilterHelper (com.developmentontheedge.be5.api.helpers.FilterHelper)1 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)1 UserInfoHolder (com.developmentontheedge.be5.api.helpers.UserInfoHolder)1 DatabaseService (com.developmentontheedge.be5.api.services.DatabaseService)1 Meta (com.developmentontheedge.be5.api.services.Meta)1 SqlService (com.developmentontheedge.be5.api.services.SqlService)1 DpsRecordAdapter (com.developmentontheedge.be5.api.sql.DpsRecordAdapter)1 ResultSetParser (com.developmentontheedge.be5.api.sql.ResultSetParser)1