001: package net.sourceforge.squirrel_sql.plugins.dbdiff;
002:
003: import java.sql.SQLException;
004: import java.util.ArrayList;
005: import java.util.Comparator;
006: import java.util.HashMap;
007: import java.util.HashSet;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
013: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
014: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
015:
016: public class TableDiffExecutor {
017:
018: private ISQLDatabaseMetaData _md1;
019: private ISQLDatabaseMetaData _md2;
020:
021: private ITableInfo _t1;
022: private ITableInfo _t2;
023:
024: private List<ColumnDifference> colDifferences = null;
025:
026: public TableDiffExecutor(ISQLDatabaseMetaData md1,
027: ISQLDatabaseMetaData md2) {
028: _md1 = md1;
029: _md2 = md2;
030: }
031:
032: public void setTableInfos(ITableInfo t1, ITableInfo t2) {
033: _t1 = t1;
034: _t2 = t2;
035: if (colDifferences != null) {
036: colDifferences.clear();
037: }
038: }
039:
040: public void execute() throws SQLException {
041: colDifferences = new ArrayList<ColumnDifference>();
042: TableColumnInfo[] t1cols = _md1.getColumnInfo(_t1);
043: TableColumnInfo[] t2cols = _md2.getColumnInfo(_t2);
044: Set<String> columnNames = getAllColumnNames(t1cols, t2cols);
045: Set<String> t1ColumnNames = getAllColumnNames(t1cols);
046: Map<String, TableColumnInfo> t1ColMap = getColumnMap(t1cols);
047: Set<String> t2ColumnNames = getAllColumnNames(t2cols);
048: Map<String, TableColumnInfo> t2ColMap = getColumnMap(t2cols);
049:
050: for (String columnName : columnNames) {
051: ColumnDifference diff = new ColumnDifference();
052: if (t1ColumnNames.contains(columnName)) {
053: TableColumnInfo c1 = t1ColMap.get(columnName);
054:
055: if (t2ColumnNames.contains(columnName)) { // Column is in both table 1 and 2
056: TableColumnInfo c2 = t2ColMap.get(columnName);
057: diff.setColumns(c1, c2);
058: } else {
059: // Column is in table 1, but not table 2
060: diff.setCol2Exists(false);
061: diff.setColumn1(c1);
062: }
063: } else {
064: // Column is in table 2, but not table 1 - how else would we get
065: // here??
066: diff.setCol1Exists(false);
067: diff.setColumn2(t2ColMap.get(columnName));
068: }
069: if (diff.execute()) {
070: colDifferences.add(diff);
071: }
072: }
073: }
074:
075: public List<ColumnDifference> getColumnDifferences() {
076: return colDifferences;
077: }
078:
079: private Map<String, TableColumnInfo> getColumnMap(
080: TableColumnInfo[] tci) {
081: HashMap<String, TableColumnInfo> result = new HashMap<String, TableColumnInfo>();
082: for (int i = 0; i < tci.length; i++) {
083: TableColumnInfo info = tci[i];
084: result.put(info.getColumnName(), info);
085: }
086: return result;
087: }
088:
089: /**
090: * Build a list of all table column names.
091: *
092: * @param tci1
093: * @param tci2
094: * @return
095: */
096: private Set<String> getAllColumnNames(TableColumnInfo[] tci1,
097: TableColumnInfo[] tci2) {
098: HashSet<String> result = new HashSet<String>();
099: result.addAll(getAllColumnNames(tci1));
100: result.addAll(getAllColumnNames(tci2));
101: return result;
102: }
103:
104: private Set<String> getAllColumnNames(TableColumnInfo[] tci) {
105: HashSet<String> result = new HashSet<String>();
106: for (int i = 0; i < tci.length; i++) {
107: TableColumnInfo info = tci[i];
108: result.add(info.getColumnName());
109: }
110: return result;
111: }
112:
113: private static class ColumnComparator implements
114: Comparator<TableColumnInfo> {
115:
116: /* (non-Javadoc)
117: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
118: */
119: public int compare(TableColumnInfo arg0, TableColumnInfo arg1) {
120: return arg0.getColumnName().compareToIgnoreCase(
121: arg1.getColumnName());
122: }
123:
124: }
125: }
|