Search in sources :

Example 11 with AstSelect

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

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

the class Be5QueryExecutor method applySort.

private void applySort(DebugQueryLogger dql, AstStart ast) {
    if (sortColumn >= 0) {
        try {
            DynamicProperty[] schema = getSchema(new Formatter().format(ast, context, parserContext));
            int sortCol = getQuerySortingColumn(schema);
            if (sortCol > 0) {
                AstSelect sel = (AstSelect) ast.getQuery().jjtGetChild(ast.getQuery().jjtGetNumChildren() - 1);
                AstOrderBy orderBy = sel.getOrderBy();
                if (orderBy == null) {
                    orderBy = new AstOrderBy();
                    sel.addChild(orderBy);
                    AstLimit astLimit = sel.children().select(AstLimit.class).findFirst().orElse(null);
                    if (astLimit != null) {
                        sel.removeChild(astLimit);
                        sel.addChild(astLimit);
                    }
                }
                AstOrderingElement oe = new AstOrderingElement(AstNumericConstant.of(sortCol));
                if (sortDesc) {
                    oe.setDirectionToken(new Token(0, "DESC"));
                }
                orderBy.addChild(oe);
                orderBy.moveToFront(oe);
            }
            dql.log("With sort", ast);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstLimit(com.developmentontheedge.sql.model.AstLimit) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Token(com.developmentontheedge.sql.model.Token)

Example 13 with AstSelect

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

the class EntityModelBase method count.

@Override
public long count(Map<String, ? super Object> conditions) {
    Objects.requireNonNull(conditions);
    AstSelect sql = Ast.selectCount().from(entity.getName()).where(conditions);
    return db.getLong(sql.format(), conditions.values().toArray());
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect)

Example 14 with AstSelect

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

the class ProvincesRepository method findFirst.

@Override
public Provinces findFirst(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Provinces.class) Closure conditions) {
    Map<String, Object> conditionsMap = toMap(conditions);
    AstSelect sql = Ast.selectAll().from(entityName).where(conditionsMap);
    return db.query(sql.format(), beanHandler, conditionsMap.values().toArray());
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect)

Example 15 with AstSelect

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

the class ProvincesRepository method count.

public Long count(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Provinces.class) final Closure conditions) {
    Map<String, Object> conditionsMap = toMap(conditions);
    AstSelect sql = Ast.selectCount().from(entityName).where(conditionsMap);
    return db.getLong(sql.format(), conditionsMap.values().toArray());
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect)

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