01: /*
02: * This is free software, licensed under the Gnu Public License (GPL)
03: * get a copy from <http://www.gnu.org/licenses/gpl.html>
04: */
05: package henplus.plugins.tablediff;
06:
07: import henplus.sqlmodel.Column;
08: import henplus.sqlmodel.Table;
09:
10: import java.util.ListIterator;
11:
12: /**
13: * <p>Title: TableDiffer</p>
14: * <p>Description:<br>
15: * Created on: 24.07.2003</p>
16: * @version $Id: TableDiffer.java,v 1.2 2004/01/27 18:16:33 hzeller Exp $
17: * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
18: */
19: public final class TableDiffer {
20:
21: /**
22: * Compares two tables by their columns.
23: * @param referenceTable
24: * @param diffTable
25: * @param colNameIgnoreCase specifies if column names shall be compared in a case insensitive way.
26: * @return An instance of <code>TableDiffResult</code> if their are
27: * differences between the tables, otherwise <code>null</code>.
28: */
29: public static TableDiffResult diffTables(Table referenceTable,
30: Table diffTable, boolean colNameIgnoreCase) {
31: TableDiffResult result = null;
32:
33: if (referenceTable != null && diffTable != null) {
34: result = new TableDiffResult();
35: // first check for all columns of the reference table
36: ListIterator refIter = referenceTable.getColumnIterator();
37: if (refIter != null) {
38: while (refIter.hasNext()) {
39: Column col = (Column) refIter.next();
40: // System.out.println("[TableDiffer.diffTables] querying table for " + col.getName());
41: Column diff = diffTable.getColumnByName(col
42: .getName(), colNameIgnoreCase);
43: // System.out.println("[TableDiffer.diffTables] got: " + diff);
44: if (diff == null) {
45: // System.out.println("missing col: " + col.getName());
46: result.addRemovedColumn(col);
47: } else if (!col.equals(diff, colNameIgnoreCase)) {
48: // System.out.println("modified col: " + col.getName());
49: result.putModifiedColumns(col, diff);
50: }
51: }
52: }
53: // now check for columns which were added to the second table
54: ListIterator diffIter = diffTable.getColumnIterator();
55: if (diffIter != null) {
56: while (diffIter.hasNext()) {
57: Column col = (Column) diffIter.next();
58: Column ref = referenceTable.getColumnByName(col
59: .getName(), colNameIgnoreCase);
60: if (ref == null) {
61: // System.out.println("added col: " + col.getName());
62: result.addAddedColumn(col);
63: }
64: }
65: }
66: result = (result.hasDiffs()) ? result : null;
67: }
68: return result;
69: }
70:
71: }
|