stmt.setNull(count++, columns[j].getSqlType());
}
} else if (value instanceof Identity) {
Identity id = (Identity) value;
if (id.size() != columns.length) {
throw new PersistenceException("Size of identity field mismatch!");
}
for (int j = 0; j < columns.length; j++) {
SQLTypeInfos.setValue(stmt, count++,
columns[j].toSQL(id.get(j)), columns[j].getSqlType());
}
} else {
if (columns.length != 1) {
throw new PersistenceException("Complex field expected!");
}
SQLTypeInfos.setValue(stmt, count++, columns[0].toSQL(value),
columns[0].getSqlType());
}
}
}
// bind the identity of the row to be stored into the preparedStatement
SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities();
if (identity.size() != ids.length) {
throw new PersistenceException("Size of identity field mismatched!");
}
for (int i = 0; i < ids.length; i++) {
stmt.setObject(count++, ids[i].toSQL(identity.get(i)));
if (LOG.isTraceEnabled()) {
LOG.trace(Messages.format("jdo.bindingIdentity", ids[i].getName(),
ids[i].toSQL(identity.get(i))));
}
}
// bind the old fields of the row to be stored into the preparedStatement
if (oldentity.getFields() != null) {
boolean supportsSetNull = _factory.supportsSetNullInWhere();
for (int i = 0; i < fields.length; ++i) {
if (fields[i].isStore() && fields[i].isDirtyCheck()) {
SQLColumnInfo[] columns = fields[i].getColumnInfo();
Object value = oldentity.getField(i);
if (value == null) {
if (supportsSetNull) {
for (int j = 0; j < columns.length; j++) {
stmt.setNull(count++, columns[j].getSqlType());
}
}
} else if (value instanceof Identity) {
Identity id = (Identity) value;
if (id.size() != columns.length) {
throw new PersistenceException(
"Size of identity field mismatch!");
}
for (int j = 0; j < columns.length; j++) {
SQLTypeInfos.setValue(stmt, count++,
columns[j].toSQL(id.get(j)), columns[j].getSqlType());
if (LOG.isTraceEnabled()) {
LOG.trace(Messages.format("jdo.bindingField",
columns[j].getName(), columns[j].toSQL(id.get(j))));
}
}
} else {
if (columns.length != 1) {
throw new PersistenceException("Complex field expected!");
}
SQLTypeInfos.setValue(stmt, count++, columns[0].toSQL(value),
columns[0].getSqlType());
if (LOG.isTraceEnabled()) {
LOG.trace(Messages.format("jdo.bindingField",
columns[0].getName(), columns[0].toSQL(value)));
}
}
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.format("jdo.storing", _type, stmt.toString()));
}
if (stmt.executeUpdate() <= 0) { // SAP DB returns -1 here
// If no update was performed, the object has been previously
// removed from persistent storage or has been modified if
// dirty checking. Determine which is which.
stmt.close();
if (oldentity.getFields() != null) {
stmt = conn.prepareStatement(_statementLoad);
if (LOG.isTraceEnabled()) {
LOG.trace(Messages.format("jdo.storing", _type, stmt.toString()));
}
// bind the identity to the prepareStatement
count = 1;
for (int i = 0; i < ids.length; i++) {
stmt.setObject(count++, ids[i].toSQL(identity.get(i)));
}
ResultSet res = stmt.executeQuery();
if (res.next()) {
StringBuffer enlistFieldsNotMatching = new StringBuffer();
int numberOfFieldsNotMatching = 0;
for (int i = 0; i < fields.length; i++) {
SQLColumnInfo[] columns = fields[i].getColumnInfo();
Object value = oldentity.getField(i);
Object currentField = columns[0].toJava(res.getObject(
columns[0].getName()));
if (fields[i].getTableName().compareTo(_mapTo) == 0) {
if ((value == null) || ((value != null)
&& (currentField == null))) {
enlistFieldsNotMatching.append("(" + _type + ")."
+ columns[0].getName() + ": ");
enlistFieldsNotMatching.append("[" + value + "/"
+ currentField + "]");
} else if (!value.equals(currentField)) {
if (numberOfFieldsNotMatching >= 1) {
enlistFieldsNotMatching.append(", ");
}
enlistFieldsNotMatching.append("(" + _type + ")."
+ columns[0].getName() + ": ");
enlistFieldsNotMatching.append("[" + value + "/"
+ currentField + "]");
numberOfFieldsNotMatching++;
}
}
}
throw new ObjectModifiedException(Messages.format(
"persist.objectModified", _type, identity,
enlistFieldsNotMatching.toString()));
}
}
throw new ObjectDeletedException(Messages.format(
"persist.objectDeleted", _type, identity));
}
} catch (SQLException except) {
LOG.fatal(Messages.format("jdo.storeFatal", _type, storeStatement), except);
throw new PersistenceException(Messages.format("persist.nested", except), except);
} finally {
try {
// Close the insert/select statement
if (stmt != null) { stmt.close(); }
} catch (SQLException except2) {