diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 9e4c1a0..4b70f2f 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -993,7 +993,7 @@ public class DepotMarshaller public static TableMetaData load (PersistenceContext ctx, final String tableName) throws DatabaseException { - return ctx.invoke(new Query.TrivialQuery() { + return ctx.invoke(new Query.Trivial() { @Override public TableMetaData invoke (Connection conn, DatabaseLiaison dl) throws SQLException { return new TableMetaData(conn.getMetaData(), tableName); diff --git a/src/java/com/samskivert/jdbc/depot/DepotRepository.java b/src/java/com/samskivert/jdbc/depot/DepotRepository.java index 0dd0811..93fdc97 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotRepository.java +++ b/src/java/com/samskivert/jdbc/depot/DepotRepository.java @@ -25,6 +25,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -182,6 +183,43 @@ public abstract class DepotRepository return findAll(type, Arrays.asList(clauses)); } + /** + * Looks up and returns {@link Key} records for all rows that match the supplied query clauses. + */ + protected List> findAllKeys ( + Class type, QueryClause... clause) + { + return findAllKeys(type, Arrays.asList(clause)); + } + + /** + * Looks up and returns {@link Key} records for all rows that match the supplied query clauses. + */ + protected List> findAllKeys ( + Class type, Collection clauses) + { + // first look up the primary keys for all the rows that match our where clause + final DepotMarshaller marsh = _ctx.getMarshaller(type); + final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, clauses)); + builder.newQuery(new SelectClause(type, marsh.getPrimaryKeyFields(), clauses)); + return _ctx.invoke(new Query.Trivial>>() { + @Override public List> invoke (Connection conn, DatabaseLiaison liaison) + throws SQLException { + List> keys = new ArrayList>(); + PreparedStatement stmt = builder.prepare(conn); + try { + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + keys.add(marsh.makePrimaryKey(rs)); + } + return keys; + } finally { + JDBCUtil.close(stmt); + } + } + }); + } + /** * Inserts the supplied persistent object into the database, assigning its primary key (if it * has one) in the process. @@ -710,29 +748,8 @@ public abstract class DepotRepository protected int deleteAll (Class type, final WhereClause where) throws DatabaseException { - // first look up the primary keys for all the rows that match our where clause - final Set> keys = new HashSet>(); - final DepotMarshaller marsh = _ctx.getMarshaller(type); - final SQLBuilder fbuilder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, where)); - fbuilder.newQuery(new SelectClause(type, marsh.getPrimaryKeyFields(), where)); - _ctx.invoke(new Modifier(null) { - @Override - public int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { - PreparedStatement stmt = fbuilder.prepare(conn); - try { - ResultSet rs = stmt.executeQuery(); - while (rs.next()) { - keys.add(marsh.makePrimaryKey(rs)); - } - return 0; - } finally { - JDBCUtil.close(stmt); - } - } - }); - - // now delete using that primary key set - KeySet pwhere = new KeySet(type, keys); + // look up the primary keys for all rows matching our where clause and delete using those + KeySet pwhere = new KeySet(type, findAllKeys(type, where)); return deleteAll(type, pwhere, pwhere); } diff --git a/src/java/com/samskivert/jdbc/depot/KeySet.java b/src/java/com/samskivert/jdbc/depot/KeySet.java index d3bec2c..d7ca966 100644 --- a/src/java/com/samskivert/jdbc/depot/KeySet.java +++ b/src/java/com/samskivert/jdbc/depot/KeySet.java @@ -21,7 +21,6 @@ package com.samskivert.jdbc.depot; import java.util.Collection; -import java.util.Set; import com.samskivert.jdbc.depot.expression.ExpressionVisitor; import com.samskivert.jdbc.depot.expression.LiteralExp; @@ -43,7 +42,7 @@ public class KeySet extends WhereClause /** * Creates a set from the supplied primary keys. */ - public KeySet (Class pClass, Set> keys) + public KeySet (Class pClass, Collection> keys) { _pClass = pClass; _keys = keys; @@ -148,6 +147,6 @@ public class KeySet extends WhereClause } protected Class _pClass; - protected Set> _keys; + protected Collection> _keys; protected SQLExpression _condition; } diff --git a/src/java/com/samskivert/jdbc/depot/Query.java b/src/java/com/samskivert/jdbc/depot/Query.java index 4a912d9..bb182b3 100644 --- a/src/java/com/samskivert/jdbc/depot/Query.java +++ b/src/java/com/samskivert/jdbc/depot/Query.java @@ -31,7 +31,7 @@ import com.samskivert.jdbc.DatabaseLiaison; public interface Query { /** A simple base class for non-complex queries. */ - public abstract class TrivialQuery implements Query + public abstract class Trivial implements Query { public abstract T invoke (Connection conn, DatabaseLiaison liaison) throws SQLException;