Search in sources :

Example 16 with AstSelect

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

Example 17 with AstSelect

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

the class EntityModelBase method getColumns.

@Override
public RecordModel getColumns(List<String> columns, Map<String, ? super Object> conditions) {
    Objects.requireNonNull(conditions);
    AstSelect sql = Ast.select(addPrimaryKeyColumnIfNotEmpty(columns)).from(entity.getName()).where(conditions);
    DynamicPropertySet dps = db.select(sql.format(), rs -> {
        DynamicPropertySet newDps = new DynamicPropertySetSupport();
        dpsHelper.addDpWithoutTags(newDps, entity, rs);
        return newDps;
    }, conditions.values().toArray());
    return dps == null ? null : new RecordModelBase(this, dps);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) DynamicPropertySet(com.developmentontheedge.beans.DynamicPropertySet) DynamicPropertySetSupport(com.developmentontheedge.beans.DynamicPropertySetSupport)

Example 18 with AstSelect

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

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

Example 20 with AstSelect

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

Aggregations

AstSelect (com.developmentontheedge.sql.model.AstSelect)28 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)11 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)10 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)9 AstFrom (com.developmentontheedge.sql.model.AstFrom)8 AstQuery (com.developmentontheedge.sql.model.AstQuery)8 Test (org.junit.Test)8 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)7 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)6 AstLimit (com.developmentontheedge.sql.model.AstLimit)6 AstWhere (com.developmentontheedge.sql.model.AstWhere)5 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)5 HashMap (java.util.HashMap)5 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)3 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)3 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)2 DynamicPropertySet (com.developmentontheedge.beans.DynamicPropertySet)2 DynamicPropertySetSupport (com.developmentontheedge.beans.DynamicPropertySetSupport)2