Search in sources :

Example 1 with CompiledQuery

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

the class TestWithBindingsQueryCompiler method main.

public static void main(String[] args) throws Exception {
    Map<String, Object> bindings = new HashMap<String, Object>();
    bindings.put("alpha", "alpha-val");
    bindings.put("beta", new Date());
    Query query = new JsonQuery(Json.load(new File("tests/queries/with-bindings.json")), bindings);
    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) HashMap(java.util.HashMap) 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) Date(java.util.Date) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery)

Example 2 with CompiledQuery

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

the class TestQueryCompilerWithOperators method main.

public static void main(String[] args) throws Exception {
    Query query = new JsonQuery(Json.load(new File("tests/queries/with-operators.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)

Example 3 with CompiledQuery

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

the class OrientDatabase method compile.

private CompiledQuery compile(String entity, Query.Construct construct, final Query query, final boolean returnBefore) throws DatabaseException {
    final String fEntity = entity;
    QueryCompiler compiler = new SqlQueryCompiler(construct) {

        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(Sql.Skip).append(Lang.SPACE).append(query.start());
            }
            if (query.count() > 0) {
                buff.append(Lang.SPACE).append(Sql.Limit).append(Lang.SPACE).append(query.count());
            }
        }

        @Override
        protected void onSelect(Timing timing, Select select) throws DatabaseException {
            super.onSelect(timing, select);
            if (Timing.end.equals(timing) && returnBefore) {
                buff.append(Lang.SPACE).append(Sql.ReturnBefore);
            }
        }

        @Override
        protected String operatorFor(Operator operator) {
            if (Operator.ftq.equals(operator)) {
                return Lucene;
            } else if (Operator.regex.equals(operator)) {
                return Maches;
            }
            return super.operatorFor(operator);
        }

        @Override
        protected void entity() {
            buff.append(fEntity);
        }
    };
    return compiler.compile(query);
}
Also used : Operator(com.bluenimble.platform.db.query.Query.Operator) Query(com.bluenimble.platform.db.query.Query) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) Select(com.bluenimble.platform.db.query.Select) SqlQueryCompiler(com.bluenimble.platform.db.query.impls.SqlQueryCompiler) QueryCompiler(com.bluenimble.platform.db.query.QueryCompiler)

Example 4 with CompiledQuery

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

the class OrientDatabase method _query.

private Object _query(String type, Query.Construct construct, final Query query, boolean returnBefore) throws DatabaseException {
    if (query == null) {
        return null;
    }
    if (Query.Construct.select.equals(construct)) {
        returnBefore = false;
    }
    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);
    if (!db.getMetadata().getSchema().existsClass(entity)) {
        tracer.log(Tracer.Level.Debug, "Entity {0} not found", entity);
        return null;
    }
    String cacheKey = construct.name() + query.name();
    String sQuery = null;
    Map<String, Object> bindings = query.bindings();
    if (queryHasEntity && query.caching().cache(Target.meta) && !Lang.isNullOrEmpty(query.name())) {
        sQuery = (String) QueriesCache.get(cacheKey);
        tracer.log(Tracer.Level.Debug, "Query meta loaded from cache {0}", sQuery);
    }
    if (sQuery == null) {
        CompiledQuery cQuery = compile(entity, construct, query, returnBefore);
        sQuery = (String) cQuery.query();
        bindings = cQuery.bindings();
        if (queryHasEntity && query.caching().cache(Target.meta) && !Lang.isNullOrEmpty(query.name())) {
            QueriesCache.put(cacheKey, sQuery);
            tracer.log(Tracer.Level.Debug, "Query meta stored in cache {0}", sQuery);
        }
    }
    tracer.log(Tracer.Level.Debug, "\tQuery {0}", sQuery);
    tracer.log(Tracer.Level.Debug, "\tBindings: {0}", bindings);
    if (Query.Construct.select.equals(construct)) {
        OSQLSynchQuery<ODocument> q = new OSQLSynchQuery<ODocument>(sQuery);
        List<ODocument> result = db.command(q).execute(bindings);
        if (result == null || result.isEmpty()) {
            return null;
        }
        return result;
    } else {
        return db.command(new OCommandSQL(sQuery)).execute(bindings);
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 5 with CompiledQuery

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

the class MongoDatabaseImpl method compile.

// REF: https://docs.mongodb.com/manual/reference/method/db.collection.find/
private CompiledQuery compile(String entity, Query.Construct construct, final Query query) {
    BasicDBObject mq = new BasicDBObject();
    CompiledQuery cQuery = new CompiledQuery() {

        @Override
        public Object query() {
            return mq;
        }

        @Override
        public Map<String, Object> bindings() {
            return query.bindings();
        }
    };
    // where
    Where where = query.where();
    if (where == null || where.count() == 0) {
        return cQuery;
    }
    // Selectors
    Iterator<String> fields = where.conditions();
    while (fields.hasNext()) {
        String f = fields.next();
        Object condOrFilter = where.get(f);
        if (Condition.class.isAssignableFrom(condOrFilter.getClass())) {
            Condition c = (Condition) condOrFilter;
            BasicDBObject criteria = (BasicDBObject) mq.get(c.field());
            boolean newlyCreated = false;
            if (criteria == null) {
                newlyCreated = true;
                criteria = new BasicDBObject();
                mq.put(c.field(), criteria);
            }
            FilterAppender fa = FilterAppenders.get(c.operator());
            if (fa == null) {
                fa = DefaultFilterAppender;
            }
            BasicDBObject filter = fa.append(c, criteria);
            if (filter != null) {
                mq.putAll((Map<String, Object>) filter);
                if (newlyCreated) {
                    mq.remove(c.field());
                }
            }
        }
    }
    return cQuery;
}
Also used : Condition(com.bluenimble.platform.db.query.Condition) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) Where(com.bluenimble.platform.db.query.Where) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) RegexFilterAppender(com.bluenimble.platform.plugins.database.mongodb.impls.filters.RegexFilterAppender) TextFilterAppender(com.bluenimble.platform.plugins.database.mongodb.impls.filters.TextFilterAppender) NilFilterAppender(com.bluenimble.platform.plugins.database.mongodb.impls.filters.NilFilterAppender) LikeFilterAppender(com.bluenimble.platform.plugins.database.mongodb.impls.filters.LikeFilterAppender) FilterAppender(com.bluenimble.platform.plugins.database.mongodb.impls.filters.FilterAppender) BetweenFilterAppender(com.bluenimble.platform.plugins.database.mongodb.impls.filters.BetweenFilterAppender) DefaultFilterAppender(com.bluenimble.platform.plugins.database.mongodb.impls.filters.DefaultFilterAppender)

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