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