use of com.bluenimble.platform.plugins.database.mongodb.impls.filters.FilterAppender 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;
}
Aggregations