Search in sources :

Example 1 with AstQuery

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

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

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

the class ColumnAdder method addColumn.

/**
 * Add a column to the main query with given alias. Do nothing if something with given alias already present
 * (even if something else is in the columnName).
 *
 * @param ast AST to modify (clone it if necessary)
 * @param tableName tableName to look for
 * @param columnName column name to add to select list
 * @param alias alias to use/search for
 * @throws IllegalArgumentException if given AST has no such table in the FROM field
 */
public void addColumn(AstStart ast, String tableName, String columnName, String alias) {
    AstQuery query = ast.getQuery();
    AstSelect firstSelect = query.children().select(AstSelect.class).findFirst().get();
    if (firstSelect.getSelectList().children().select(AstDerivedColumn.class).anyMatch(dc -> alias.equalsIgnoreCase(dc.getAlias())))
        return;
    query.children().select(AstSelect.class).forEach(select -> processSelect(select, tableName, columnName, alias));
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn)

Example 4 with AstQuery

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

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

the class Be5QueryExecutor method hasColumnWithLabel.

private boolean hasColumnWithLabel(AstStart ast, String idColumnLabel) {
    AstQuery query = ast.getQuery();
    Optional<AstSelect> selectOpt = query.children().select(AstSelect.class).collect(MoreCollectors.onlyOne());
    if (!selectOpt.isPresent())
        return false;
    AstSelect select = selectOpt.get();
    return select.getSelectList().children().select(AstDerivedColumn.class).map(AstDerivedColumn::getAlias).nonNull().map(alias -> alias.replaceFirst("^\"(.+)\"$", "$1")).has(idColumnLabel);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) Connection(java.sql.Connection) DynamicPropertySet(com.developmentontheedge.beans.DynamicPropertySet) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) Meta(com.developmentontheedge.be5.api.services.Meta) Query(com.developmentontheedge.be5.metadata.model.Query) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) ColumnAdder(com.developmentontheedge.sql.format.ColumnAdder) ResultSetParser(com.developmentontheedge.be5.api.sql.ResultSetParser) UserInfoHolder(com.developmentontheedge.be5.api.helpers.UserInfoHolder) Map(java.util.Map) AstLimit(com.developmentontheedge.sql.model.AstLimit) Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) Context(com.developmentontheedge.sql.format.Context) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) MoreCollectors(one.util.streamex.MoreCollectors) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) Logger(java.util.logging.Logger) EntityModel(com.developmentontheedge.be5.databasemodel.EntityModel) Objects(java.util.Objects) List(java.util.List) QueryType(com.developmentontheedge.be5.metadata.QueryType) LimitsApplier(com.developmentontheedge.sql.format.LimitsApplier) Simplifier(com.developmentontheedge.sql.format.Simplifier) AstBeParameterTag(com.developmentontheedge.sql.model.AstBeParameterTag) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) StreamEx(one.util.streamex.StreamEx) DatabaseConstants(com.developmentontheedge.be5.metadata.DatabaseConstants) Optional(java.util.Optional) FilterHelper(com.developmentontheedge.be5.api.helpers.FilterHelper) RecordModel(com.developmentontheedge.be5.databasemodel.RecordModel) ResultSetMetaData(java.sql.ResultSetMetaData) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstQuery(com.developmentontheedge.sql.model.AstQuery) DatabaseService(com.developmentontheedge.be5.api.services.DatabaseService) QueryContext(com.developmentontheedge.sql.format.QueryContext) HashMap(java.util.HashMap) AstStart(com.developmentontheedge.sql.model.AstStart) Token(com.developmentontheedge.sql.model.Token) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Entity(com.developmentontheedge.be5.metadata.model.Entity) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Injector(com.developmentontheedge.be5.env.Injector) DynamicPropertySetSupport(com.developmentontheedge.beans.DynamicPropertySetSupport) ParserContext(com.developmentontheedge.sql.model.ParserContext) AstSelect(com.developmentontheedge.sql.model.AstSelect) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) SqlService(com.developmentontheedge.be5.api.services.SqlService) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) DatabaseModel(com.developmentontheedge.be5.databasemodel.impl.DatabaseModel) DpsRecordAdapter(com.developmentontheedge.be5.api.sql.DpsRecordAdapter) Ast(com.developmentontheedge.sql.format.Ast) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstNumericConstant(com.developmentontheedge.sql.model.AstNumericConstant) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Collections(java.util.Collections) 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