currentErrors.clear();
currentErrors.putAll(validateAll());
if (currentErrors.size() > 0)
{
throw new PersistenceException("Field validation errors in persistent '" + myMetaData.getName() + "': ",
getErrors());
}
boolean needComma = false;
/* build an update statement - again we must call haveKeyFields */
/* to be sure we have all of the keys */
haveAllKeys(true, true);
StringBuffer sqlCommand = new StringBuffer("UPDATE ");
sqlCommand.append(myMetaData.getTableName());
sqlCommand.append(" SET ");
for (Iterator i = myMetaData.getFieldNames().iterator(); i.hasNext();)
{
String oneFieldName = (String) i.next();
/* We skip any key fields in the update part (not allowed to */
/* upate them). Also skip any virtual fields */
if ((! myMetaData.isKeyField(oneFieldName)) && (! myMetaData.isAutoIncremented(oneFieldName)))
{
checkField(oneFieldName, getField(oneFieldName));
if (needComma)
{
sqlCommand.append(", ");
}
if (! myMetaData.isKeyField(oneFieldName))
{
sqlCommand.append(myMetaData.getDBFieldName(oneFieldName));
sqlCommand.append(" = ");
//updated by Adam to do proper prep for update SQL
//sqlCommand.append(quoteIfNeeded(oneFieldName, null));
sqlCommand.append("?");
sqlCommand.append(" ");
needComma = true;
}
} /* if it's not a key field */
}
sqlCommand.append(buildWhereClauseBuffer(false));
if (sqlCommand == null)
{
throw new PersistenceException("Internal error: (" + getName() + ") No SQL command built for this update");
}
PreparedStatement ps = null;
Connection myConnection = null;
//Statement s = null;
try
{
if (currentTransaction != null)
{
myConnection = currentTransaction.getConnection();
}
else
{
myConnection = myDataSource.getConnection();
}
ps = myConnection.prepareStatement(sqlCommand.toString());
// populate the fields for the update
int count = 1;
String oneFieldName = null;
for (Iterator i = myMetaData.getFieldNames().iterator(); i.hasNext();)
{
oneFieldName = (String) i.next();
if ((! myMetaData.isKeyField(oneFieldName)) && (! myMetaData.isAutoIncremented(oneFieldName)))
{
setPreparedStatementObject(ps, count, oneFieldName);
count++;
}
//}
} /* for each field */
//s = myConnection.createStatement();
if (log.isDebugEnabled())
{
log.debug("Executing: " + sqlCommand.toString());
}
//int updateCount = s.executeUpdate(sqlCommand.toString());
int updateCount = ps.executeUpdate();
if ((updateCount == 0) && (getCheckZeroUpdate()))
{
log.error("No record updated for SQL '" + sqlCommand.toString() + "'");
throw new PersistenceException("(" + getName() + ") No records updated for" + toString());
}
}
catch (PersistenceException de)
{
throw de;
}
catch (SQLException se)
{
throw new PersistenceException(se);
}
finally
{
cleanUp(myConnection, null, null, ps);
}