diff --git a/src/java/com/samskivert/depot/DepotRepository.java b/src/java/com/samskivert/depot/DepotRepository.java index 2d2ac57..d45fac2 100644 --- a/src/java/com/samskivert/depot/DepotRepository.java +++ b/src/java/com/samskivert/depot/DepotRepository.java @@ -480,34 +480,17 @@ public abstract class DepotRepository * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected int update (T record) + protected int update (PersistentRecord record) throws DatabaseException { - @SuppressWarnings("unchecked") Class pClass = (Class) record.getClass(); + Class pClass = record.getClass(); requireNotComputed(pClass, "update"); - - DepotMarshaller marsh = _ctx.getMarshaller(pClass); - Key key = marsh.getPrimaryKey(record); + DepotMarshaller marsh = _ctx.getMarshaller(pClass); + Key key = marsh.getPrimaryKey(record); if (key == null) { throw new IllegalArgumentException("Can't update record with null primary key."); } - - UpdateClause update = - new UpdateClause(pClass, key, marsh.getColumnFieldNames(), record); - final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, update)); - builder.newQuery(update); - - return _ctx.invoke(new CachingModifier(record, key, key) { - @Override - protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { - PreparedStatement stmt = builder.prepare(conn); - try { - return stmt.executeUpdate(); - } finally { - JDBCUtil.close(stmt); - } - } - }); + return doUpdate(key, new UpdateClause(pClass, key, marsh.getColumnFieldNames(), record)); } /** @@ -525,31 +508,13 @@ public abstract class DepotRepository { @SuppressWarnings("unchecked") Class pClass = (Class) record.getClass(); requireNotComputed(pClass, "update"); - DepotMarshaller marsh = _ctx.getMarshaller(pClass); Key key = marsh.getPrimaryKey(record); if (key == null) { throw new IllegalArgumentException("Can't update record with null primary key."); } - - UpdateClause update = new UpdateClause( - pClass, key, ColumnExp.toNames(modifiedFields), record); - final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, update)); - builder.newQuery(update); - - return _ctx.invoke(new CachingModifier(record, key, key) { - @Override - protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { - PreparedStatement stmt = builder.prepare(conn); - // clear out _result so that we don't rewrite this partial record to the cache - _result = null; - try { - return stmt.executeUpdate(); - } finally { - JDBCUtil.close(stmt); - } - } - }); + String[] fields = ColumnExp.toNames(modifiedFields); + return doUpdate(key, new UpdateClause(pClass, key, fields, record)); } /** @@ -594,6 +559,7 @@ public abstract class DepotRepository Class type, final WhereClause key, CacheInvalidator invalidator, Object... fieldsValues) throws DatabaseException { + requireNotComputed(type, "updateLiteral"); if (invalidator instanceof ValidatingCacheInvalidator) { ((ValidatingCacheInvalidator)invalidator).validateFlushType(type); // sanity check } @@ -603,38 +569,15 @@ public abstract class DepotRepository final String[] fields = new String[fieldsValues.length/2]; final SQLExpression[] values = new SQLExpression[fields.length]; for (int ii = 0, idx = 0; ii < fields.length; ii++) { - if (fieldsValues[idx] instanceof ColumnExp) { - fields[ii] = ((ColumnExp) fieldsValues[idx]).name; - } else if (fieldsValues[idx] instanceof String) { - fields[ii] = (String) fieldsValues[idx]; - } else { - throw new IllegalArgumentException( - "Field identifier #" + (ii+1) + " is neither String nor ColumnExp"); - } - idx ++; - + fields[ii] = ((ColumnExp) fieldsValues[idx++]).name; if (fieldsValues[idx] instanceof SQLExpression) { - values[ii] = (SQLExpression) fieldsValues[idx]; + values[ii] = (SQLExpression) fieldsValues[idx++]; } else { - values[ii] = new ValueExp(fieldsValues[idx]); + values[ii] = new ValueExp(fieldsValues[idx++]); } - idx ++; } - UpdateClause update = new UpdateClause(type, key, fields, values); - final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, update)); - builder.newQuery(update); - return _ctx.invoke(new Modifier(invalidator) { - @Override - protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { - PreparedStatement stmt = builder.prepare(conn); - try { - return stmt.executeUpdate(); - } finally { - JDBCUtil.close(stmt); - } - } - }); + return doUpdate(invalidator, new UpdateClause(type, key, fields, values)); } /** @@ -690,7 +633,6 @@ public abstract class DepotRepository throws DatabaseException { requireNotComputed(type, "updateLiteral"); - if (invalidator instanceof ValidatingCacheInvalidator) { ((ValidatingCacheInvalidator)invalidator).validateFlushType(type); // sanity check } @@ -706,21 +648,7 @@ public abstract class DepotRepository ii ++; } - UpdateClause update = new UpdateClause(type, key, fields, values); - final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, update)); - builder.newQuery(update); - - return _ctx.invoke(new Modifier(invalidator) { - @Override - protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { - PreparedStatement stmt = builder.prepare(conn); - try { - return stmt.executeUpdate(); - } finally { - JDBCUtil.close(stmt); - } - } - }); + return doUpdate(invalidator, new UpdateClause(type, key, fields, values)); } /** @@ -740,8 +668,8 @@ public abstract class DepotRepository final DepotMarshaller marsh = _ctx.getMarshaller(pClass); Key key = marsh.hasPrimaryKey() ? marsh.getPrimaryKey(record) : null; - final UpdateClause update = - new UpdateClause(pClass, key, marsh.getColumnFieldNames(), record); + final UpdateClause update = + new UpdateClause(pClass, key, marsh.getColumnFieldNames(), record); final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, update)); // if our primary key isn't null, we start by trying to update rather than insert @@ -900,6 +828,26 @@ public abstract class DepotRepository } } + /** + * A helper method for the various partial update methods. + */ + protected int doUpdate (CacheInvalidator invalidator, UpdateClause update) + { + final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, update)); + builder.newQuery(update); + return _ctx.invoke(new Modifier(invalidator) { + @Override + protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { + PreparedStatement stmt = builder.prepare(conn); + try { + return stmt.executeUpdate(); + } finally { + JDBCUtil.close(stmt); + } + } + }); + } + /** * If the supplied migration has not already been run, it will be run and if it completes, we * will note in the DepotMigrationHistory table that it has been run. diff --git a/src/java/com/samskivert/depot/clause/SelectClause.java b/src/java/com/samskivert/depot/clause/SelectClause.java index 64a21f3..bead869 100644 --- a/src/java/com/samskivert/depot/clause/SelectClause.java +++ b/src/java/com/samskivert/depot/clause/SelectClause.java @@ -34,14 +34,15 @@ import com.samskivert.depot.impl.ExpressionVisitor; /** * Builds actual SQL given a main persistent type and some {@link QueryClause} objects. */ -public class SelectClause implements QueryClause +public class SelectClause implements QueryClause { /** * Creates a new Query object to generate one or more instances of the specified persistent * class, as dictated by the key and query clauses. A persistence context is supplied for * instantiation of marshallers, which may trigger table creations and schema migrations. */ - public SelectClause (Class pClass, String[] fields, Collection clauses) + public SelectClause (Class pClass, String[] fields, + Collection clauses) { _pClass = pClass; _fields = fields; @@ -109,7 +110,8 @@ public class SelectClause implements QueryClause /** * A varargs version of the constructor. */ - public SelectClause (Class pClass, String[] fields, QueryClause... clauses) + public SelectClause (Class pClass, + String[] fields, QueryClause... clauses) { this(pClass, fields, Arrays.asList(clauses)); } @@ -124,7 +126,7 @@ public class SelectClause implements QueryClause return _disMap.values(); } - public Class getPersistentClass () + public Class getPersistentClass () { return _pClass; } @@ -224,7 +226,7 @@ public class SelectClause implements QueryClause protected Map _disMap = Maps.newHashMap(); /** The persistent class this select defines. */ - protected Class _pClass; + protected Class _pClass; /** The persistent fields to select. */ protected String[] _fields; diff --git a/src/java/com/samskivert/depot/impl/BuildVisitor.java b/src/java/com/samskivert/depot/impl/BuildVisitor.java index 34d6cec..7b251a8 100644 --- a/src/java/com/samskivert/depot/impl/BuildVisitor.java +++ b/src/java/com/samskivert/depot/impl/BuildVisitor.java @@ -334,7 +334,7 @@ public abstract class BuildVisitor implements ExpressionVisitor return null; } - public Void visit (SelectClause selectClause) + public Void visit (SelectClause selectClause) { Class pClass = selectClause.getPersistentClass(); boolean isInner = _innerClause; @@ -432,7 +432,7 @@ public abstract class BuildVisitor implements ExpressionVisitor return null; } - public Void visit (UpdateClause updateClause) + public Void visit (UpdateClause updateClause) { if (updateClause.getWhereClause() == null) { throw new IllegalArgumentException( diff --git a/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java b/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java index d735255..eb08b61 100644 --- a/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java +++ b/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java @@ -299,12 +299,12 @@ public class ExpressionEvaluator throw new IllegalArgumentException("Can't evaluate expression: " + fromOverride); } - public Object visit (SelectClause selectClause) + public Object visit (SelectClause selectClause) { throw new IllegalArgumentException("Can't evaluate expression: " + selectClause); } - public Object visit (UpdateClause updateClause) + public Object visit (UpdateClause updateClause) { throw new IllegalArgumentException("Can't evaluate expression: " + updateClause); } diff --git a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java index d82d81a..2bdf829 100644 --- a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java +++ b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java @@ -83,8 +83,8 @@ public interface ExpressionVisitor public T visit (WhereClause where); public T visit (Key.Expression key); public T visit (Exists exists); - public T visit (SelectClause selectClause); - public T visit (UpdateClause updateClause); + public T visit (SelectClause selectClause); + public T visit (UpdateClause updateClause); public T visit (DeleteClause deleteClause); public T visit (InsertClause insertClause); public T visit (CreateIndexClause createIndexClause); diff --git a/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java b/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java index 3d1b642..9b02a9b 100644 --- a/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java +++ b/src/java/com/samskivert/depot/impl/FindAllKeysQuery.java @@ -54,7 +54,7 @@ public class FindAllKeysQuery extends Query(type, _marsh.getPrimaryKeyFields(), clauses); + _select = new SelectClause(type, _marsh.getPrimaryKeyFields(), clauses); _builder = ctx.getSQLBuilder(DepotTypes.getDepotTypes(ctx, _select)); _builder.newQuery(_select); } @@ -101,5 +101,5 @@ public class FindAllKeysQuery extends Query _marsh; - protected SelectClause _select; + protected SelectClause _select; } diff --git a/src/java/com/samskivert/depot/impl/FindAllQuery.java b/src/java/com/samskivert/depot/impl/FindAllQuery.java index 7f48602..70c4343 100644 --- a/src/java/com/samskivert/depot/impl/FindAllQuery.java +++ b/src/java/com/samskivert/depot/impl/FindAllQuery.java @@ -84,7 +84,7 @@ public abstract class FindAllQuery extends Query(_type, _marsh.getPrimaryKeyFields(), clauses); + _select = new SelectClause(_type, _marsh.getPrimaryKeyFields(), clauses); switch(strategy) { case SHORT_KEYS: case LONG_KEYS: _qkey = new SimpleCacheKey(_marsh.getTableName() + "Keys", _select.toString()); @@ -159,7 +159,7 @@ public abstract class FindAllQuery extends Query _select; + protected SelectClause _select; protected KeySet _keys; protected Set> _fetchKeys; protected Map, T> _entities = Maps.newHashMap(); @@ -212,7 +212,7 @@ public abstract class FindAllQuery extends Query(type, _marsh.getFieldNames(), clauses); + _select = new SelectClause(type, _marsh.getFieldNames(), clauses); if (cachedContents) { _qkey = new SimpleCacheKey(_marsh.getTableName() + "Contents", _select.toString()); @@ -258,7 +258,7 @@ public abstract class FindAllQuery extends Query _select; + protected SelectClause _select; protected SimpleCacheKey _qkey; } @@ -343,7 +343,7 @@ public abstract class FindAllQuery extends Query, T> entities, String origStmt) throws SQLException { - SelectClause select = new SelectClause( + SelectClause select = new SelectClause( _type, _marsh.getFieldNames(), KeySet.newKeySet(_type, keys)); SQLBuilder builder = ctx.getSQLBuilder(DepotTypes.getDepotTypes(ctx, select)); builder.newQuery(select); diff --git a/src/java/com/samskivert/depot/impl/FindOneQuery.java b/src/java/com/samskivert/depot/impl/FindOneQuery.java index d8a93e9..d98e33d 100644 --- a/src/java/com/samskivert/depot/impl/FindOneQuery.java +++ b/src/java/com/samskivert/depot/impl/FindOneQuery.java @@ -52,7 +52,7 @@ public class FindOneQuery extends Query { _strategy = strategy; _marsh = ctx.getMarshaller(type); - _select = new SelectClause(type, _marsh.getFieldNames(), clauses); + _select = new SelectClause(type, _marsh.getFieldNames(), clauses); WhereClause where = _select.getWhereClause(); if (where != null) { _select.getWhereClause().validateQueryType(type); // sanity check @@ -135,7 +135,7 @@ public class FindOneQuery extends Query protected DepotRepository.CacheStrategy _strategy; protected DepotMarshaller _marsh; - protected SelectClause _select; + protected SelectClause _select; protected SQLBuilder _builder; protected int _cachedRecords; } diff --git a/src/java/com/samskivert/depot/impl/clause/UpdateClause.java b/src/java/com/samskivert/depot/impl/clause/UpdateClause.java index 17d9bf9..0df297c 100644 --- a/src/java/com/samskivert/depot/impl/clause/UpdateClause.java +++ b/src/java/com/samskivert/depot/impl/clause/UpdateClause.java @@ -32,11 +32,11 @@ import com.samskivert.depot.impl.ExpressionVisitor; /** * Builds actual SQL given a main persistent type and some {@link QueryClause} objects. */ -public class UpdateClause +public class UpdateClause implements QueryClause { public UpdateClause (Class pClass, WhereClause where, - String[] fields, T pojo) + String[] fields, PersistentRecord pojo) { _pClass = pClass; _where = where; @@ -70,7 +70,7 @@ public class UpdateClause return _values; } - public Object getPojo () + public PersistentRecord getPojo () { return _pojo; } @@ -113,5 +113,5 @@ public class UpdateClause protected SQLExpression[] _values; /** The object from which to fetch values, or null. */ - protected Object _pojo; + protected PersistentRecord _pojo; } diff --git a/src/java/com/samskivert/depot/operator/Exists.java b/src/java/com/samskivert/depot/operator/Exists.java index 720764d..bc253f0 100644 --- a/src/java/com/samskivert/depot/operator/Exists.java +++ b/src/java/com/samskivert/depot/operator/Exists.java @@ -32,7 +32,7 @@ import com.samskivert.depot.impl.ExpressionVisitor; public class Exists implements SQLOperator { - public Exists (SelectClause clause) + public Exists (SelectClause clause) { _clause = clause; } @@ -47,7 +47,7 @@ public class Exists _clause.addClasses(classSet); } - public SelectClause getSubClause () + public SelectClause getSubClause () { return _clause; } @@ -58,5 +58,5 @@ public class Exists return "Exists(" + _clause + ")"; } - protected SelectClause _clause; + protected SelectClause _clause; }