public Query<T> filter(String condition, Object value) { String[] parts = condition.trim().split(" "); if (parts.length < 1 || parts.length > 6) throw new IllegalArgumentException("'" + condition + "' is not a legal filter condition"); String prop = parts[0].trim(); FilterOperator op = (parts.length == 2) ? this.translate(parts[1]) : FilterOperator.EQUAL; this.add(new FieldCriteria(this, prop, op, value, this.validateName, this.validateType)); return this; }
public FieldEnd<? extends CriteriaContainerImpl> criteria(String field) { return this.criteria(field, this.validateName); }
QueryImpl<T> qi = (QueryImpl<T>) query; DBCollection dbColl = qi.getCollection(); dbColl = getCollection(qi.getEntityClass()); if (qi.getSortObject() != null && qi.getSortObject().keySet() != null && !qi.getSortObject().keySet().isEmpty()) throw new QueryException("sorting is not allowed for updates."); if (qi.getOffset() > 0) throw new QueryException("a query offset is not allowed for updates."); if (qi.getLimit() > 0) throw new QueryException("a query limit is not allowed for updates."); DBObject q = qi.getQueryObject(); if (q == null) q = new BasicDBObject();
/** * @param query * @param <T> * @return */ public <T> T findAndDelete(Query<T> query) { DBCollection dbColl = ((QueryImpl<T>) query).getCollection(); //TODO remove this after testing. if (dbColl == null) dbColl = getCollection(((QueryImpl<T>) query).getEntityClass()); QueryImpl<T> qi = ((QueryImpl<T>) query); EntityCache cache = createCache(); if (log.isTraceEnabled()) log.trace("Executing findAndModify(" + dbColl.getName() + ") with delete ..."); DBObject result = dbColl.findAndModify(qi.getQueryObject(), qi.getFieldsObject(), qi.getSortObject(), true, null, false, false); if (result != null) { T entity = (T) mapr.fromDBObject(qi.getEntityClass(), result, cache); return entity; } return null; }
/** * @param kind * @param clazz * @param q * @param <T> * @return */ public <T> Query<T> createQuery(String kind, Class<T> clazz, DBObject q) { return new QueryImpl<T>(clazz, db.getCollection(kind), this, q); }
@SuppressWarnings("unchecked") public List<T> asList() { List<T> results = new ArrayList<T>(); MorphiaIterator<T, T> iter = (MorphiaIterator<T, T>) fetch().iterator(); for (T ent : iter) results.add(ent); if (log.isTraceEnabled()) log.trace(String.format("\nasList: %s \t %d entities, iterator time: driver %n ms, mapper %n ms \n cache: %s \n for $s \n ", dbColl.getName(), results.size(), iter.getDriverTime(), iter.getMapperTime(), cache.stats().toString(), getQueryObject())); return results; }
protected FieldCriteria(QueryImpl<?> query, String field, FilterOperator op, Object value, boolean validateNames, boolean validateTypes, boolean not) { StringBuffer sb = new StringBuffer(field); //validate might modify prop string to translate java field name to db field name MappedField mf = DefaultMapper.validate(query.getEntityClass(), query.getDatastore().getMapper(), sb, op, value, validateNames, validateTypes); field = sb.toString(); Mapper mapr = query.getDatastore().getMapper();
public Iterable<T> fetchEmptyEntities() { String[] oldFields = fields; Boolean oldInclude = includeFields; fields = new String[]{Mapper.ID_KEY}; includeFields = true; Iterable<T> res = fetch(); fields = oldFields; includeFields = oldInclude; return res; }
public Query<T> where(String js) { this.add(new WhereCriteria(js)); return this; }
public List<Key<T>> asKeyList() { List<Key<T>> results = new ArrayList<Key<T>>(); for (Key<T> key : fetchKeys()) results.add(key); return results; }
public Query<T> createQuery() { return baseQuery.clone(); }
public FieldEnd<? extends Query<T>> field(String name) { return this.field(name, this.validateName); }
public DBObject getQueryObject() { DBObject obj = new BasicDBObject(); if (this.baseQuery != null) { obj.putAll((BSONObject) this.baseQuery); } this.addTo(obj); return obj; }
/** * @param kind * @param q * @param <T> * @return */ public <T> Query<T> createQuery(Class<T> kind, DBObject q) { return new QueryImpl<T>(kind, getCollection(kind), this, q); }
public Iterator<T> iterator() { return fetch().iterator(); }
public Query<T> where(CodeWScope js) { this.add(new WhereCriteria(js)); return this; }
public Key<T> getKey() { int oldLimit = limit; limit = 1; Iterator<Key<T>> it = fetchKeys().iterator(); limit = oldLimit; return (it.hasNext()) ? it.next() : null; }
/** * @param coll * @param example * @param <T> * @return */ private <T> Query<T> queryByExample(DBCollection coll, T example) { //TODO: think about remove className from baseQuery param below. return new QueryImpl<T>((Class<T>) example.getClass(), coll, this, entityToDBObj(example, new HashMap<Object, DBObject>())); }
public T get() { int oldLimit = limit; limit = 1; Iterator<T> it = fetch().iterator(); limit = oldLimit; return (it.hasNext()) ? it.next() : null; }