View Javadoc

1   package org.kuali.ole.utility;
2   
3   import java.sql.Connection;
4   import java.sql.DatabaseMetaData;
5   import java.sql.DriverManager;
6   import java.sql.PreparedStatement;
7   import java.sql.ResultSet;
8   import java.sql.ResultSetMetaData;
9   import java.sql.SQLException;
10  import java.util.ArrayList;
11  import java.util.Collections;
12  import java.util.List;
13  
14  public class DatabaseComparer {
15  
16      public static void main(String[] args) {
17          new DatabaseComparer().execute();
18      }
19  
20      public void execute() {
21          Credentials c1 = getOleFsAntCredentials();
22          Credentials c2 = getOleFsMvnCredentials();
23          Connection conn1 = getConnection(c1);
24          Connection conn2 = getConnection(c2);
25          List<Table> tables1 = getTables(conn1);
26          List<Table> tables2 = getTables(conn2);
27          System.out.println(tables1.size() + " " + tables2.size());
28          boolean equals = equals(tables1, tables2);
29          System.out.println(equals);
30      }
31  
32      /**
33       * Return true if the tables have the same name and the same number of rows. False otherwise.
34       */
35      public boolean equals(Table table1, Table table2) {
36          String name1 = table1.getName();
37          String name2 = table2.getName();
38          int rows1 = table1.getRowCount();
39          int rows2 = table2.getRowCount();
40          return name1.equals(name2) && rows1 == rows2;
41      }
42  
43      public boolean equals(List<Table> tables1, List<Table> tables2) {
44          // Make sure the lists are the same size
45          if (tables1.size() != tables2.size()) {
46              return false;
47          }
48  
49          // Both lists are the same size, so just grab the size from one of them
50          int size = tables1.size();
51  
52          // Sort both lists on table name
53          Collections.sort(tables1);
54          Collections.sort(tables2);
55  
56          // Iterate through the list and compare the table from each db
57          for (int i = 0; i < size; i++) {
58              Table table1 = tables1.get(i);
59              Table table2 = tables2.get(i);
60              boolean equals = equals(table1, table2);
61              if (!equals) {
62                  return false;
63              }
64          }
65          return true;
66      }
67  
68      public Credentials getOleFsAntCredentials() {
69          Credentials c = new Credentials();
70          c.setUsername("olefsant");
71          c.setPassword("olefsant");
72          c.setUrl("jdbc:mysql://localhost/olefsant");
73          return c;
74      }
75  
76      public Credentials getOleFsMvnCredentials() {
77          Credentials c = new Credentials();
78          c.setUsername("olefsmvn");
79          c.setPassword("olefsmvn");
80          c.setUrl("jdbc:mysql://localhost/olefsmvn");
81          return c;
82      }
83  
84      public Connection getConnection(Credentials c) {
85          try {
86              Class.forName("com.mysql.jdbc.Driver");
87              return DriverManager.getConnection(c.getUrl(), c.getUsername(), c.getPassword());
88          } catch (Throwable e) {
89              throw new IllegalArgumentException(e);
90          }
91      }
92  
93      protected void showResultSet(ResultSet rs) {
94          try {
95              ResultSetMetaData md = rs.getMetaData();
96              int columnCount = md.getColumnCount();
97              for (int i = 0; i < columnCount; i++) {
98                  int columnIndex = i + 1;
99                  String name = md.getColumnName(columnIndex);
100                 System.out.print(name + " ");
101             }
102             System.out.println();
103         } catch (SQLException e) {
104             throw new IllegalStateException(e);
105         }
106     }
107 
108     protected int getRowCount(Connection c, Table table) {
109         String sql = "select count(*) from " + table.getName();
110         PreparedStatement s = null;
111         try {
112             s = c.prepareStatement(sql);
113             ResultSet rs = s.executeQuery();
114             rs.next();
115             return rs.getInt(1);
116         } catch (SQLException e) {
117             throw new IllegalStateException(e);
118         }
119 
120     }
121 
122     protected List<Table> getTables(Connection c) {
123         try {
124             DatabaseMetaData md = c.getMetaData();
125             ResultSet rs = md.getTables(null, null, null, null);
126             List<Table> tables = new ArrayList<Table>();
127             int count = 0;
128             while (rs.next()) {
129                 count++;
130                 String name = rs.getString(3);
131                 Table table = new Table();
132                 table.setName(name);
133                 int rowCount = getRowCount(c, table);
134                 table.setRowCount(rowCount);
135                 tables.add(table);
136                 if ((count % 300) == 0) {
137                     System.out.println();
138                 } else {
139                     System.out.print(".");
140                 }
141             }
142             System.out.println();
143             return tables;
144         } catch (SQLException e) {
145             throw new IllegalStateException(e);
146         }
147     }
148 }