diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/JORARepository.java b/projects/samskivert/src/java/com/samskivert/jdbc/JORARepository.java index d9bf6c10..04a60f5f 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/JORARepository.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/JORARepository.java @@ -104,6 +104,23 @@ public abstract class JORARepository extends SimpleRepository }); } + /** + * Loads a single object from the specified table that matches the + * supplied example. Note: the query should match one or zero + * records, not more. + */ + protected Object loadByExample (final Table table, final Object example) + throws PersistenceException + { + return execute(new Operation() { + public Object invoke (Connection conn, DatabaseLiaison liaison) + throws SQLException, PersistenceException + { + return table.queryByExample(example).next(); + } + }); + } + /** * First attempts to update the supplied object and if that modifies * zero rows, inserts the object into the specified table. The table diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java b/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java index 40f46511..2bda4760 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java @@ -197,6 +197,53 @@ public class SimpleRepository extends Repository } } + /** + * Executes the supplied update query in this repository, ignoring the + * return value. + */ + protected void update (final String query) + throws PersistenceException + { + execute(new Operation() { + public Object invoke (Connection conn, DatabaseLiaison liaison) + throws SQLException, PersistenceException + { + Statement stmt = null; + try { + stmt = conn.createStatement(); + stmt.executeUpdate(query); + } finally { + JDBCUtil.close(stmt); + } + return null; + } + }); + } + + /** + * Executes the supplied update query in this repository, throwing an + * exception if the modification count is not equal to the specified + * count. + */ + protected void checkedUpdate (final String query, final int count) + throws PersistenceException + { + execute(new Operation() { + public Object invoke (Connection conn, DatabaseLiaison liaison) + throws SQLException, PersistenceException + { + Statement stmt = null; + try { + stmt = conn.createStatement(); + JDBCUtil.checkedUpdate(stmt, query, 1); + } finally { + JDBCUtil.close(stmt); + } + return null; + } + }); + } + /** * Called when we fetch a connection from the provider. This gives * derived classes an opportunity to configure whatever internals they