if (ti == null)
return TI_ERROR_MESSAGE;
// get the SQL session
final ISession session = _session;
final ISQLConnection conn = session.getSQLConnection();
// string used as error indicator and description of problems seen
// when checking for 0 or mulitple matches in DB
String rowCountErrorMessage = "";
// for each row in table, count how many rows match where clause
// if not exactly one, generate message describing situation
for (int i = 0; i < rowData.length; i++) {
// get WHERE clause for the selected row
// the -1 says to just use the contents of the values without
// any substitutions
String whereClause = getWhereClause(rowData[i], colDefs, -1, null);
// count how many rows this WHERE matches
try {
// do the delete and add the number of rows deleted to the count
final Statement stmt = conn.createStatement();
try
{
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM " +
ti.getQualifiedName()+whereClause);
rs.next();
if (rs.getInt(1) != 1) {
if (rs.getInt(1) == 0) {
// i18n[DataSetUpdateableTableModelImpl.error.rownotmatch=\n Row {0} did not match any row in DB]
rowCountErrorMessage +=
s_stringMgr.getString(
"DataSetUpdateableTableModelImpl.error.rownotmatch",
Integer.valueOf(i+1));
} else {
//i18n[DataSetUpdateableTableModelImpl.error.rowmatched=\n Row {0} matched {1} rows in DB]
rowCountErrorMessage +=
s_stringMgr.getString(
"DataSetUpdateableTableModelImpl.error.rowmatched",
new Object[] { Integer.valueOf(i+1), Integer.valueOf(rs.getInt(1)) });
}
}
}
finally
{
stmt.close();
}
}
catch (Exception e) {
// some kind of problem - tell user
// i18n[DataSetUpdateableTableModelImpl.error.preparingdelete=While preparing for delete, saw exception:\n{0}]
return
s_stringMgr.getString(
"DataSetUpdateableTableModelImpl.error.preparingdelete",
e);
}
}
// if the rows do not match 1-for-1 to DB, ask user if they
// really want to do delete
if (rowCountErrorMessage.length() > 0) {
// i18n[DataSetUpdateableTableModelImpl.error.tabledbmismatch=There may be a mismatch between the table and the DB:\n{0}\nDo you wish to proceed with the deletes anyway?]
String msg =
s_stringMgr.getString("DataSetUpdateableTableModelImpl.error.tabledbmismatch",
rowCountErrorMessage);
int option =
JOptionPane.showConfirmDialog(null, msg, "Warning",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if ( option != JOptionPane.YES_OPTION) {
// i18n[DataSetUpdateableTableModelImpl.info.deletecancelled=Delete canceled at user request.]
return s_stringMgr.getString("DataSetUpdateableTableModelImpl.info.deletecancelled");
}
}
// for each row in table, do delete and add to number of rows deleted from DB
for (int i = 0; i < rowData.length; i++) {
// get WHERE clause for the selected row
// the -1 says to just use the contents of the values without
// any substitutions
String whereClause = getWhereClause(rowData[i], colDefs, -1, null);
// try to delete
try {
// do the delete and add the number of rows deleted to the count
final Statement stmt = conn.createStatement();
try
{
stmt.executeUpdate("DELETE FROM " +
ti.getQualifiedName() + whereClause);
}