Search in sources :

Example 31 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class JdbcMappingUtils method getJoinFields.

public static <T> List<Field> getJoinFields(Query<T> query) {
    List<Field> joinFields = null;
    // adds all join fields brought by call to .join() functions
    if (query.getJoins().size() > 0) {
        joinFields = new ArrayList<Field>();
        for (QueryJoin join : query.getJoins()) joinFields.add(join.field);
    }
    // then adds the remaining joins coming from @Join if not added yet 
    ClassInfo ci = ClassInfo.getClassInfo(query.getQueriedClass());
    if (ci.joinFields.size() > 0) {
        if (joinFields == null)
            joinFields = new ArrayList<Field>();
        for (Field f : ci.joinFields) {
            if (!joinFields.contains(f))
                joinFields.add(f);
        }
    }
    return joinFields;
}
Also used : Field(java.lang.reflect.Field) QueryJoin(siena.QueryJoin) ArrayList(java.util.ArrayList) JdbcClassInfo(siena.jdbc.JdbcPersistenceManager.JdbcClassInfo) ClassInfo(siena.ClassInfo)

Example 32 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class H2PersistenceManager method doSearch.

protected <T> List<T> doSearch(Query<T> query, int limit, int offset) {
    // TODO this is a very raw impl: need some work certainly 
    try {
        Connection conn = this.getConnection();
        ClassInfo ci = ClassInfo.getClassInfo(query.getQueriedClass());
        // doesn't index a table that has already been indexed
        if (!tableIndexMap.containsKey(ci.tableName)) {
            List<String> colList = ci.getUpdateFieldsColumnNames();
            String cols = null;
            if (!colList.isEmpty()) {
                cols = "";
                // removes auto generated IDs from index
                int sz = colList.size();
                for (int i = 0; i < sz; i++) {
                    if ("h2".equals(dbMode))
                        cols += colList.get(i).toUpperCase();
                    else // !!! mysql mode means case INsensitive to lowercase !!!!
                    if ("mysql".equals(dbMode))
                        cols += colList.get(i).toLowerCase();
                    else
                        cols += colList.get(i).toUpperCase();
                    if (i < sz - 1)
                        cols += ",";
                }
            }
            // creates the index
            FullText.createIndex(conn, "PUBLIC", ci.tableName.toUpperCase(), cols);
            tableIndexMap.put(ci.tableName, true);
        }
        String searchString = "";
        Iterator<QueryFilterSearch> it = query.getSearches().iterator();
        boolean first = true;
        while (it.hasNext()) {
            if (!first) {
                searchString += " ";
            } else {
                first = false;
            }
            searchString += it.next().match;
        }
        ResultSet rs = FullText.searchData(conn, searchString, limit, offset);
        List<T> res = new ArrayList<T>();
        Field idField = ci.getIdField();
        Class<?> idClass = idField.getType();
        while (rs.next()) {
            //String queryStr = rs.getString("QUERY");
            //String score = rs.getString("SCORE");
            //Array columns = rs.getArray("COLUMNS");
            Object[] keys = (Object[]) rs.getArray("KEYS").getArray();
            // convert keys into real type if the key is not a string
            Object[] realKeys = null;
            if (idField.getType() != String.class) {
                realKeys = new Object[keys.length];
                for (int i = 0; i < keys.length; i++) {
                    realKeys[i] = Util.fromString(idClass, (String) keys[i]);
                }
            } else {
                realKeys = keys;
            }
            if (res == null)
                res = this.getByKeys(query.getQueriedClass(), realKeys);
            else
                res.addAll(this.getByKeys(query.getQueriedClass(), realKeys));
        }
        return res;
    } catch (SQLException e) {
        throw new SienaException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) QueryFilterSearch(siena.QueryFilterSearch) Field(java.lang.reflect.Field) ResultSet(java.sql.ResultSet) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 33 with ClassInfo

use of siena.ClassInfo 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)

Example 34 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _insertMultiple.

private <T> int _insertMultiple(Iterable<T> objects) {
    List<Entity> entities = new ArrayList<Entity>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        if (info.hasAggregator) {
            Relation rel = (Relation) Util.readField(obj, info.aggregator);
            if (rel != null && rel.mode == RelationMode.AGGREGATION) {
                ClassInfo parentInfo = ClassInfo.getClassInfo(rel.target.getClass());
                Key parentKey = GaeMappingUtils.makeKey(parentInfo, rel.target);
                _insertAddEntity(entities, obj, info, parentKey, parentInfo, (Field) rel.discriminator);
            } else {
                _insertAddEntity(entities, obj, info, null, null, null);
            }
        } else {
            _insertAddEntity(entities, obj, info, null, null, null);
        }
    }
    return _insertPutEntities(entities, objects);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Relation(siena.core.Relation) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 35 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method get.

public void get(Object obj) {
    Key key = GaeMappingUtils.getKey(obj);
    ClassInfo info = ClassInfo.getClassInfo(obj.getClass());
    try {
        Entity entity = ds.get(key);
        if (entity != null) {
            GaeMappingUtils.fillModel(obj, entity);
            // related fields (Many<T> management mainly)
            if (!info.ownedFields.isEmpty()) {
                mapOwned(obj);
            }
            // aggregated management
            if (!info.aggregatedFields.isEmpty()) {
                mapAggregated(obj);
            }
            // join management
            if (!info.joinFields.isEmpty()) {
                mapJoins(obj);
            }
        }
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key) SienaException(siena.SienaException) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) ClassInfo(siena.ClassInfo)

Aggregations

ClassInfo (siena.ClassInfo)68 Field (java.lang.reflect.Field)33 ArrayList (java.util.ArrayList)24 Key (com.google.appengine.api.datastore.Key)23 SienaException (siena.SienaException)21 Entity (com.google.appengine.api.datastore.Entity)17 QueryResultList (com.google.appengine.api.datastore.QueryResultList)10 List (java.util.List)10 DeletableItem (com.amazonaws.services.simpledb.model.DeletableItem)6 Item (com.amazonaws.services.simpledb.model.Item)6 ReplaceableItem (com.amazonaws.services.simpledb.model.ReplaceableItem)6 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 SienaRestrictedApiException (siena.SienaRestrictedApiException)5 Relation (siena.core.Relation)5 IOException (java.io.IOException)4 QueryFilterSearch (siena.QueryFilterSearch)4 Many4PM (siena.core.Many4PM)4 SienaFutureContainer (siena.core.async.SienaFutureContainer)4