Search in sources :

Example 6 with CompiledQuery

use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.

the class MongoDatabaseImpl method _query.

private Object _query(String type, Query.Construct construct, final Query query) throws DatabaseException {
    if (query == null) {
        return null;
    }
    boolean queryHasEntity = true;
    String entity = query.entity();
    if (Lang.isNullOrEmpty(entity)) {
        queryHasEntity = false;
        entity = type;
    }
    entity = checkNotNull(entity);
    tracer.log(Tracer.Level.Debug, "Query Entity {0}", entity);
    String cacheKey = construct.name() + query.name();
    BasicDBObject mQuery = null;
    Map<String, Object> bindings = query.bindings();
    boolean cacheable = queryHasEntity && query.caching().cache(Target.meta) && !Lang.isNullOrEmpty(query.name());
    if (cacheable) {
        mQuery = (BasicDBObject) QueriesCache.get(cacheKey);
        tracer.log(Tracer.Level.Debug, "Query meta loaded from cache {0}", mQuery);
    }
    if (mQuery == null) {
        CompiledQuery cQuery = compile(entity, construct, query);
        mQuery = (BasicDBObject) cQuery.query();
        bindings = cQuery.bindings();
        if (cacheable && mQuery != null) {
            QueriesCache.put(cacheKey, mQuery);
            tracer.log(Tracer.Level.Debug, "Query meta stored in cache {0}", mQuery);
        }
    }
    mQuery = cacheable ? applyBindings(mQuery, bindings) : mQuery;
    tracer.log(Tracer.Level.Debug, "       Query {0}", mQuery);
    tracer.log(Tracer.Level.Debug, "    Bindings {0}", bindings);
    if (Query.Construct.select.equals(construct)) {
        FindIterable<Document> cursor = db.getCollection(entity).find(mQuery);
        // start / skip
        if (query.start() > 0) {
            cursor.skip(query.start());
        }
        // count / limit
        if (query.count() > 0) {
            cursor.limit(query.count());
        }
        // orderBy
        OrderBy orderBy = query.orderBy();
        if (orderBy != null && orderBy.count() > 0) {
            List<Bson> sorts = new ArrayList<Bson>();
            Iterator<String> oFields = orderBy.fields();
            while (oFields.hasNext()) {
                String field = oFields.next();
                OrderByField of = orderBy.get(field);
                Bson sort = null;
                if (of.direction().equals(Direction.asc)) {
                    sort = Sorts.ascending(field);
                } else {
                    sort = Sorts.descending(field);
                }
                sorts.add(sort);
            }
            cursor.sort(Sorts.orderBy(sorts));
        }
        Select select = query.select();
        if (select != null && select.count() > 0) {
            String[] pFields = new String[select.count()];
            for (int i = 0; i < select.count(); i++) {
                System.out.println(select.get(i));
                pFields[i] = select.get(i);
            }
            cursor.projection(Projections.include(pFields));
        }
        return cursor;
    } else if (Query.Construct.delete.equals(construct)) {
        return db.getCollection(entity).deleteMany(mQuery);
    }
    return null;
}
Also used : OrderBy(com.bluenimble.platform.db.query.OrderBy) ArrayList(java.util.ArrayList) Document(org.bson.Document) Bson(org.bson.conversions.Bson) BasicDBObject(com.mongodb.BasicDBObject) OrderByField(com.bluenimble.platform.db.query.OrderByField) Select(com.bluenimble.platform.db.query.Select) BasicDBObject(com.mongodb.BasicDBObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery)

Example 7 with CompiledQuery

use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.

the class TestStartPageQueryCompiler method main.

public static void main(String[] args) throws Exception {
    Query query = new JsonQuery(Json.load(new File("tests/queries/complete.json")));
    System.out.println("Select==>");
    QueryCompiler sc = new SqlQueryCompiler(Query.Construct.select) {

        private static final long serialVersionUID = -1248971549807669897L;

        @Override
        protected void onQuery(Timing timing, Query query) throws DatabaseException {
            super.onQuery(timing, query);
            if (Timing.start.equals(timing)) {
                return;
            }
            if (query.start() > 0) {
                buff.append(Lang.SPACE).append("skip").append(Lang.SPACE).append(query.start());
            }
            if (query.count() > 0) {
                buff.append(Lang.SPACE).append("limit").append(Lang.SPACE).append(query.count());
            }
        }
    };
    CompiledQuery cq = sc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
    System.out.println("Delete==>");
    QueryCompiler dc = new SqlQueryCompiler(Query.Construct.delete);
    cq = dc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
}
Also used : CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) Query(com.bluenimble.platform.db.query.Query) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) QueryCompiler(com.bluenimble.platform.db.query.QueryCompiler) File(java.io.File) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery)

Example 8 with CompiledQuery

use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.

the class TestQueryCompiler method main.

public static void main(String[] args) throws Exception {
    Query query = new JsonQuery(Json.load(new File("tests/queries/simple.json")));
    System.out.println("Select==>");
    QueryCompiler sc = new SqlQueryCompiler(Query.Construct.select);
    CompiledQuery cq = sc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
    System.out.println("Delete==>");
    QueryCompiler dc = new SqlQueryCompiler(Query.Construct.delete);
    cq = dc.compile(query);
    System.out.println("   query: " + cq.query());
    System.out.println();
    System.out.println("bindings: " + cq.bindings());
}
Also used : CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) Query(com.bluenimble.platform.db.query.Query) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) QueryCompiler(com.bluenimble.platform.db.query.QueryCompiler) File(java.io.File) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery)

Aggregations

CompiledQuery (com.bluenimble.platform.db.query.CompiledQuery)8 Query (com.bluenimble.platform.db.query.Query)5 QueryCompiler (com.bluenimble.platform.db.query.QueryCompiler)5 SqlQueryCompiler (com.bluenimble.platform.db.query.impls.SqlQueryCompiler)5 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)4 File (java.io.File)4 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)3 JsonObject (com.bluenimble.platform.json.JsonObject)3 Select (com.bluenimble.platform.db.query.Select)2 BasicDBObject (com.mongodb.BasicDBObject)2 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)2 Condition (com.bluenimble.platform.db.query.Condition)1 OrderBy (com.bluenimble.platform.db.query.OrderBy)1 OrderByField (com.bluenimble.platform.db.query.OrderByField)1 Operator (com.bluenimble.platform.db.query.Query.Operator)1 Where (com.bluenimble.platform.db.query.Where)1 BetweenFilterAppender (com.bluenimble.platform.plugins.database.mongodb.impls.filters.BetweenFilterAppender)1 DefaultFilterAppender (com.bluenimble.platform.plugins.database.mongodb.impls.filters.DefaultFilterAppender)1 FilterAppender (com.bluenimble.platform.plugins.database.mongodb.impls.filters.FilterAppender)1 LikeFilterAppender (com.bluenimble.platform.plugins.database.mongodb.impls.filters.LikeFilterAppender)1