Search in sources :

Example 1 with Query

use of siena.Query in project siena by mandubian.

the class GaePersistenceManager method prepareKeysOnly.

private <T> PreparedQuery prepareKeysOnly(Query<T> query) {
    Class<?> clazz = query.getQueriedClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    com.google.appengine.api.datastore.Query q;
    // manages aggregation at first
    List<QueryAggregated> aggregs = query.getAggregatees();
    if (aggregs.isEmpty()) {
        q = new com.google.appengine.api.datastore.Query(ClassInfo.getClassInfo(clazz).tableName);
    } else if (aggregs.size() == 1) {
        QueryAggregated aggreg = aggregs.get(0);
        q = new com.google.appengine.api.datastore.Query(GaeMappingUtils.getKindWithAncestorField(info, ClassInfo.getClassInfo(aggreg.aggregator.getClass()), aggreg.field));
        q.setAncestor(GaeMappingUtils.getKey(aggreg.aggregator));
    } else {
        throw new SienaException("Only one aggregation per query allowed");
    }
    return ds.prepare(GaeQueryUtils.addFiltersOrders(query, q).setKeysOnly());
}
Also used : PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(siena.Query) SienaException(siena.SienaException) QueryAggregated(siena.QueryAggregated) ClassInfo(siena.ClassInfo)

Example 2 with Query

use of siena.Query in project siena by mandubian.

the class GaePersistenceManager method prepare.

private <T> PreparedQuery prepare(Query<T> query) {
    Class<?> clazz = query.getQueriedClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    com.google.appengine.api.datastore.Query q;
    // manages aggregation at first
    List<QueryAggregated> aggregs = query.getAggregatees();
    if (aggregs.isEmpty()) {
        q = new com.google.appengine.api.datastore.Query(info.tableName);
        return ds.prepare(GaeQueryUtils.addFiltersOrders(query, q));
    } else if (aggregs.size() == 1) {
        QueryAggregated aggreg = aggregs.get(0);
        Key ancestorKey = GaeMappingUtils.getKey(aggreg.aggregator);
        q = new com.google.appengine.api.datastore.Query(GaeMappingUtils.getKindWithAncestorField(info, ClassInfo.getClassInfo(aggreg.aggregator.getClass()), aggreg.field));
        q.setAncestor(ancestorKey);
        return ds.prepare(GaeQueryUtils.addFiltersOrders(query, q, ancestorKey));
    } else {
        throw new SienaException("Only one aggregation per query allowed");
    }
}
Also used : PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(siena.Query) SienaException(siena.SienaException) QueryAggregated(siena.QueryAggregated) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 3 with Query

use of siena.Query in project siena by mandubian.

the class GaePersistenceManager method fillAggregated.

protected <T> void fillAggregated(ClassInfo info, T ancestor, Key ancestorKey) {
    // now gets aggregated one2one (one2many are retrieved by ListQuery except with @Join)
    for (Field f : info.aggregatedFields) {
        Class<?> cClazz = f.getType();
        ClassInfo cInfo = ClassInfo.getClassInfo(cClazz);
        if (ClassInfo.isModel(cClazz)) {
            // creates a query for fieldname:child_tablename
            com.google.appengine.api.datastore.Query q = new com.google.appengine.api.datastore.Query(GaeMappingUtils.getKindWithAncestorField(cInfo, info, f));
            PreparedQuery pq = ds.prepare(q.setAncestor(ancestorKey));
            Entity cEntity = pq.asSingleEntity();
            Object cObj = Util.createObjectInstance(cClazz);
            GaeMappingUtils.fillModelAndKey(cObj, cEntity);
            Util.setField(ancestor, f, cObj);
        } else // todo manage joined one2many listquery
        if (ClassInfo.isMany(f)) {
            Many4PM<?> lq = (Many4PM<?>) Util.readField(ancestor, f);
            // sets the sync flag to false to tell that it should be fetched when the listquery is accessed!
            lq.setSync(false);
        }
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(siena.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Field(java.lang.reflect.Field) Many4PM(siena.core.Many4PM) ClassInfo(siena.ClassInfo)

Aggregations

PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)3 ClassInfo (siena.ClassInfo)3 Query (siena.Query)3 QueryAggregated (siena.QueryAggregated)2 SienaException (siena.SienaException)2 Entity (com.google.appengine.api.datastore.Entity)1 Key (com.google.appengine.api.datastore.Key)1 Field (java.lang.reflect.Field)1 Many4PM (siena.core.Many4PM)1