EVarId evarId = new EVarId(newval.locator.pid, newval.locator.varId);
DbExternalVariable evar = _vars.get(evarId);
if (evar == null)
throw new ExternalVariableModuleException("No such variable. "); // todo
RowKey key = evar.keyFromLocator(newval.locator);
RowVal val = evar.parseXmlRow(evar.new RowVal(), (Element) newval.value);
if (__log.isDebugEnabled())
__log.debug("JdbcExternalVariable.writeValue() RowKey: " + key + " RowVal: " + val);
if (!key.missingValues() && evar._initType == InitType.delete_insert) {
// do delete...
throw new ExternalVariableModuleException("Delete not implemented. "); // todo
}
// should we try an update first? to do this we need to have all the required keys
// and there should be some keys
boolean tryupdatefirst = (evar._initType == InitType.update || evar._initType == InitType.update_insert)
&& !evar._keycolumns.isEmpty() && !key.missingDatabaseGeneratedValues();
boolean insert = evar._initType != InitType.update;
if (__log.isDebugEnabled())
__log.debug("tryUpdateFirst: " + tryupdatefirst
+ " insert: " + insert
+ " initType: " + evar._initType
+ " key.isEmpty: " + evar._keycolumns.isEmpty()
+ " key.missingValues: " + key.missingValues()
+ " key.missingDBValues: " + key.missingDatabaseGeneratedValues());
try {
if (tryupdatefirst)
insert = execUpdate(evar, key, val) == 0;
if (insert) {
key = execInsert(evar, newval.locator, key, val);
// Transfer the keys obtained from the db.
key.write(varType, newval.locator);
}
} catch (SQLException se) {
throw new ExternalVariableModuleException("Error updating row.", se);
}