1 | |
package org.apache.torque.engine.database.model; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
import java.util.ArrayList; |
17 | |
import java.util.Collections; |
18 | |
import java.util.HashMap; |
19 | |
import java.util.Hashtable; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.commons.collections.map.ListOrderedMap; |
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
import org.apache.torque.engine.EngineException; |
28 | |
import org.apache.torque.engine.database.transform.DTDResolver; |
29 | |
import org.apache.torque.engine.platform.Platform; |
30 | |
import org.apache.torque.engine.platform.PlatformFactory; |
31 | |
import org.xml.sax.Attributes; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class Database { |
45 | |
|
46 | 0 | private static Log log = LogFactory.getLog(Database.class); |
47 | |
|
48 | 0 | private String databaseType = null; |
49 | 0 | private List tableList = new ArrayList(100); |
50 | 0 | private Map domainMap = new HashMap(); |
51 | |
private String name; |
52 | |
private String javaName; |
53 | |
private String pkg; |
54 | |
private String baseClass; |
55 | |
private String basePeer; |
56 | |
private String defaultIdMethod; |
57 | |
private String defaultJavaType; |
58 | |
private String defaultJavaNamingMethod; |
59 | 0 | private Hashtable tablesByName = new Hashtable(); |
60 | 0 | private Hashtable tablesByJavaName = new Hashtable(); |
61 | |
private boolean heavyIndexing; |
62 | |
|
63 | |
private String fileName; |
64 | 0 | private Map options = Collections.synchronizedMap(new ListOrderedMap()); |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 0 | public Database(String databaseType) { |
73 | 0 | this.databaseType = databaseType; |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void loadFromXML(Attributes attrib) { |
83 | 0 | setName(attrib.getValue("name")); |
84 | 0 | pkg = attrib.getValue("package"); |
85 | 0 | baseClass = attrib.getValue("baseClass"); |
86 | 0 | basePeer = attrib.getValue("basePeer"); |
87 | 0 | defaultJavaType = attrib.getValue("defaultJavaType"); |
88 | 0 | defaultIdMethod = attrib.getValue("defaultIdMethod"); |
89 | 0 | defaultJavaNamingMethod = attrib.getValue("defaultJavaNamingMethod"); |
90 | 0 | if (defaultJavaNamingMethod == null) { |
91 | 0 | defaultJavaNamingMethod = NameGenerator.CONV_METHOD_UNDERSCORE; |
92 | |
} |
93 | 0 | heavyIndexing = "true".equals(attrib.getValue("heavyIndexing")); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public String getName() { |
102 | 0 | return name; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void setName(String name) { |
112 | |
|
113 | |
|
114 | 0 | this.name = (name == null ? "default" : name); |
115 | 0 | } |
116 | |
|
117 | |
public String getFileName() { |
118 | 0 | return fileName; |
119 | |
} |
120 | |
|
121 | |
public void setFileName(String name) { |
122 | 0 | this.fileName = name; |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public String getPackage() { |
131 | 0 | return pkg; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public void setPackage(String v) { |
141 | 0 | this.pkg = v; |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public String getBaseClass() { |
150 | 0 | if (baseClass == null) { |
151 | 0 | return "BaseObject"; |
152 | |
} |
153 | 0 | return baseClass; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public void setBaseClass(String v) { |
163 | 0 | this.baseClass = v; |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public String getBasePeer() { |
172 | 0 | if (basePeer == null) { |
173 | 0 | return "BasePeer"; |
174 | |
} |
175 | 0 | return basePeer; |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public void setBasePeer(String v) { |
185 | 0 | this.basePeer = v; |
186 | 0 | } |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public String getDefaultIdMethod() { |
194 | 0 | return defaultIdMethod; |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public void setDefaultIdMethod(String v) { |
204 | 0 | this.defaultIdMethod = v; |
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public String getDefaultJavaType() { |
213 | 0 | return defaultJavaType; |
214 | |
} |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public String getDefaultJavaNamingMethod() { |
223 | 0 | return defaultJavaNamingMethod; |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public void setDefaultJavaNamingMethod(String v) { |
233 | 0 | this.defaultJavaNamingMethod = v; |
234 | 0 | } |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public boolean isHeavyIndexing() { |
242 | 0 | return heavyIndexing; |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public void setHeavyIndexing(boolean v) { |
252 | 0 | this.heavyIndexing = v; |
253 | 0 | } |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public List getTables() { |
261 | 0 | return tableList; |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public Table getTable(String name) { |
272 | 0 | return (Table) tablesByName.get(name); |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
public Table getTableByJavaName(String javaName) { |
283 | 0 | return (Table) tablesByJavaName.get(javaName); |
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public Table addTable(Attributes attrib) { |
294 | 0 | Table tbl = new Table(); |
295 | 0 | tbl.setDatabase(this); |
296 | 0 | tbl.loadFromXML(attrib, this.getDefaultIdMethod()); |
297 | 0 | addTable(tbl); |
298 | 0 | return tbl; |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
public void addTable(Table tbl) { |
308 | 0 | tbl.setDatabase(this); |
309 | 0 | tbl.setPackage(getPackage()); |
310 | 0 | tableList.add(tbl); |
311 | 0 | List<String> tableNames = getTableNames(tbl); |
312 | 0 | for (String tableName : tableNames) { |
313 | 0 | tablesByName.put(tableName, tbl); |
314 | |
} |
315 | 0 | tablesByJavaName.put(tbl.getJavaName(), tbl); |
316 | 0 | } |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
public void removeTable(Table tbl) { |
322 | 0 | removeFromList(tbl); |
323 | 0 | List<String> tableNames = getTableNames(tbl); |
324 | 0 | for (String tableName : tableNames) { |
325 | 0 | tablesByName.remove(tableName); |
326 | |
} |
327 | 0 | tablesByJavaName.remove(tbl.getJavaName()); |
328 | 0 | } |
329 | |
|
330 | |
protected List<String> getTableNames(Table table) { |
331 | 0 | List<String> tableNames = new ArrayList<String>(); |
332 | 0 | tableNames.add(table.getName()); |
333 | 0 | tableNames.add(table.getName().toLowerCase()); |
334 | 0 | tableNames.add(table.getName().toUpperCase()); |
335 | 0 | return tableNames; |
336 | |
} |
337 | |
|
338 | |
protected void removeFromList(Table targetTable) { |
339 | 0 | for (int i = 0; i < tableList.size(); i++) { |
340 | 0 | Table table = (Table) tableList.get(i); |
341 | 0 | String name1 = table.getName(); |
342 | 0 | String name2 = targetTable.getName(); |
343 | 0 | if (name1.equalsIgnoreCase(name2)) { |
344 | 0 | tableList.remove(i); |
345 | 0 | return; |
346 | |
} |
347 | |
} |
348 | |
|
349 | 0 | } |
350 | |
|
351 | |
public void addDomain(Domain domain) { |
352 | 0 | domainMap.put(domain.getName(), domain); |
353 | 0 | } |
354 | |
|
355 | |
public Domain getDomain(String domainName) { |
356 | 0 | return (Domain) domainMap.get(domainName); |
357 | |
} |
358 | |
|
359 | |
protected String getDatabaseType() { |
360 | 0 | return databaseType; |
361 | |
} |
362 | |
|
363 | |
public void setDatabaseType(String databaseType) { |
364 | 0 | this.databaseType = databaseType; |
365 | 0 | } |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
public Platform getPlatform() { |
373 | 0 | return PlatformFactory.getPlatformFor(databaseType); |
374 | |
} |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
public boolean requiresIdTable() { |
384 | 0 | Iterator iter = getTables().iterator(); |
385 | 0 | while (iter.hasNext()) { |
386 | 0 | Table table = (Table) iter.next(); |
387 | 0 | if (table.getIdMethod().equals(IDMethod.ID_BROKER)) { |
388 | 0 | return true; |
389 | |
} |
390 | 0 | } |
391 | 0 | return false; |
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
public void doFinalInitialization() throws EngineException { |
400 | 0 | Iterator iter = getTables().iterator(); |
401 | 0 | while (iter.hasNext()) { |
402 | 0 | Table currTable = (Table) iter.next(); |
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | 0 | if (currTable.getIdMethod().equals("autoincrement")) { |
410 | 0 | boolean foundOne = false; |
411 | 0 | Iterator colIter = currTable.getColumns().iterator(); |
412 | 0 | while (colIter.hasNext() && !foundOne) { |
413 | 0 | foundOne = ((Column) colIter.next()).isAutoIncrement(); |
414 | |
} |
415 | |
|
416 | 0 | if (!foundOne) { |
417 | 0 | String errorMessage = "Table '" + currTable.getName() |
418 | |
+ "' is marked as autoincrement, but it does not " |
419 | |
+ "have a column which declared as the one to " |
420 | |
+ "auto increment (i.e. autoIncrement=\"true\")\n"; |
421 | 0 | throw new EngineException("Error in XML schema: " + errorMessage); |
422 | |
} |
423 | |
} |
424 | |
|
425 | 0 | currTable.doFinalInitialization(); |
426 | |
|
427 | |
|
428 | 0 | Iterator fks = currTable.getForeignKeys().iterator(); |
429 | 0 | while (fks.hasNext()) { |
430 | 0 | ForeignKey currFK = (ForeignKey) fks.next(); |
431 | 0 | Table foreignTable = getTable(currFK.getForeignTableName()); |
432 | 0 | if (foreignTable == null) { |
433 | 0 | throw new EngineException("Attempt to set foreign" + " key to nonexistent table, " |
434 | |
+ currFK.getForeignTableName()); |
435 | |
} else { |
436 | |
|
437 | 0 | List referrers = foreignTable.getReferrers(); |
438 | 0 | if ((referrers == null || !referrers.contains(currFK))) { |
439 | 0 | foreignTable.addReferrer(currFK); |
440 | |
} |
441 | |
|
442 | |
|
443 | 0 | Iterator localColumnNames = currFK.getLocalColumns().iterator(); |
444 | 0 | while (localColumnNames.hasNext()) { |
445 | 0 | Column local = currTable.getColumn((String) localColumnNames.next()); |
446 | |
|
447 | |
|
448 | |
|
449 | 0 | if (local == null) { |
450 | 0 | throw new EngineException("Attempt to define foreign" |
451 | |
+ " key with nonexistent column in table, " + currTable.getName()); |
452 | |
} else { |
453 | |
|
454 | 0 | if (local.isPrimaryKey()) { |
455 | 0 | currTable.setContainsForeignPK(true); |
456 | |
} |
457 | |
} |
458 | 0 | } |
459 | |
|
460 | |
|
461 | 0 | Iterator foreignColumnNames = currFK.getForeignColumns().iterator(); |
462 | 0 | while (foreignColumnNames.hasNext()) { |
463 | 0 | String foreignColumnName = (String) foreignColumnNames.next(); |
464 | 0 | Column foreign = foreignTable.getColumn(foreignColumnName); |
465 | |
|
466 | |
|
467 | 0 | if (foreign == null) { |
468 | 0 | throw new EngineException("Attempt to set foreign" + " key to nonexistent column: table=" |
469 | |
+ currTable.getName() + ", foreign column=" + foreignColumnName); |
470 | |
} else { |
471 | 0 | foreign.addReferrer(currFK); |
472 | |
} |
473 | 0 | } |
474 | |
} |
475 | 0 | } |
476 | 0 | } |
477 | 0 | } |
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
public String getJavaName() { |
486 | 0 | if (javaName == null) { |
487 | 0 | List inputs = new ArrayList(2); |
488 | 0 | inputs.add(name); |
489 | 0 | inputs.add(defaultJavaNamingMethod); |
490 | |
try { |
491 | 0 | javaName = NameFactory.generateName(NameFactory.JAVA_GENERATOR, inputs); |
492 | 0 | } catch (EngineException e) { |
493 | 0 | log.error(e, e); |
494 | 0 | } |
495 | |
} |
496 | 0 | return javaName; |
497 | |
} |
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
public String getStandardJavaName() { |
505 | 0 | if (javaName == null) { |
506 | 0 | List inputs = new ArrayList(2); |
507 | 0 | inputs.add(name); |
508 | 0 | inputs.add(NameGenerator.CONV_METHOD_JAVANAME); |
509 | |
try { |
510 | 0 | javaName = NameFactory.generateName(NameFactory.JAVA_GENERATOR, inputs); |
511 | 0 | } catch (EngineException e) { |
512 | 0 | log.error(e, e); |
513 | 0 | } |
514 | |
} |
515 | 0 | return javaName; |
516 | |
} |
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
@Override |
524 | |
public String toString() { |
525 | 0 | StringBuffer result = new StringBuffer(); |
526 | |
|
527 | 0 | result.append("<?xml version=\"1.0\"?>\n"); |
528 | 0 | result.append("<!DOCTYPE database SYSTEM \"" + DTDResolver.WEB_SITE_DTD + "\">\n"); |
529 | 0 | result.append("<!-- Autogenerated by SQLToXMLSchema! -->\n"); |
530 | 0 | result.append("<database name=\"").append(getName()).append('"').append(" package=\"").append(getPackage()) |
531 | |
.append('"').append(" defaultIdMethod=\"").append(getDefaultIdMethod()).append('"') |
532 | |
.append(" baseClass=\"").append(getBaseClass()).append('"').append(" basePeer=\"") |
533 | |
.append(getBasePeer()).append('"').append(">\n"); |
534 | |
|
535 | 0 | for (Iterator i = tableList.iterator(); i.hasNext();) { |
536 | 0 | result.append(i.next()); |
537 | |
} |
538 | |
|
539 | 0 | result.append("</database>"); |
540 | 0 | return result.toString(); |
541 | |
} |
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
public void addOption(String key, String value) { |
552 | 0 | options.put(key, value); |
553 | 0 | } |
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
public String getOption(String key) { |
563 | 0 | return (String) options.get(key); |
564 | |
} |
565 | |
|
566 | |
|
567 | |
|
568 | |
|
569 | |
|
570 | |
|
571 | |
|
572 | |
|
573 | |
|
574 | |
|
575 | |
public Map getOptions() { |
576 | 0 | return options; |
577 | |
} |
578 | |
} |