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;
}
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);
}
}
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);
}
}
}
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);
}
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);
}
}
Aggregations