shadowRsHasRecords = shadowRs.next();
}
private void update(Table table, String sourcePk, SourceResultSet sourceRs, ShadowResultSet shadowRs) throws SQLException {
List<Transaction> transactionList = new ArrayList<Transaction>();
LoggableTransaction loggableTransaction = new LoggableTransaction(table, TransactionType.UPDATE);
List<LoggableTransactionDatum> loggableTransactionDatumList = new ArrayList<LoggableTransactionDatum>();
// String pk = null;
for (Column column : table.getColumnList()) {
/*
* Ensure cells associated with new columns are created
*/
// if (pk == null) {
// pk = sourceRs.getString("PK");
// }
Cell shadowCell = shadowRs.getCell(column);
String sourceColumnValue = sourceRs.getString(column);
if (shadowCell == null) {
Cell cell = new Cell(shadowRs.getCell("PK").getData(), sourceColumnValue);
cell.setColumn(column);
transactionList.add(new DataTransaction(cell, TransactionType.INSERT));
loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));
continue;
}
/*
* Judge which columns we need to compare
*/
String shadowColumnValue = shadowRs.getCell(column).getData();
boolean pkColumn = table.getPk().contains(column.getName() + ",");
boolean compare = (shadowColumnValue != null && sourceColumnValue != null)//compare only if both values are not null and this column is not (part of) the primary key
&& !pkColumn;
boolean ignore = (shadowColumnValue == null && sourceColumnValue == null);//if both values are null, no update has taken place
boolean update = false;
if (compare) {
update = !shadowColumnValue.equals(sourceColumnValue);//if both values are not equal then an update has taken place
} else {
if (!pkColumn) {
update = !ignore;//if only one value is null then an update has taken place
}
}
if (update) {
Cell cell = shadowRs.getCell(column);
cell.setData(sourceColumnValue);
cell.setColumn(column);
transactionList.add(new DataTransaction(cell, TransactionType.UPDATE));
loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));
}
}
if (!loggableTransactionDatumList.isEmpty()) {
// String pk = null;
Cell cell = shadowRs.getCell("PK");
// cell.setData(sourceRs.getString("PK"));
cell.setData(sourcePk);
//TODO: Accommodate cases where the pk is not the first column of the table
cell.setColumn(table.getColumnList().get(0));
loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));
loggableTransaction.setLoggableTransactionDatumList(loggableTransactionDatumList);
transactionList.add(loggableTransaction);
}
processTransactions(transactionList);
sourceRsHasRecords = sourceRs.next();
shadowRsHasRecords = shadowRs.next();