Use DepotTypes to keep track of the types in our query. That's it's whole

purpose in life.
This commit is contained in:
Michael Bayne
2010-12-09 00:42:34 +00:00
parent f629b267ba
commit 800c566fc6
2 changed files with 14 additions and 20 deletions
@@ -150,9 +150,10 @@ public class DepotTypes
* *
* @exception IllegalArgumentException thrown if the specified class is not known. * @exception IllegalArgumentException thrown if the specified class is not known.
*/ */
public DepotMarshaller<?> getMarshaller (Class<? extends PersistentRecord> cl) public <T extends PersistentRecord> DepotMarshaller<T> getMarshaller (Class<T> cl)
{ {
DepotMarshaller<?> marsh = _classMap.get(cl); @SuppressWarnings("unchecked") DepotMarshaller<T> marsh =
(DepotMarshaller<T>)_classMap.get(cl);
checkArgument(marsh != null, "Persistent class not known: " + cl); checkArgument(marsh != null, "Persistent class not known: " + cl);
return marsh; return marsh;
} }
@@ -271,9 +271,10 @@ public abstract class FindAllQuery<T extends PersistentRecord,R>
Iterable<? extends QueryClause> clauses) Iterable<? extends QueryClause> clauses)
throws DatabaseException throws DatabaseException
{ {
super(cset.ptype, new ColumnSetQueryMarshaller<T,R>(ctx, cset), super(cset.ptype, null, new NonCloningCloner<R>());
new NonCloningCloner<R>());
_select = new SelectClause(cset.ptype, cset.columns, clauses); _select = new SelectClause(cset.ptype, cset.columns, clauses);
_types = DepotTypes.getDepotTypes(ctx, _select);
_marsh = new ColumnSetQueryMarshaller<T,R>(cset, _types);
} }
@Override // from Query @Override // from Query
@@ -286,7 +287,7 @@ public abstract class FindAllQuery<T extends PersistentRecord,R>
public List<R> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) public List<R> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
throws SQLException throws SQLException
{ {
SQLBuilder builder = ctx.getSQLBuilder(DepotTypes.getDepotTypes(ctx, _select)); SQLBuilder builder = ctx.getSQLBuilder(_types);
builder.newQuery(_select); builder.newQuery(_select);
ResultSet rs = builder.prepare(conn).executeQuery(); ResultSet rs = builder.prepare(conn).executeQuery();
List<R> result = Lists.newArrayList(); List<R> result = Lists.newArrayList();
@@ -297,26 +298,19 @@ public abstract class FindAllQuery<T extends PersistentRecord,R>
} }
protected SelectClause _select; protected SelectClause _select;
protected DepotTypes _types;
} }
protected static class ColumnSetQueryMarshaller<T extends PersistentRecord,R> protected static class ColumnSetQueryMarshaller<T extends PersistentRecord,R>
implements QueryMarshaller<T,R> implements QueryMarshaller<T,R>
{ {
public ColumnSetQueryMarshaller (PersistenceContext ctx, ColumnSet<T,R> cset) { public ColumnSetQueryMarshaller (ColumnSet<T,R> cset, DepotTypes types) {
_cset = cset; _cset = cset;
_cmarsh = ctx.getMarshaller(cset.ptype); _types = types;
_marshes = Maps.newHashMap();
_marshes.put(cset.ptype, _cmarsh);
for (ColumnExp<?> cexp : cset.columns) {
Class<? extends PersistentRecord> cclass = cexp.getPersistentClass();
if (!_marshes.containsKey(cclass)) {
_marshes.put(cclass, ctx.getMarshaller(cexp.getPersistentClass()));
}
}
} }
public String getTableName () { public String getTableName () {
return _cmarsh.getTableName(); return _types.getTableName(_cset.ptype);
} }
public ColumnExp<?>[] getFieldNames () { public ColumnExp<?>[] getFieldNames () {
@@ -324,22 +318,21 @@ public abstract class FindAllQuery<T extends PersistentRecord,R>
} }
public Key<T> getPrimaryKey (Object object) { public Key<T> getPrimaryKey (Object object) {
return _cmarsh.getPrimaryKey(object); return _types.getMarshaller(_cset.ptype).getPrimaryKey(object);
} }
public R createObject (ResultSet rs) throws SQLException { public R createObject (ResultSet rs) throws SQLException {
Object[] data = new Object[_cset.columns.length]; Object[] data = new Object[_cset.columns.length];
for (int ii = 0; ii < data.length; ii++) { for (int ii = 0; ii < data.length; ii++) {
ColumnExp<?> col = _cset.columns[ii]; ColumnExp<?> col = _cset.columns[ii];
data[ii] = _marshes.get(col.getPersistentClass()). data[ii] = _types.getMarshaller(col.getPersistentClass()).
getFieldMarshaller(col.name).getFromSet(rs, ii+1); getFieldMarshaller(col.name).getFromSet(rs, ii+1);
} }
return _cset.createObject(data); return _cset.createObject(data);
} }
protected ColumnSet<T,R> _cset; protected ColumnSet<T,R> _cset;
protected DepotMarshaller<T> _cmarsh; protected DepotTypes _types;
protected Map<Class<? extends PersistentRecord>, DepotMarshaller<?>> _marshes;
} }
// from Query // from Query