Search in sources :

Example 1 with Condition

use of com.bluenimble.platform.db.query.Condition 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

DatabaseObject (com.bluenimble.platform.db.DatabaseObject)1 CompiledQuery (com.bluenimble.platform.db.query.CompiledQuery)1 Condition (com.bluenimble.platform.db.query.Condition)1 Where (com.bluenimble.platform.db.query.Where)1 JsonObject (com.bluenimble.platform.json.JsonObject)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 NilFilterAppender (com.bluenimble.platform.plugins.database.mongodb.impls.filters.NilFilterAppender)1 RegexFilterAppender (com.bluenimble.platform.plugins.database.mongodb.impls.filters.RegexFilterAppender)1 TextFilterAppender (com.bluenimble.platform.plugins.database.mongodb.impls.filters.TextFilterAppender)1 BasicDBObject (com.mongodb.BasicDBObject)1