001: /*
002: $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/generator/MineDatabase.java,v 1.1 2003/09/19 06:45:16 dcheckoway Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.tools.generator;
021:
022: import java.io.FileInputStream;
023: import java.lang.reflect.Method;
024: import java.sql.Connection;
025: import java.sql.DatabaseMetaData;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.SQLException;
029: import java.util.ArrayList;
030: import java.util.Collections;
031: import java.util.Comparator;
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.Properties;
035: import javax.sql.DataSource;
036: import org.xorm.datastore.sql.SQLConnectionInfo;
037: import org.xorm.datastore.sql.SQLType;
038:
039: public class MineDatabase {
040: static HashMap methodToAttribute = new HashMap();
041:
042: static String getPropertyName(Method method) {
043: String attrName = (String) methodToAttribute.get(method
044: .getName());
045: if (attrName == null) {
046: attrName = method.getName();
047: if (attrName.startsWith("get")) {
048: boolean capitalize = false;
049: if (attrName.length() > 4) {
050: char c = attrName.charAt(4);
051: capitalize = c >= 'A' && c <= 'Z';
052: }
053: if (capitalize) {
054: attrName = attrName.substring(3);
055: } else {
056: attrName = Character
057: .toLowerCase(attrName.charAt(3))
058: + attrName.substring(4);
059: }
060: }
061: methodToAttribute.put(method.getName(), attrName);
062: }
063: return attrName;
064: }
065:
066: static void displayResultSet(String name, ResultSet results)
067: throws SQLException {
068: ResultSetMetaData md = results.getMetaData();
069: int numColumns = md.getColumnCount();
070: System.out.println("<h3>" + name + "</h3>");
071: if (!results.next()) {
072: System.out.println("NO DATA");
073: } else {
074: System.out
075: .println("<table border=1 cellpadding=1 cellspacing=0>");
076: System.out.println("<tr>");
077: for (int k = 1; k <= md.getColumnCount(); ++k) {
078: String columnName = md.getColumnName(k);
079: System.out.println("<th>" + md.getColumnName(k)
080: + "</th>");
081: }
082: System.out.println("</tr>");
083: do {
084: System.out.println("<tr>");
085: for (int k = 1; k <= md.getColumnCount(); ++k) {
086: System.out.println("<td>" + results.getString(k)
087: + "</td>");
088: }
089: System.out.println("</tr>");
090: } while (results.next());
091: System.out.println("</table>");
092: }
093: results.close();
094: }
095:
096: public static void main(String[] argv) {
097: int exitCode = 0;
098: Connection connection = null;
099: try {
100: String propertiesFileName = null;
101:
102: for (int k = 0; k < argv.length; ++k) {
103: if (argv[k].startsWith("-prop")) {
104: propertiesFileName = argv[++k];
105: } else {
106: System.out
107: .println("Unrecognized command line option: "
108: + argv[k]);
109: }
110: }
111:
112: if (propertiesFileName == null
113: || propertiesFileName.equals("")) {
114: System.out
115: .println("Usage: MineDatabase -properties path/to/xorm.properties");
116: Runtime.getRuntime().exit(1);
117: }
118:
119: Properties properties = new Properties();
120: properties.load(new FileInputStream(propertiesFileName));
121: SQLConnectionInfo info = new SQLConnectionInfo();
122: info.setProperties(properties);
123: DataSource dataSource = (DataSource) info.getDataSource();
124: connection = dataSource.getConnection();
125: DatabaseMetaData md = connection.getMetaData();
126:
127: System.out
128: .println("<html><head><title>Database MetaData</title></head><body>");
129: System.out.println("<h1>Database MetaData</h1>");
130:
131: // First call all the simple (no arg) methods
132: ArrayList simpleMethods = new ArrayList();
133: Method[] methods = DatabaseMetaData.class.getMethods();
134: for (int k = 0; k < methods.length; ++k) {
135: if (methods[k].getParameterTypes().length == 0
136: && methods[k].getReturnType() != ResultSet.class) {
137: simpleMethods.add(methods[k]);
138: }
139: }
140: Collections.sort(simpleMethods, new Comparator() {
141: public int compare(Object o1, Object o2) {
142: Method m1 = (Method) o1;
143: Method m2 = (Method) o2;
144: return String.CASE_INSENSITIVE_ORDER.compare(
145: getPropertyName(m1), getPropertyName(m2));
146: }
147: });
148: System.out
149: .println("<p><b>Attributes:</b><br/><table border=1 cellpadding=1 cellspacing=0><tr><th>Attribute</th><th>Value</th></tr>");
150: for (Iterator iter = simpleMethods.iterator(); iter
151: .hasNext();) {
152: Method method = (Method) iter.next();
153: String attrName = getPropertyName(method);
154: try {
155: Object result = method.invoke(md, new Object[0]);
156: System.out.println("<tr><td>" + attrName
157: + "</td><td>" + String.valueOf(result)
158: + "</td></tr>");
159: } catch (Throwable t) {
160: System.out.println("<tr><td>" + attrName
161: + "</td><td>" + "Not available: "
162: + t.getClass().getName() + "</td></tr>");
163: }
164: }
165: System.out.println("</table></p>");
166:
167: // Now dive in deeper
168:
169: // Read list of tables
170: ResultSet results = md.getTables(null, null, null,
171: new String[] { "TABLE" });
172: ArrayList tables = new ArrayList();
173: while (results.next()) {
174: tables.add(results.getString("TABLE_NAME"));
175: }
176: results.close();
177:
178: for (Iterator iter = tables.iterator(); iter.hasNext();) {
179: String tableName = (String) iter.next();
180: System.out.println("<hr/>");
181: System.out.println("<h2>Table: " + tableName + "</h2>");
182: displayResultSet("Columns", md.getColumns(null, null,
183: tableName, null));
184: displayResultSet("PrimaryKeys", md.getPrimaryKeys(null,
185: null, tableName));
186: displayResultSet("ExportedKeys", md.getExportedKeys(
187: null, null, tableName));
188: displayResultSet("ImportedKeys", md.getImportedKeys(
189: null, null, tableName));
190: displayResultSet("IndexInfo", md.getIndexInfo(null,
191: null, tableName, false, false));
192: displayResultSet("TablePrivileges", md
193: .getTablePrivileges(null, null, tableName));
194: displayResultSet("VersionColumns", md
195: .getVersionColumns(null, null, tableName));
196: }
197:
198: System.out.println("<hr/>");
199: System.out.println("<h2>Other Database Attributes:</h2>");
200:
201: displayResultSet("TableTypes", md.getTableTypes());
202: System.out.println("<hr/>");
203: displayResultSet("TypeInfo", md.getTypeInfo());
204: System.out.println("<hr/>");
205: displayResultSet("Catalogs", md.getCatalogs());
206: System.out.println("<hr/>");
207: displayResultSet("Schemas", md.getSchemas());
208:
209: System.out.println("</body></html>");
210: } catch (Throwable t) {
211: t.printStackTrace();
212: exitCode = 1;
213: } finally {
214: if (connection != null) {
215: try {
216: connection.close();
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220: }
221: Runtime.getRuntime().exit(exitCode);
222: }
223: }
224: }
|