public <T, V> Query<T> find(
     String kind, Class<T> clazz, String property, V value, int offset, int size) {
   Query<T> query = find(kind, clazz);
   query.offset(offset);
   query.limit(size);
   return query.filter(property, value);
 }
  public <T> UpdateResults<T> update(T ent, UpdateOperations<T> ops) {
    MappedClass mc = mapr.getMappedClass(ent);
    Query<T> q = (Query<T>) createQuery(mc.getClazz());
    q.disableValidation().filter(Mapper.ID_KEY, getId(ent));

    if (mc.getFieldsAnnotatedWith(Version.class).size() > 0) {
      MappedField versionMF = mc.getFieldsAnnotatedWith(Version.class).get(0);
      Long oldVer = (Long) versionMF.getFieldValue(ent);
      q.filter(versionMF.getNameToStore(), oldVer);
      ops.set(versionMF.getNameToStore(), VersionHelper.nextValue(oldVer));
    }

    return update(q, ops);
  }
Example #3
0
 public static List<Quote> findAllSortedByVoteFilteredByAuthor(String author, QuoteState state) {
   Query<Quote> query = DBHolder.ds.find(Quote.class).field("deleted").equal(false);
   query = query.field("quoteState").equal(state);
   return query.filter("author", author).order("-voteCount").asList();
 }
 /*
  * Support the following syntax at the moment: property = 'val' property
  * in ('val1', 'val2' ...) prop1 ... and prop2 ...
  */
 public static void processWhere(Query<?> q, String where) {
   if (null != where) {
     where = where.trim();
   } else {
     where = "";
   }
   if ("".equals(where) || "null".equalsIgnoreCase(where)) return;
   if (where.startsWith("function")) {
     q.where(where);
     return;
   }
   String[] propValPairs = where.split("(and|&&)");
   for (String propVal : propValPairs) {
     if (propVal.contains("=")) {
       String[] sa = propVal.split("=");
       if (sa.length != 2) {
         throw new IllegalArgumentException("invalid where clause: " + where);
       }
       String prop = sa[0];
       String val = sa[1];
       debug("where prop val pair found: %1$s = %2$s", prop, val);
       prop = prop.replaceAll("[\"' ]", "");
       if (val.matches("[\"'].*[\"']")) {
         // string value
         val = val.replaceAll("[\"' ]", "");
         q.filter(prop, val);
       } else {
         // possible string, number or boolean value
         if (val.matches("[-+]?\\d+\\.\\d+")) {
           q.filter(prop, Float.parseFloat(val));
         } else if (val.matches("[-+]?\\d+")) {
           q.filter(prop, Integer.parseInt(val));
         } else if (val.matches("(false|true|FALSE|TRUE|False|True)")) {
           q.filter(prop, Boolean.parseBoolean(val));
         } else {
           q.filter(prop, val);
         }
       }
     } else if (propVal.contains(" in ")) {
       String[] sa = propVal.split(" in ");
       if (sa.length != 2) {
         throw new IllegalArgumentException("invalid where clause: " + where);
       }
       String prop = sa[0].trim();
       String val0 = sa[1].trim();
       if (!val0.matches("\\(.*\\)")) {
         throw new IllegalArgumentException("invalid where clause: " + where);
       }
       val0 = val0.replaceAll("[\\(\\)]", "");
       String[] vals = val0.split(",");
       List<Object> l = new ArrayList<Object>();
       for (String val : vals) {
         // possible string, number or boolean value
         if (val.matches("[-+]?\\d+\\.\\d+")) {
           l.add(Float.parseFloat(val));
         } else if (val.matches("[-+]?\\d+")) {
           l.add(Integer.parseInt(val));
         } else if (val.matches("(false|true|FALSE|TRUE|False|True)")) {
           l.add(Boolean.parseBoolean(val));
         } else {
           l.add(val);
         }
       }
       q.filter(prop + " in ", l);
     } else {
       throw new IllegalArgumentException("invalid where clause: " + where);
     }
   }
 }
 public <T, V> Query<T> find(Class<T> clazz, String property, V value) {
   Query<T> query = createQuery(clazz);
   return query.filter(property, value);
 }