From 4b6dc9f5c78102c723a559593c593c046a051a4b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 29 May 2009 00:11:53 +0000 Subject: [PATCH] Nix a bunch of untyped methods in favor of just a version that takes Key. It's only one character more to type FooRecord.getKey(someId) than to type FooRecord.class, someId and with the new Key.newKey() methods, you can also create ad hoc keys briefly when needed. --- .../com/samskivert/depot/DepotRepository.java | 135 +++--------------- src/java/com/samskivert/depot/Key.java | 44 +++--- .../depot/tests/TestRepository.java | 2 +- 3 files changed, 42 insertions(+), 139 deletions(-) diff --git a/src/java/com/samskivert/depot/DepotRepository.java b/src/java/com/samskivert/depot/DepotRepository.java index c85ab46..517e0e9 100644 --- a/src/java/com/samskivert/depot/DepotRepository.java +++ b/src/java/com/samskivert/depot/DepotRepository.java @@ -212,6 +212,17 @@ public abstract class DepotRepository */ protected abstract void getManagedRecords (Set> classes); + /** + * Loads the persistent object that matches the specified primary key. + * + * @throws DatabaseException if any problem is encountered communicating with the database. + */ + protected T load (Key key) + throws DatabaseException + { + return load(key.getPersistentClass(), key); + } + /** * Loads the persistent object that matches the specified primary key. * @@ -234,35 +245,7 @@ public abstract class DepotRepository QueryClause... clauses) throws DatabaseException { - clauses = ArrayUtil.append(clauses, new Key(type, ix, val)); - return load(type, clauses); - } - - /** - * Loads the persistent object that matches the specified two-column primary key. - * - * @throws DatabaseException if any problem is encountered communicating with the database. - */ - protected T load (Class type, ColumnExp ix1, Comparable val1, - ColumnExp ix2, Comparable val2, - QueryClause... clauses) - throws DatabaseException - { - clauses = ArrayUtil.append(clauses, new Key(type, ix1, val1, ix2, val2)); - return load(type, clauses); - } - - /** - * Loads the persistent object that matches the specified three-column primary key. - * - * @throws DatabaseException if any problem is encountered communicating with the database. - */ - protected T load (Class type, ColumnExp ix1, Comparable val1, - ColumnExp ix2, Comparable val2, ColumnExp ix3, - Comparable val3, QueryClause... clauses) - throws DatabaseException - { - clauses = ArrayUtil.append(clauses, new Key(type, ix1, val1, ix2, val2, ix3, val3)); + clauses = ArrayUtil.append(clauses, Key.newKey(type, ix, val)); return load(type, clauses); } @@ -740,8 +723,7 @@ public abstract class DepotRepository * update BAZ set BIF = NOW(); * * - * @param type the type of the persistent object to be modified. - * @param primaryKey the key to match in the update. + * @param key the key to match in the update. * @param fieldsValues a map containing the columns and the values to be assigned. * * @return the number of rows modified by this action. @@ -751,69 +733,10 @@ public abstract class DepotRepository * @throws DatabaseException if any problem is encountered communicating with the database. */ protected int updateLiteral ( - Class type, Comparable primaryKey, - Map fieldsValues) + Key key, Map fieldsValues) throws DatabaseException { - Key key = _ctx.getMarshaller(type).makePrimaryKey(primaryKey); - return updateLiteral(type, key, key, fieldsValues); - } - - /** - * Updates the specified columns for all persistent objects matching the supplied two-column - * primary key. The values in this case must be literal SQL to be inserted into the update - * statement. In general this is used when you want to do something like the following: - * - *
-     * update FOO set BAR = BAR + 1;
-     * update BAZ set BIF = NOW();
-     * 
- * - * @param type the type of the persistent object to be modified. - * @param fieldsValues a map containing the columns and the values to be assigned. - * - * @return the number of rows modified by this action. - * - * @throws DuplicateKeyException if the update attempts to change the key columns of a row to - * values that duplicate another row already in the database. - * @throws DatabaseException if any problem is encountered communicating with the database. - */ - protected int updateLiteral ( - Class type, ColumnExp ix1, Comparable val1, ColumnExp ix2, Comparable val2, - Map fieldsValues) - throws DatabaseException - { - Key key = new Key(type, ix1, val1, ix2, val2); - return updateLiteral(type, key, key, fieldsValues); - } - - /** - * Updates the specified columns for all persistent objects matching the supplied three-column - * primary key. The values in this case must be literal SQL to be inserted into the update - * statement. In general this is used when you want to do something like the following: - * - *
-     * update FOO set BAR = BAR + 1;
-     * update BAZ set BIF = NOW();
-     * 
- * - * @param type the type of the persistent object to be modified. - * @param fieldsValues an array containing the names of the fields/columns and the values to be - * assigned, in key, literal value, key, literal value, etc. order. - * - * @return the number of rows modified by this action. - * - * @throws DuplicateKeyException if the update attempts to change the key columns of a row to - * values that duplicate another row already in the database. - * @throws DatabaseException if any problem is encountered communicating with the database. - */ - protected int updateLiteral ( - Class type, ColumnExp ix1, Comparable val1, ColumnExp ix2, Comparable val2, - ColumnExp ix3, Comparable val3, Map fieldsValues) - throws DatabaseException - { - Key key = new Key(type, ix1, val1, ix2, val2, ix3, val3); - return updateLiteral(type, key, key, fieldsValues); + return updateLiteral(key.getPersistentClass(), key, key, fieldsValues); } /** @@ -950,8 +873,8 @@ public abstract class DepotRepository } /** - * Deletes all persistent objects from the database with a primary key matching the primary key - * of the supplied object. + * Deletes all persistent objects from the database matching the primary key of the supplied + * object (which should be one or zero). * * @return the number of rows deleted by this action. * @@ -965,35 +888,21 @@ public abstract class DepotRepository if (primaryKey == null) { throw new IllegalArgumentException("Can't delete record with null primary key."); } - return delete(type, primaryKey); + return delete(primaryKey); } /** - * Deletes all persistent objects from the database with a primary key matching the supplied - * primary key. + * Deletes all persistent objects from the database matching the supplied primary key (which + * should be one or zero). * * @return the number of rows deleted by this action. * * @throws DatabaseException if any problem is encountered communicating with the database. */ - protected int delete (Class type, Comparable primaryKeyValue) + protected int delete (Key primaryKey) throws DatabaseException { - return delete(type, _ctx.getMarshaller(type).makePrimaryKey(primaryKeyValue)); - } - - /** - * Deletes all persistent objects from the database with a primary key matching the supplied - * primary key. - * - * @return the number of rows deleted by this action. - * - * @throws DatabaseException if any problem is encountered communicating with the database. - */ - protected int delete (Class type, Key primaryKey) - throws DatabaseException - { - return deleteAll(type, primaryKey, primaryKey); + return deleteAll(primaryKey.getPersistentClass(), primaryKey, primaryKey); } /** diff --git a/src/java/com/samskivert/depot/Key.java b/src/java/com/samskivert/depot/Key.java index d01471a..51768e5 100644 --- a/src/java/com/samskivert/depot/Key.java +++ b/src/java/com/samskivert/depot/Key.java @@ -36,11 +36,11 @@ import com.samskivert.depot.impl.ExpressionVisitor; import com.samskivert.util.StringUtil; /** - * A special form of {@link WhereClause} that uniquely specifies a single database row and - * thus also a single persistent object. Because it implements both {@link CacheKey} and - * {@link CacheInvalidator} it also uniquely indexes into the cache and knows how to invalidate - * itself upon modification. This class is created by many {@link DepotMarshaller} methods as - * a convenience, and may also be instantiated explicitly. + * A special form of {@link WhereClause} that uniquely specifies a single database row and thus + * also a single persistent object. Because it implements both {@link CacheKey} and {@link + * CacheInvalidator} it also uniquely indexes into the cache and knows how to invalidate itself + * upon modification. This class is created by many {@link DepotMarshaller} methods as a + * convenience, and may also be instantiated explicitly. */ public class Key extends WhereClause implements SQLExpression, CacheKey, ValidatingCacheInvalidator, Serializable @@ -70,38 +70,32 @@ public class Key extends WhereClause } /** - * Constructs a new single-column {@code Key} with the given value. + * Creates a single column key. */ - public Key (Class pClass, ColumnExp ix, Comparable val) + public static Key newKey ( + Class pClass, ColumnExp ix, Comparable val) { - this(pClass, new ColumnExp[] { ix }, new Comparable[] { val }); + return new Key(pClass, new ColumnExp[] { ix }, new Comparable[] { val }); } /** - * Constructs a new two-column {@code Key} with the given values. + * Creates a two column key. */ - public Key (Class pClass, ColumnExp ix1, Comparable val1, - ColumnExp ix2, Comparable val2) + public static Key newKey ( + Class pClass, ColumnExp ix1, Comparable val1, ColumnExp ix2, Comparable val2) { - this(pClass, new ColumnExp[] { ix1, ix2 }, new Comparable[] { val1, val2 }); + return new Key(pClass, new ColumnExp[] { ix1, ix2 }, new Comparable[] { val1, val2 }); } /** - * Constructs a new three-column {@code Key} with the given values. + * Creates a three column key. */ - public Key (Class pClass, ColumnExp ix1, Comparable val1, - ColumnExp ix2, Comparable val2, ColumnExp ix3, Comparable val3) + public static Key newKey ( + Class pClass, ColumnExp ix1, Comparable val1, ColumnExp ix2, Comparable val2, + ColumnExp ix3, Comparable val3) { - this(pClass, new ColumnExp[] { ix1, ix2, ix3 }, new Comparable[] { val1, val2, val3 }); - } - - /** - * TEMP: legacy foo bar! - */ - public Key (Class pClass, String[] fields, Comparable[] values) - { - _pClass = pClass; - _values = values; + return new Key(pClass, new ColumnExp[] { ix1, ix2, ix3 }, + new Comparable[] { val1, val2, val3 }); } /** diff --git a/src/java/com/samskivert/depot/tests/TestRepository.java b/src/java/com/samskivert/depot/tests/TestRepository.java index 64ba73c..b2ecfd9 100644 --- a/src/java/com/samskivert/depot/tests/TestRepository.java +++ b/src/java/com/samskivert/depot/tests/TestRepository.java @@ -75,7 +75,7 @@ public class TestRepository extends DepotRepository TestRepository repo = new TestRepository(perCtx); System.out.println("Deleting old record."); - repo.delete(TestRecord.class, 1); + repo.delete(TestRecord.getKey(1)); Date now = new Date(System.currentTimeMillis()); Timestamp tnow = new Timestamp(System.currentTimeMillis());