001: /*
002: $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/generator/DBToXML.java,v 1.7 2003/11/12 16:13:37 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.io.FileOutputStream;
024: import java.sql.*;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.HashMap;
028: import java.util.Properties;
029: import java.util.regex.Matcher;
030: import java.util.regex.Pattern;
031: import javax.sql.DataSource;
032: import org.jdom.*;
033: import org.jdom.output.XMLOutputter;
034: import org.xorm.XORM;
035: import org.xorm.datastore.sql.SQLConnectionInfo;
036: import org.xorm.datastore.sql.SQLType;
037:
038: /**
039: * Connects to the database and writes out the tables and columns it
040: * finds. Requires a recent JDBC driver. To execute, give the
041: * properties file as the first parameter. Additional parameters: Run
042: * with argument "-a" to append autoincrement to all primary keys.
043: * Run with "-s pattern" to generate sequence keys; pattern can
044: * contain "/C/" or "/T/", which means substitute the column or table
045: * name. Run with "-u" or "-l" to output all UPPER or all lower case
046: * for table and column names.
047: */
048: public class DBToXML {
049: public static void main(String[] argv) {
050: int exitCode = 0;
051: try {
052: DBToXML dtx = new DBToXML();
053: dtx.generateXML(argv);
054: } catch (Exception e) {
055: e.printStackTrace();
056: exitCode = 1;
057: } finally {
058: Runtime.getRuntime().exit(exitCode);
059: }
060: }
061:
062: public DBToXML() {
063: }
064:
065: public void generateXML(String[] argv) throws Exception {
066: int index = 0;
067: boolean autoincrement = false;
068: String tablePattern = null;
069: String sequencePattern = null;
070: String propertiesFileName = null;
071: String outputFileName = null;
072: boolean lower = false;
073: boolean upper = false;
074:
075: while (index < argv.length) {
076: if ("-a".equals(argv[index])) {
077: autoincrement = true;
078: ++index;
079: } else if ("-u".equals(argv[index])) {
080: upper = true;
081: ++index;
082: } else if ("-l".equals(argv[index])) {
083: lower = true;
084: ++index;
085: } else if ("-o".equals(argv[index])) {
086: outputFileName = argv[++index];
087: ++index;
088: } else if ("-s".equals(argv[index])) {
089: sequencePattern = argv[++index];
090: ++index;
091: } else if ("-t".equals(argv[index])) {
092: tablePattern = argv[++index];
093: ++index;
094: } else {
095: propertiesFileName = argv[index];
096: ++index;
097: }
098: }
099: if (propertiesFileName == null) {
100: System.out
101: .println("Usage: DBToXML [-u|-l] [-a] [-s pattern] xorm.properties");
102: System.exit(0);
103: }
104:
105: Element database = new Element("database");
106: Properties properties = new Properties();
107: properties.load(new FileInputStream(propertiesFileName));
108: SQLConnectionInfo info = new SQLConnectionInfo();
109: info.setProperties(properties);
110: DataSource dataSource = (DataSource) info.getDataSource();
111: Connection connection = dataSource.getConnection();
112: DatabaseMetaData metadata = connection.getMetaData();
113: ResultSet results;
114:
115: ArrayList sequences = new ArrayList();
116: String[] tableTypes = new String[1];
117:
118: // Read sequences if necessary
119: if (sequencePattern != null) {
120: tableTypes[0] = "SEQUENCE";
121: results = metadata.getTables(null, null, null, tableTypes);
122: while (results.next()) {
123: sequences.add(results.getString("TABLE_NAME"));
124: }
125: results.close();
126: }
127:
128: // Read list of tables
129: tableTypes[0] = "TABLE";
130: results = metadata.getTables(null, null, null, tableTypes);
131: ArrayList tables = new ArrayList();
132: while (results.next()) {
133: String tableName = results.getString("TABLE_NAME");
134: if (tablePattern != null && !tablePattern.equals("")
135: && !tableName.matches(tablePattern)) {
136: continue;
137: }
138: tables.add(results.getString("TABLE_NAME"));
139: }
140: results.close();
141:
142: // Read columns for each table
143: Iterator i = tables.iterator();
144: while (i.hasNext()) {
145: String tableName = (String) i.next();
146:
147: // Read primary keys
148: results = metadata.getPrimaryKeys(null, null, tableName);
149: HashMap tableToPK = new HashMap();
150: while (results.next()) {
151: String columnName = results.getString("COLUMN_NAME");
152: tableToPK.put(tableName, columnName);
153: }
154: results.close();
155:
156: Element table = new Element("table");
157: database.addContent(table);
158: String outputTableName = tableName;
159: if (upper)
160: outputTableName = tableName.toUpperCase();
161: else if (lower)
162: outputTableName = tableName.toLowerCase();
163:
164: table.setAttribute("name", outputTableName);
165: results = metadata.getColumns(null, null, tableName, null);
166: while (results.next()) {
167: Element column = new Element("column");
168: String columnName = results.getString("COLUMN_NAME");
169: String outputColumnName = columnName;
170: if (upper)
171: outputColumnName = columnName.toUpperCase();
172: if (lower)
173: outputColumnName = columnName.toLowerCase();
174: column.setAttribute("name", outputColumnName);
175: String typeName = SQLType.nameFor(results
176: .getInt("DATA_TYPE"));
177: if (typeName != null) {
178: column.setAttribute("type", typeName);
179: }
180: if (columnName.equals(tableToPK.get(tableName))) {
181: column.setAttribute("primary-key", "true");
182: if (autoincrement) {
183: column.setAttribute("auto", "true");
184: }
185: if (sequencePattern != null) {
186: String sequenceName = sequencePattern;
187: sequenceName = Pattern.compile("/C/").matcher(
188: sequenceName).replaceAll(columnName);
189: sequenceName = Pattern.compile("/T/").matcher(
190: sequenceName).replaceAll(tableName);
191: if (sequences.contains(sequenceName)) {
192: column.setAttribute("sequence",
193: sequenceName);
194: }
195: }
196: }
197: if ("NO".equals(results.getString("IS_NULLABLE"))) {
198: column.setAttribute("non-null", "true");
199: }
200: table.addContent(column);
201: }
202: results.close();
203: }
204: connection.close();
205:
206: Document document = new Document(database);
207: DocType dt = new DocType("database");
208: dt.setSystemID("database.dtd");
209: document.setDocType(dt);
210: XMLOutputter xml = new XMLOutputter(" ", true);
211: if (outputFileName == null) {
212: xml.output(document, System.out);
213: } else {
214: xml.output(document, new FileOutputStream(outputFileName));
215: }
216: }
217: }
|