%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.task.TorqueJDBCTransformTask |
|
|
1 | package org.apache.torque.task; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2004 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.io.FileOutputStream; |
|
20 | import java.io.PrintWriter; |
|
21 | ||
22 | import java.sql.Connection; |
|
23 | import java.sql.DatabaseMetaData; |
|
24 | import java.sql.DriverManager; |
|
25 | import java.sql.ResultSet; |
|
26 | import java.sql.SQLException; |
|
27 | import java.sql.Types; |
|
28 | ||
29 | import java.util.ArrayList; |
|
30 | import java.util.Collection; |
|
31 | import java.util.Hashtable; |
|
32 | import java.util.Iterator; |
|
33 | import java.util.List; |
|
34 | ||
35 | import org.apache.tools.ant.BuildException; |
|
36 | import org.apache.tools.ant.Task; |
|
37 | ||
38 | import org.apache.torque.engine.database.model.TypeMap; |
|
39 | import org.apache.torque.engine.database.transform.DTDResolver; |
|
40 | ||
41 | import org.apache.xerces.dom.DocumentImpl; |
|
42 | import org.apache.xerces.dom.DocumentTypeImpl; |
|
43 | ||
44 | import org.apache.xml.serialize.Method; |
|
45 | import org.apache.xml.serialize.OutputFormat; |
|
46 | import org.apache.xml.serialize.XMLSerializer; |
|
47 | ||
48 | import org.w3c.dom.Element; |
|
49 | import org.w3c.dom.Node; |
|
50 | ||
51 | /** |
|
52 | * This class generates an XML schema of an existing database from |
|
53 | * JDBC metadata. |
|
54 | * |
|
55 | * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> |
|
56 | * @author <a href="mailto:fedor.karpelevitch@barra.com">Fedor Karpelevitch</a> |
|
57 | * @version $Id: TorqueJDBCTransformTask.java,v 1.9 2005/06/14 20:51:13 tfischer Exp $ |
|
58 | */ |
|
59 | 0 | public class TorqueJDBCTransformTask extends Task |
60 | { |
|
61 | /** Name of XML database schema produced. */ |
|
62 | protected String xmlSchema; |
|
63 | ||
64 | /** JDBC URL. */ |
|
65 | protected String dbUrl; |
|
66 | ||
67 | /** JDBC driver. */ |
|
68 | protected String dbDriver; |
|
69 | ||
70 | /** JDBC user name. */ |
|
71 | protected String dbUser; |
|
72 | ||
73 | /** JDBC password. */ |
|
74 | protected String dbPassword; |
|
75 | ||
76 | /** DB schema to use. */ |
|
77 | protected String dbSchema; |
|
78 | ||
79 | /** DOM document produced. */ |
|
80 | protected DocumentImpl doc; |
|
81 | ||
82 | /** The document root element. */ |
|
83 | protected Element databaseNode; |
|
84 | ||
85 | /** Hashtable of columns that have primary keys. */ |
|
86 | protected Hashtable primaryKeys; |
|
87 | ||
88 | /** Hashtable to track what table a column belongs to. */ |
|
89 | protected Hashtable columnTableMap; |
|
90 | ||
91 | protected boolean sameJavaName; |
|
92 | ||
93 | private XMLSerializer xmlSerializer; |
|
94 | ||
95 | public String getDbSchema() |
|
96 | { |
|
97 | 0 | return dbSchema; |
98 | } |
|
99 | ||
100 | public void setDbSchema(String dbSchema) |
|
101 | { |
|
102 | 0 | this.dbSchema = dbSchema; |
103 | 0 | } |
104 | ||
105 | public void setDbUrl(String v) |
|
106 | { |
|
107 | 0 | dbUrl = v; |
108 | 0 | } |
109 | ||
110 | public void setDbDriver(String v) |
|
111 | { |
|
112 | 0 | dbDriver = v; |
113 | 0 | } |
114 | ||
115 | public void setDbUser(String v) |
|
116 | { |
|
117 | 0 | dbUser = v; |
118 | 0 | } |
119 | ||
120 | public void setDbPassword(String v) |
|
121 | { |
|
122 | 0 | dbPassword = v; |
123 | 0 | } |
124 | ||
125 | public void setOutputFile (String v) |
|
126 | { |
|
127 | 0 | xmlSchema = v; |
128 | 0 | } |
129 | ||
130 | public void setSameJavaName(boolean v) |
|
131 | { |
|
132 | 0 | this.sameJavaName = v; |
133 | 0 | } |
134 | ||
135 | public boolean isSameJavaName() |
|
136 | { |
|
137 | 0 | return this.sameJavaName; |
138 | } |
|
139 | ||
140 | /** |
|
141 | * Default constructor. |
|
142 | * |
|
143 | * @throws BuildException |
|
144 | */ |
|
145 | public void execute() throws BuildException |
|
146 | { |
|
147 | 0 | log("Torque - JDBCToXMLSchema starting"); |
148 | 0 | log("Your DB settings are:"); |
149 | 0 | log("driver : " + dbDriver); |
150 | 0 | log("URL : " + dbUrl); |
151 | 0 | log("user : " + dbUser); |
152 | // log("password : " + dbPassword); |
|
153 | 0 | log("schema : " + dbSchema); |
154 | ||
155 | 0 | DocumentTypeImpl docType = new DocumentTypeImpl(null, "database", null, |
156 | DTDResolver.WEB_SITE_DTD); |
|
157 | 0 | doc = new DocumentImpl(docType); |
158 | 0 | doc.appendChild(doc.createComment( |
159 | " Autogenerated by JDBCToXMLSchema! ")); |
|
160 | ||
161 | try |
|
162 | { |
|
163 | 0 | generateXML(); |
164 | 0 | log(xmlSchema); |
165 | 0 | xmlSerializer = new XMLSerializer( |
166 | new PrintWriter( |
|
167 | new FileOutputStream(xmlSchema)), |
|
168 | new OutputFormat(Method.XML, null, true)); |
|
169 | 0 | xmlSerializer.serialize(doc); |
170 | } |
|
171 | 0 | catch (Exception e) |
172 | { |
|
173 | 0 | throw new BuildException(e); |
174 | 0 | } |
175 | 0 | log("Torque - JDBCToXMLSchema finished"); |
176 | 0 | } |
177 | ||
178 | /** |
|
179 | * Generates an XML database schema from JDBC metadata. |
|
180 | * |
|
181 | * @throws Exception a generic exception. |
|
182 | */ |
|
183 | public void generateXML() throws Exception |
|
184 | { |
|
185 | // Load the Interbase Driver. |
|
186 | 0 | Class.forName(dbDriver); |
187 | 0 | log("DB driver sucessfuly instantiated"); |
188 | ||
189 | // Attemtp to connect to a database. |
|
190 | 0 | Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword); |
191 | 0 | log("DB connection established"); |
192 | ||
193 | // Get the database Metadata. |
|
194 | 0 | DatabaseMetaData dbMetaData = con.getMetaData(); |
195 | ||
196 | // The database map. |
|
197 | 0 | List tableList = getTableNames(dbMetaData); |
198 | ||
199 | 0 | databaseNode = doc.createElement("database"); |
200 | 0 | databaseNode.setAttribute("name", dbUser); |
201 | ||
202 | // Build a database-wide column -> table map. |
|
203 | 0 | columnTableMap = new Hashtable(); |
204 | ||
205 | 0 | log("Building column/table map..."); |
206 | 0 | for (int i = 0; i < tableList.size(); i++) |
207 | { |
|
208 | 0 | String curTable = (String) tableList.get(i); |
209 | 0 | List columns = getColumns(dbMetaData, curTable); |
210 | ||
211 | 0 | for (int j = 0; j < columns.size(); j++) |
212 | { |
|
213 | 0 | List col = (List) columns.get(j); |
214 | 0 | String name = (String) col.get(0); |
215 | ||
216 | 0 | columnTableMap.put(name, curTable); |
217 | } |
|
218 | } |
|
219 | ||
220 | 0 | for (int i = 0; i < tableList.size(); i++) |
221 | { |
|
222 | // Add Table. |
|
223 | 0 | String curTable = (String) tableList.get(i); |
224 | // dbMap.addTable(curTable); |
|
225 | 0 | log("Processing table: " + curTable); |
226 | ||
227 | 0 | Element table = doc.createElement("table"); |
228 | 0 | table.setAttribute("name", curTable); |
229 | 0 | if (isSameJavaName()) |
230 | { |
|
231 | 0 | table.setAttribute("javaName", curTable); |
232 | } |
|
233 | ||
234 | // Add Columns. |
|
235 | // TableMap tblMap = dbMap.getTable(curTable); |
|
236 | ||
237 | 0 | List columns = getColumns(dbMetaData, curTable); |
238 | 0 | List primKeys = getPrimaryKeys(dbMetaData, curTable); |
239 | 0 | Collection forgnKeys = getForeignKeys(dbMetaData, curTable); |
240 | ||
241 | // Set the primary keys. |
|
242 | 0 | primaryKeys = new Hashtable(); |
243 | ||
244 | 0 | for (int k = 0; k < primKeys.size(); k++) |
245 | { |
|
246 | 0 | String curPrimaryKey = (String) primKeys.get(k); |
247 | 0 | primaryKeys.put(curPrimaryKey, curPrimaryKey); |
248 | } |
|
249 | ||
250 | 0 | for (int j = 0; j < columns.size(); j++) |
251 | { |
|
252 | 0 | List col = (List) columns.get(j); |
253 | 0 | String name = (String) col.get(0); |
254 | 0 | Integer type = ((Integer) col.get(1)); |
255 | 0 | int size = ((Integer) col.get(2)).class="keyword">intValue(); |
256 | ||
257 | // From DatabaseMetaData.java |
|
258 | // |
|
259 | // Indicates column might not allow NULL values. Huh? |
|
260 | // Might? Boy, that's a definitive answer. |
|
261 | /* int columnNoNulls = 0; */ |
|
262 | ||
263 | // Indicates column definitely allows NULL values. |
|
264 | /* int columnNullable = 1; */ |
|
265 | ||
266 | // Indicates NULLABILITY of column is unknown. |
|
267 | /* int columnNullableUnknown = 2; */ |
|
268 | ||
269 | 0 | Integer nullType = (Integer) col.get(3); |
270 | 0 | String defValue = (String) col.get(4); |
271 | ||
272 | 0 | Element column = doc.createElement("column"); |
273 | 0 | column.setAttribute("name", name); |
274 | 0 | if (isSameJavaName()) |
275 | { |
|
276 | 0 | column.setAttribute("javaName", name); |
277 | } |
|
278 | 0 | column.setAttribute("type", TypeMap.getTorqueType(type).getName()); |
279 | ||
280 | 0 | if (size > 0 && (type.intValue() == Types.CHAR |
281 | || type.intValue() == Types.VARCHAR |
|
282 | || type.intValue() == Types.LONGVARCHAR |
|
283 | || type.intValue() == Types.DECIMAL |
|
284 | || type.intValue() == Types.NUMERIC)) |
|
285 | { |
|
286 | 0 | column.setAttribute("size", String.valueOf(size)); |
287 | } |
|
288 | ||
289 | 0 | if (nullType.intValue() == 0) |
290 | { |
|
291 | 0 | column.setAttribute("required", "true"); |
292 | } |
|
293 | ||
294 | 0 | if (primaryKeys.containsKey(name)) |
295 | { |
|
296 | 0 | column.setAttribute("primaryKey", "true"); |
297 | } |
|
298 | ||
299 | 0 | if (defValue != null) |
300 | { |
|
301 | // trim out parens & quotes out of def value. |
|
302 | // makes sense for MSSQL. not sure about others. |
|
303 | 0 | if (defValue.startsWith("(") && defValue.endsWith(")")) |
304 | { |
|
305 | 0 | defValue = defValue.substring(1, defValue.length() - 1); |
306 | } |
|
307 | ||
308 | 0 | if (defValue.startsWith("'") && defValue.endsWith("'")) |
309 | { |
|
310 | 0 | defValue = defValue.substring(1, defValue.length() - 1); |
311 | } |
|
312 | ||
313 | 0 | column.setAttribute("default", defValue); |
314 | } |
|
315 | 0 | table.appendChild(column); |
316 | } |
|
317 | ||
318 | // Foreign keys for this table. |
|
319 | 0 | for (Iterator l = class="keyword">forgnKeys.iterator(); l.hasNext();) |
320 | { |
|
321 | 0 | Object[] forKey = (Object[]) l.next(); |
322 | 0 | String foreignKeyTable = (String) forKey[0]; |
323 | 0 | List refs = (List) forKey[1]; |
324 | 0 | Element fk = doc.createElement("foreign-key"); |
325 | 0 | fk.setAttribute("foreignTable", foreignKeyTable); |
326 | 0 | for (int m = 0; m < refs.size(); m++) |
327 | { |
|
328 | 0 | Element ref = doc.createElement("reference"); |
329 | 0 | String[] refData = (String[]) refs.get(m); |
330 | 0 | ref.setAttribute("local", refData[0]); |
331 | 0 | ref.setAttribute("foreign", refData[1]); |
332 | 0 | fk.appendChild(ref); |
333 | } |
|
334 | 0 | table.appendChild(fk); |
335 | } |
|
336 | 0 | databaseNode.appendChild(table); |
337 | } |
|
338 | 0 | doc.appendChild(databaseNode); |
339 | 0 | } |
340 | ||
341 | /** |
|
342 | * Get all the table names in the current database that are not |
|
343 | * system tables. |
|
344 | * |
|
345 | * @param dbMeta JDBC database metadata. |
|
346 | * @return The list of all the tables in a database. |
|
347 | * @throws SQLException |
|
348 | */ |
|
349 | public List getTableNames(DatabaseMetaData dbMeta) |
|
350 | throws SQLException |
|
351 | { |
|
352 | 0 | log("Getting table list..."); |
353 | 0 | List tables = new ArrayList(); |
354 | 0 | ResultSet tableNames = null; |
355 | // these are the entity types we want from the database |
|
356 | 0 | String[] types = {"TABLE", "VIEW"}; |
357 | try |
|
358 | { |
|
359 | 0 | tableNames = dbMeta.getTables(null, dbSchema, "%", types); |
360 | 0 | while (tableNames.next()) |
361 | { |
|
362 | 0 | String name = tableNames.getString(3); |
363 | 0 | String type = tableNames.getString(4); |
364 | 0 | tables.add(name); |
365 | } |
|
366 | } |
|
367 | finally |
|
368 | { |
|
369 | 0 | if (tableNames != null) |
370 | { |
|
371 | 0 | tableNames.close(); |
372 | } |
|
373 | } |
|
374 | 0 | return tables; |
375 | } |
|
376 | ||
377 | /** |
|
378 | * Retrieves all the column names and types for a given table from |
|
379 | * JDBC metadata. It returns a List of Lists. Each element |
|
380 | * of the returned List is a List with: |
|
381 | * |
|
382 | * element 0 => a String object for the column name. |
|
383 | * element 1 => an Integer object for the column type. |
|
384 | * element 2 => size of the column. |
|
385 | * element 3 => null type. |
|
386 | * |
|
387 | * @param dbMeta JDBC metadata. |
|
388 | * @param tableName Table from which to retrieve column information. |
|
389 | * @return The list of columns in <code>tableName</code>. |
|
390 | * @throws SQLException |
|
391 | */ |
|
392 | public List getColumns(DatabaseMetaData dbMeta, String tableName) |
|
393 | throws SQLException |
|
394 | { |
|
395 | 0 | List columns = new ArrayList(); |
396 | 0 | ResultSet columnSet = null; |
397 | try |
|
398 | { |
|
399 | 0 | columnSet = dbMeta.getColumns(null, dbSchema, tableName, class="keyword">null); |
400 | 0 | while (columnSet.next()) |
401 | { |
|
402 | 0 | String name = columnSet.getString(4); |
403 | 0 | Integer sqlType = new Integer(columnSet.getString(5)); |
404 | 0 | Integer size = new Integer(columnSet.getInt(7)); |
405 | 0 | Integer nullType = new Integer(columnSet.getInt(11)); |
406 | 0 | String defValue = columnSet.getString(13); |
407 | ||
408 | 0 | List col = new ArrayList(5); |
409 | 0 | col.add(name); |
410 | 0 | col.add(sqlType); |
411 | 0 | col.add(size); |
412 | 0 | col.add(nullType); |
413 | 0 | col.add(defValue); |
414 | 0 | columns.add(col); |
415 | } |
|
416 | } |
|
417 | finally |
|
418 | { |
|
419 | 0 | if (columnSet != null) |
420 | { |
|
421 | 0 | columnSet.close(); |
422 | } |
|
423 | } |
|
424 | 0 | return columns; |
425 | } |
|
426 | ||
427 | /** |
|
428 | * Retrieves a list of the columns composing the primary key for a given |
|
429 | * table. |
|
430 | * |
|
431 | * @param dbMeta JDBC metadata. |
|
432 | * @param tableName Table from which to retrieve PK information. |
|
433 | * @return A list of the primary key parts for <code>tableName</code>. |
|
434 | * @throws SQLException |
|
435 | */ |
|
436 | public List getPrimaryKeys(DatabaseMetaData dbMeta, String tableName) |
|
437 | throws SQLException |
|
438 | { |
|
439 | 0 | List pk = new ArrayList(); |
440 | 0 | ResultSet parts = null; |
441 | try |
|
442 | { |
|
443 | 0 | parts = dbMeta.getPrimaryKeys(null, dbSchema, tableName); |
444 | 0 | while (parts.next()) |
445 | { |
|
446 | 0 | pk.add(parts.getString(4)); |
447 | } |
|
448 | } |
|
449 | finally |
|
450 | { |
|
451 | 0 | if (parts != null) |
452 | { |
|
453 | 0 | parts.close(); |
454 | } |
|
455 | } |
|
456 | 0 | return pk; |
457 | } |
|
458 | ||
459 | /** |
|
460 | * Retrieves a list of foreign key columns for a given table. |
|
461 | * |
|
462 | * @param dbMeta JDBC metadata. |
|
463 | * @param tableName Table from which to retrieve FK information. |
|
464 | * @return A list of foreign keys in <code>tableName</code>. |
|
465 | * @throws SQLException |
|
466 | */ |
|
467 | public Collection getForeignKeys(DatabaseMetaData dbMeta, String tableName) |
|
468 | throws SQLException |
|
469 | { |
|
470 | 0 | Hashtable fks = new Hashtable(); |
471 | 0 | ResultSet foreignKeys = null; |
472 | try |
|
473 | { |
|
474 | 0 | foreignKeys = dbMeta.getImportedKeys(null, dbSchema, tableName); |
475 | 0 | while (foreignKeys.next()) |
476 | { |
|
477 | 0 | String refTableName = foreignKeys.getString(3); |
478 | 0 | String fkName = foreignKeys.getString(12); |
479 | // if FK has no name - make it up (use tablename instead) |
|
480 | 0 | if (fkName == null) |
481 | { |
|
482 | 0 | fkName = refTableName; |
483 | } |
|
484 | 0 | Object[] fk = (Object[]) fks.get(fkName); |
485 | List refs; |
|
486 | 0 | if (fk == null) |
487 | { |
|
488 | 0 | fk = new Object[2]; |
489 | 0 | fk[0] = refTableName; //referenced table name |
490 | 0 | refs = new ArrayList(); |
491 | 0 | fk[1] = refs; |
492 | 0 | fks.put(fkName, fk); |
493 | } |
|
494 | else |
|
495 | { |
|
496 | 0 | refs = (ArrayList) fk[1]; |
497 | } |
|
498 | 0 | String[] ref = new String[2]; |
499 | 0 | ref[0] = foreignKeys.getString(8); //local column |
500 | 0 | ref[1] = foreignKeys.getString(4); //foreign column |
501 | 0 | refs.add(ref); |
502 | } |
|
503 | } |
|
504 | finally |
|
505 | { |
|
506 | 0 | if (foreignKeys != null) |
507 | { |
|
508 | 0 | foreignKeys.close(); |
509 | } |
|
510 | } |
|
511 | 0 | return fks.values(); |
512 | } |
|
513 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |