Don't try to build an UPDATE statement at all if no key were set on the record sent into store(). Also throw a sensible (if scaredy-cat) exception rather than an NPE if no WHERE clause were given to an UPDATE. We probably do want to allow rampant nutty 20,000-row UPDATEs at some point, but not because of an accidental null key.

This commit is contained in:
Par Winzell
2008-03-10 22:28:11 +00:00
parent 2783d31ca2
commit 7056f6c05c
3 changed files with 12 additions and 4 deletions
@@ -418,6 +418,9 @@ public abstract class BuildVisitor implements ExpressionVisitor
public void visit (UpdateClause<? extends PersistentRecord> updateClause)
throws Exception
{
if (updateClause.getWhereClause() == null) {
throw new SQLException("I dare not currently perform UPDATE without a WHERE clause.");
}
Class<? extends PersistentRecord> pClass = updateClause.getPersistentClass();
_innerClause = true;
@@ -572,26 +572,30 @@ public abstract class DepotRepository
final UpdateClause<T> update =
new UpdateClause<T>(pClass, key, marsh._columnFields, record);
final SQLBuilder builder = _ctx.getSQLBuilder(DepotTypes.getDepotTypes(_ctx, update));
builder.newQuery(update);
// if our primary key isn't null, we start by trying to update rather than insert
if (key != null) {
builder.newQuery(update);
}
final boolean[] created = new boolean[1];
_ctx.invoke(new CachingModifier<T>(record, key, key) {
public int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException {
PreparedStatement stmt = null;
try {
// if our primary key isn't null, update rather than insert the record before
// been persisted and insert
if (_key != null) {
// run the update
stmt = builder.prepare(conn);
int mods = stmt.executeUpdate();
if (mods > 0) {
// if it succeeded, we're done
return mods;
}
JDBCUtil.close(stmt);
}
// if the update modified zero rows or the primary key was obviously unset, do
// an insertion: first, set any auto-generated column values
// an insertion: first, set any auto-generated column values
Set<String> identityFields =
marsh.generateFieldValues(conn, liaison, _result, false);
@@ -89,6 +89,7 @@ public class Join extends QueryClause
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
classSet.add(_joinClass);
_joinCondition.addClasses(classSet);
}
/** Indicates the type of join to be performed. */