| 1 | |
package org.kuali.core.db.torque; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileOutputStream; |
| 5 | |
import java.io.PrintWriter; |
| 6 | |
import java.io.Writer; |
| 7 | |
import java.sql.Connection; |
| 8 | |
import java.sql.DatabaseMetaData; |
| 9 | |
import java.sql.ResultSet; |
| 10 | |
import java.sql.SQLException; |
| 11 | |
import java.sql.Types; |
| 12 | |
import java.util.ArrayList; |
| 13 | |
import java.util.HashMap; |
| 14 | |
import java.util.List; |
| 15 | |
import java.util.Map; |
| 16 | |
|
| 17 | |
import org.apache.commons.io.FilenameUtils; |
| 18 | |
import org.apache.commons.io.IOUtils; |
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.apache.tools.ant.BuildException; |
| 21 | |
import org.apache.tools.ant.Project; |
| 22 | |
import org.apache.torque.engine.database.model.TypeMap; |
| 23 | |
import org.apache.torque.engine.platform.Platform; |
| 24 | |
import org.apache.torque.engine.platform.PlatformFactory; |
| 25 | |
import org.apache.xerces.dom.DocumentImpl; |
| 26 | |
import org.apache.xerces.dom.DocumentTypeImpl; |
| 27 | |
import org.apache.xml.serialize.Method; |
| 28 | |
import org.apache.xml.serialize.OutputFormat; |
| 29 | |
import org.apache.xml.serialize.XMLSerializer; |
| 30 | |
import org.kuali.core.db.torque.pojo.ForeignKey; |
| 31 | |
import org.kuali.core.db.torque.pojo.Column; |
| 32 | |
import org.kuali.core.db.torque.pojo.Reference; |
| 33 | |
import org.kuali.core.db.torque.pojo.Index; |
| 34 | |
|
| 35 | |
import static org.kuali.db.JDBCUtils.*; |
| 36 | |
import org.w3c.dom.Element; |
| 37 | |
|
| 38 | 0 | public class KualiTorqueSchemaDumpTask extends DumpTask { |
| 39 | |
|
| 40 | 0 | boolean processTables = true; |
| 41 | 0 | boolean processViews = true; |
| 42 | 0 | boolean processSequences = true; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
File schemaXMLFile; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
DocumentImpl doc; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
Element databaseNode; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
Map<String, String> primaryKeys; |
| 63 | |
|
| 64 | |
protected void showConfiguration() { |
| 65 | 0 | super.showConfiguration(); |
| 66 | 0 | log("Exporting to: " + schemaXMLFile.getAbsolutePath()); |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
protected String getSystemId() { |
| 70 | 0 | if (antCompatibilityMode) { |
| 71 | 0 | return "database.dtd"; |
| 72 | |
} else { |
| 73 | 0 | return ImpexDTDResolver.DTD_LOCATION; |
| 74 | |
} |
| 75 | |
} |
| 76 | |
|
| 77 | |
protected DocumentImpl getDocumentImpl() { |
| 78 | 0 | DocumentTypeImpl docType = new DocumentTypeImpl(null, "database", null, getSystemId()); |
| 79 | 0 | DocumentImpl doc = new DocumentImpl(docType); |
| 80 | 0 | doc.appendChild(doc.createComment(" " + getComment() + " ")); |
| 81 | 0 | return doc; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public void execute() throws BuildException { |
| 88 | |
try { |
| 89 | 0 | log("--------------------------------------"); |
| 90 | 0 | log("Impex - Schema Export"); |
| 91 | 0 | log("--------------------------------------"); |
| 92 | 0 | log("Loading platform for " + getTargetDatabase()); |
| 93 | 0 | Platform platform = PlatformFactory.getPlatformFor(targetDatabase); |
| 94 | 0 | updateConfiguration(platform); |
| 95 | 0 | showConfiguration(); |
| 96 | 0 | doc = getDocumentImpl(); |
| 97 | 0 | generateXML(platform); |
| 98 | 0 | serialize(); |
| 99 | 0 | } catch (Exception e) { |
| 100 | 0 | throw new BuildException(e); |
| 101 | 0 | } |
| 102 | 0 | log("Impex - Schema Export finished"); |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
protected void serialize() throws BuildException { |
| 106 | 0 | Writer out = null; |
| 107 | |
try { |
| 108 | 0 | File file = new File(FilenameUtils.getFullPath(getSchemaXMLFile().getCanonicalPath())); |
| 109 | 0 | File parentDirectory = file.getParentFile(); |
| 110 | 0 | if (!parentDirectory.exists()) { |
| 111 | 0 | boolean mkdirs = parentDirectory.mkdirs(); |
| 112 | 0 | if (!mkdirs) { |
| 113 | 0 | throw new BuildException("Unable to create " + parentDirectory.getAbsolutePath()); |
| 114 | |
} |
| 115 | |
} |
| 116 | 0 | out = new PrintWriter(new FileOutputStream(getSchemaXMLFile())); |
| 117 | 0 | OutputFormat format = new OutputFormat(Method.XML, getEncoding(), true); |
| 118 | 0 | XMLSerializer xmlSerializer = new XMLSerializer(out, format); |
| 119 | 0 | xmlSerializer.serialize(doc); |
| 120 | 0 | } catch (Exception e) { |
| 121 | 0 | throw new BuildException("Error serializing", e); |
| 122 | |
} finally { |
| 123 | 0 | IOUtils.closeQuietly(out); |
| 124 | 0 | } |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
protected Map<String, String> getPrimaryKeys(Platform platform, DatabaseMetaData dbMetaData, String curTable) |
| 128 | |
throws SQLException { |
| 129 | 0 | List<String> primKeys = platform.getPrimaryKeys(dbMetaData, schema, curTable); |
| 130 | |
|
| 131 | |
|
| 132 | 0 | Map<String, String> primaryKeys = new HashMap<String, String>(); |
| 133 | 0 | for (int k = 0; k < primKeys.size(); k++) { |
| 134 | 0 | String curPrimaryKey = (String) primKeys.get(k); |
| 135 | 0 | primaryKeys.put(curPrimaryKey, curPrimaryKey); |
| 136 | |
} |
| 137 | 0 | return primaryKeys; |
| 138 | |
} |
| 139 | |
|
| 140 | |
protected Element getColumnElement(Column col, String curTable) { |
| 141 | 0 | String name = col.getName(); |
| 142 | 0 | Integer type = col.getSqlType(); |
| 143 | 0 | int size = col.getSize(); |
| 144 | 0 | int scale = col.getDecimalDigits(); |
| 145 | |
|
| 146 | 0 | Integer nullType = col.getNullType(); |
| 147 | 0 | String defValue = col.getDefValue(); |
| 148 | |
|
| 149 | 0 | Element column = doc.createElement("column"); |
| 150 | 0 | column.setAttribute("name", name); |
| 151 | |
|
| 152 | 0 | column.setAttribute("type", TypeMap.getTorqueType(type).getName()); |
| 153 | |
|
| 154 | 0 | if (size > 0 |
| 155 | |
&& (type.intValue() == Types.CHAR || type.intValue() == Types.VARCHAR |
| 156 | |
|| type.intValue() == Types.LONGVARCHAR || type.intValue() == Types.DECIMAL || type.intValue() == Types.NUMERIC)) { |
| 157 | 0 | column.setAttribute("size", String.valueOf(size)); |
| 158 | |
} |
| 159 | |
|
| 160 | 0 | if (scale > 0 && (type.intValue() == Types.DECIMAL || type.intValue() == Types.NUMERIC)) { |
| 161 | 0 | column.setAttribute("scale", String.valueOf(scale)); |
| 162 | |
} |
| 163 | |
|
| 164 | 0 | if (primaryKeys.containsKey(name)) { |
| 165 | 0 | column.setAttribute("primaryKey", "true"); |
| 166 | |
|
| 167 | |
|
| 168 | 0 | if (column.getAttribute("size") != null && size > 765) { |
| 169 | 0 | log("updating column " + curTable + "." + name + " length from " + size + " to 255"); |
| 170 | 0 | column.setAttribute("size", "255"); |
| 171 | |
} |
| 172 | |
} else { |
| 173 | 0 | if (nullType.intValue() == DatabaseMetaData.columnNoNulls) { |
| 174 | 0 | column.setAttribute("required", "true"); |
| 175 | |
} |
| 176 | |
} |
| 177 | |
|
| 178 | 0 | if (StringUtils.isNotBlank(defValue)) { |
| 179 | 0 | defValue = getDefaultValue(defValue); |
| 180 | 0 | column.setAttribute("default", defValue); |
| 181 | |
} |
| 182 | 0 | return column; |
| 183 | |
} |
| 184 | |
|
| 185 | |
protected String getDefaultValue(String defValue) { |
| 186 | 0 | if (StringUtils.isBlank(defValue)) { |
| 187 | 0 | return null; |
| 188 | |
} |
| 189 | 0 | defValue = defValue.trim(); |
| 190 | |
|
| 191 | |
|
| 192 | 0 | if (defValue.startsWith("(") && defValue.endsWith(")")) { |
| 193 | 0 | defValue = defValue.substring(1, defValue.length() - 1); |
| 194 | |
} |
| 195 | |
|
| 196 | 0 | if (defValue.startsWith("'") && defValue.endsWith("'")) { |
| 197 | 0 | defValue = defValue.substring(1, defValue.length() - 1); |
| 198 | |
} |
| 199 | 0 | if (defValue.equals("NULL")) { |
| 200 | 0 | defValue = ""; |
| 201 | |
} |
| 202 | 0 | return defValue; |
| 203 | |
} |
| 204 | |
|
| 205 | |
protected void processColumns(DatabaseMetaData dbMetaData, String curTable, Element table) throws SQLException { |
| 206 | 0 | List<Column> columns = getColumns(dbMetaData, curTable); |
| 207 | 0 | for (Column column : columns) { |
| 208 | 0 | Element columnElement = getColumnElement(column, curTable); |
| 209 | 0 | table.appendChild(columnElement); |
| 210 | 0 | } |
| 211 | 0 | } |
| 212 | |
|
| 213 | |
protected void processForeignKeys(DatabaseMetaData dbMetaData, String curTable, Element table) throws SQLException { |
| 214 | 0 | Map<String, ForeignKey> foreignKeys = getForeignKeys(dbMetaData, curTable); |
| 215 | |
|
| 216 | |
|
| 217 | 0 | for (String fkName : foreignKeys.keySet()) { |
| 218 | 0 | Element fk = getForeignKeyElement(fkName, foreignKeys); |
| 219 | 0 | table.appendChild(fk); |
| 220 | 0 | } |
| 221 | 0 | } |
| 222 | |
|
| 223 | |
protected Element getForeignKeyElement(String fkName, Map<String, ForeignKey> foreignKeys) { |
| 224 | 0 | Element fk = doc.createElement("foreign-key"); |
| 225 | 0 | fk.setAttribute("name", fkName); |
| 226 | 0 | ForeignKey forKey = foreignKeys.get(fkName); |
| 227 | 0 | String foreignKeyTable = forKey.getRefTableName(); |
| 228 | 0 | List<Reference> refs = forKey.getReferences(); |
| 229 | 0 | fk.setAttribute("foreignTable", foreignKeyTable); |
| 230 | 0 | String onDelete = forKey.getOnDelete(); |
| 231 | |
|
| 232 | 0 | if (onDelete == "cascade") { |
| 233 | 0 | fk.setAttribute("onDelete", onDelete); |
| 234 | |
} |
| 235 | 0 | for (Reference refData : refs) { |
| 236 | 0 | Element ref = doc.createElement("reference"); |
| 237 | 0 | ref.setAttribute("local", refData.getLocalColumn()); |
| 238 | 0 | ref.setAttribute("foreign", refData.getForeignColumn()); |
| 239 | 0 | fk.appendChild(ref); |
| 240 | 0 | } |
| 241 | 0 | return fk; |
| 242 | |
} |
| 243 | |
|
| 244 | |
protected void processIndexes(DatabaseMetaData dbMetaData, String curTable, Element table) throws SQLException { |
| 245 | 0 | for (Index idx : getIndexes(dbMetaData, curTable)) { |
| 246 | 0 | String tagName = idx.isUnique() ? "unique" : "index"; |
| 247 | 0 | Element index = doc.createElement(tagName); |
| 248 | 0 | index.setAttribute("name", idx.getName()); |
| 249 | 0 | for (String colName : idx.getColumns()) { |
| 250 | 0 | Element col = doc.createElement(tagName + "-column"); |
| 251 | 0 | col.setAttribute("name", colName); |
| 252 | 0 | index.appendChild(col); |
| 253 | 0 | } |
| 254 | 0 | table.appendChild(index); |
| 255 | 0 | } |
| 256 | 0 | } |
| 257 | |
|
| 258 | |
protected void processTable(String curTable, Platform platform, DatabaseMetaData dbMetaData) throws SQLException { |
| 259 | 0 | long start = System.currentTimeMillis(); |
| 260 | |
|
| 261 | 0 | Element table = doc.createElement("table"); |
| 262 | 0 | table.setAttribute("name", curTable); |
| 263 | |
|
| 264 | |
|
| 265 | 0 | primaryKeys = getPrimaryKeys(platform, dbMetaData, curTable); |
| 266 | |
|
| 267 | |
|
| 268 | 0 | processColumns(dbMetaData, curTable, table); |
| 269 | |
|
| 270 | |
|
| 271 | 0 | processForeignKeys(dbMetaData, curTable, table); |
| 272 | |
|
| 273 | |
|
| 274 | 0 | processIndexes(dbMetaData, curTable, table); |
| 275 | |
|
| 276 | |
|
| 277 | 0 | databaseNode.appendChild(table); |
| 278 | |
|
| 279 | 0 | log(utils.pad("Processed " + curTable, System.currentTimeMillis() - start)); |
| 280 | 0 | } |
| 281 | |
|
| 282 | |
protected void processTables(Platform platform, DatabaseMetaData dbMetaData) throws SQLException { |
| 283 | 0 | if (!processTables) { |
| 284 | 0 | return; |
| 285 | |
} |
| 286 | |
|
| 287 | 0 | List<String> tableList = platform.getTableNames(dbMetaData, schema); |
| 288 | 0 | log("Found " + tableList.size() + " tables"); |
| 289 | 0 | StringFilter filterer = new StringFilter(includePatterns, excludePatterns); |
| 290 | 0 | filterer.filter(tableList.iterator()); |
| 291 | 0 | log("Processing " + tableList.size() + " tables after filtering is applied"); |
| 292 | |
|
| 293 | 0 | for (String curTable : tableList) { |
| 294 | 0 | processTable(curTable, platform, dbMetaData); |
| 295 | |
} |
| 296 | 0 | } |
| 297 | |
|
| 298 | |
protected void processViews(Platform platform, DatabaseMetaData dbMetaData) throws SQLException { |
| 299 | 0 | if (!processViews) { |
| 300 | 0 | return; |
| 301 | |
} |
| 302 | 0 | List<String> viewNames = getViewNames(dbMetaData); |
| 303 | 0 | for (String viewName : viewNames) { |
| 304 | 0 | Element view = doc.createElement("view"); |
| 305 | 0 | view.setAttribute("name", viewName); |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | 0 | String definition = platform.getViewDefinition(dbMetaData.getConnection(), schema, viewName); |
| 310 | 0 | definition = definition.replaceAll("\0", ""); |
| 311 | 0 | view.setAttribute("viewdefinition", definition); |
| 312 | 0 | databaseNode.appendChild(view); |
| 313 | 0 | } |
| 314 | 0 | } |
| 315 | |
|
| 316 | |
protected void processSequences(Platform platform, DatabaseMetaData dbMetaData) throws SQLException { |
| 317 | 0 | if (!processSequences) { |
| 318 | 0 | return; |
| 319 | |
} |
| 320 | 0 | List<String> sequenceNames = getSequenceNames(dbMetaData); |
| 321 | 0 | for (String sequenceName : sequenceNames) { |
| 322 | 0 | Element sequence = doc.createElement("sequence"); |
| 323 | 0 | sequence.setAttribute("name", sequenceName); |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | 0 | Long nextVal = platform.getSequenceNextVal(dbMetaData.getConnection(), schema, sequenceName); |
| 328 | 0 | sequence.setAttribute("nextval", nextVal.toString()); |
| 329 | |
|
| 330 | 0 | databaseNode.appendChild(sequence); |
| 331 | 0 | } |
| 332 | 0 | doc.appendChild(databaseNode); |
| 333 | 0 | } |
| 334 | |
|
| 335 | |
protected String getName() { |
| 336 | 0 | return artifactId; |
| 337 | |
} |
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
protected void generateXML(Platform platform) throws Exception { |
| 346 | |
|
| 347 | 0 | Connection connection = null; |
| 348 | |
try { |
| 349 | |
|
| 350 | 0 | connection = getConnection(); |
| 351 | |
|
| 352 | |
|
| 353 | 0 | DatabaseMetaData dbMetaData = connection.getMetaData(); |
| 354 | |
|
| 355 | 0 | databaseNode = doc.createElement("database"); |
| 356 | 0 | databaseNode.setAttribute("name", getName()); |
| 357 | |
|
| 358 | 0 | databaseNode.setAttribute("defaultJavaNamingMethod", "nochange"); |
| 359 | |
|
| 360 | 0 | processTables(platform, dbMetaData); |
| 361 | 0 | processViews(platform, dbMetaData); |
| 362 | 0 | processSequences(platform, dbMetaData); |
| 363 | |
} finally { |
| 364 | 0 | closeQuietly(connection); |
| 365 | 0 | } |
| 366 | 0 | } |
| 367 | |
|
| 368 | |
public List<String> getViewNames(DatabaseMetaData dbMeta) throws SQLException { |
| 369 | 0 | log("Getting view list..."); |
| 370 | 0 | List<String> tables = new ArrayList<String>(); |
| 371 | 0 | ResultSet tableNames = null; |
| 372 | |
|
| 373 | 0 | String[] types = { "VIEW" }; |
| 374 | |
try { |
| 375 | 0 | tableNames = dbMeta.getTables(null, schema, null, types); |
| 376 | 0 | while (tableNames.next()) { |
| 377 | 0 | String name = tableNames.getString(3); |
| 378 | 0 | tables.add(name); |
| 379 | 0 | } |
| 380 | |
} finally { |
| 381 | 0 | if (tableNames != null) { |
| 382 | 0 | tableNames.close(); |
| 383 | |
} |
| 384 | |
} |
| 385 | 0 | log("Found " + tables.size() + " views."); |
| 386 | 0 | return tables; |
| 387 | |
} |
| 388 | |
|
| 389 | |
public boolean isSequence(String sequenceName) { |
| 390 | 0 | return sequenceName.toUpperCase().startsWith("SEQ_") || sequenceName.toUpperCase().startsWith("SEQUENCE_") |
| 391 | |
|| sequenceName.toUpperCase().endsWith("_SEQ") || sequenceName.toUpperCase().endsWith("_SEQUENCE") |
| 392 | |
|| sequenceName.toUpperCase().endsWith("_ID") || sequenceName.toUpperCase().endsWith("_S"); |
| 393 | |
} |
| 394 | |
|
| 395 | |
public List<String> getSequenceNames(DatabaseMetaData dbMeta) throws SQLException { |
| 396 | 0 | log("Getting sequence list..."); |
| 397 | 0 | List<String> tables = new ArrayList<String>(); |
| 398 | 0 | ResultSet tableNames = null; |
| 399 | |
|
| 400 | 0 | String[] types = { "TABLE", "SEQUENCE" }; |
| 401 | |
try { |
| 402 | 0 | tableNames = dbMeta.getTables(null, schema, null, types); |
| 403 | 0 | while (tableNames.next()) { |
| 404 | 0 | String name = tableNames.getString(3); |
| 405 | 0 | if (isSequence(name)) { |
| 406 | 0 | tables.add(name); |
| 407 | |
} |
| 408 | 0 | } |
| 409 | |
} finally { |
| 410 | 0 | if (tableNames != null) { |
| 411 | 0 | tableNames.close(); |
| 412 | |
} |
| 413 | |
} |
| 414 | 0 | log("Found " + tables.size() + " sequences."); |
| 415 | 0 | return tables; |
| 416 | |
} |
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
protected List<Column> getColumns(DatabaseMetaData dbMeta, String tableName) throws SQLException { |
| 442 | 0 | List<Column> columns = new ArrayList<Column>(); |
| 443 | 0 | ResultSet columnSet = null; |
| 444 | |
try { |
| 445 | 0 | columnSet = dbMeta.getColumns(null, schema, tableName, null); |
| 446 | 0 | while (columnSet.next()) { |
| 447 | 0 | String name = columnSet.getString(4); |
| 448 | 0 | Integer sqlType = new Integer(columnSet.getString(5)); |
| 449 | 0 | Integer size = new Integer(columnSet.getInt(7)); |
| 450 | 0 | Integer decimalDigits = new Integer(columnSet.getInt(9)); |
| 451 | 0 | Integer nullType = new Integer(columnSet.getInt(11)); |
| 452 | 0 | String defValue = columnSet.getString(13); |
| 453 | |
|
| 454 | 0 | Column col = new Column(); |
| 455 | 0 | col.setName(name); |
| 456 | 0 | col.setSqlType(sqlType); |
| 457 | 0 | col.setSize(size); |
| 458 | 0 | col.setNullType(nullType); |
| 459 | 0 | col.setDefValue(defValue); |
| 460 | 0 | col.setDecimalDigits(decimalDigits); |
| 461 | 0 | columns.add(col); |
| 462 | 0 | } |
| 463 | |
} finally { |
| 464 | 0 | closeQuietly(columnSet); |
| 465 | 0 | } |
| 466 | 0 | return columns; |
| 467 | |
} |
| 468 | |
|
| 469 | |
protected String getOnDelete(ResultSet foreignKeys) throws SQLException { |
| 470 | 0 | int deleteRule = foreignKeys.getInt(11); |
| 471 | 0 | String onDelete = "none"; |
| 472 | 0 | if (deleteRule == DatabaseMetaData.importedKeyCascade) { |
| 473 | 0 | onDelete = "cascade"; |
| 474 | 0 | } else if (deleteRule == DatabaseMetaData.importedKeyRestrict) { |
| 475 | 0 | onDelete = "restrict"; |
| 476 | 0 | } else if (deleteRule == DatabaseMetaData.importedKeySetNull) { |
| 477 | 0 | onDelete = "setnull"; |
| 478 | |
} |
| 479 | 0 | return onDelete; |
| 480 | |
} |
| 481 | |
|
| 482 | |
protected String getForeignKeyName(ResultSet foreignKeys, String refTableName) throws SQLException { |
| 483 | 0 | String fkName = foreignKeys.getString(12); |
| 484 | |
|
| 485 | 0 | if (fkName == null) { |
| 486 | 0 | fkName = refTableName; |
| 487 | |
} |
| 488 | 0 | return fkName; |
| 489 | |
} |
| 490 | |
|
| 491 | |
protected ForeignKey getNewKualiForeignKey(String refTableName, String onDelete) { |
| 492 | 0 | ForeignKey fk = new ForeignKey(); |
| 493 | 0 | fk.setRefTableName(refTableName); |
| 494 | 0 | fk.setReferences(new ArrayList<Reference>()); |
| 495 | 0 | fk.setOnDelete(onDelete); |
| 496 | 0 | return fk; |
| 497 | |
} |
| 498 | |
|
| 499 | |
protected void addForeignKey(Map<String, ForeignKey> fks, String fkName, String refTableName, String onDelete, |
| 500 | |
ResultSet foreignKeys) throws SQLException { |
| 501 | 0 | ForeignKey fk = (ForeignKey) fks.get(fkName); |
| 502 | 0 | if (fk == null) { |
| 503 | 0 | fk = getNewKualiForeignKey(refTableName, onDelete); |
| 504 | 0 | fks.put(fkName, fk); |
| 505 | |
} |
| 506 | 0 | List<Reference> references = fk.getReferences(); |
| 507 | 0 | Reference reference = new Reference(); |
| 508 | 0 | reference.setLocalColumn(foreignKeys.getString(8)); |
| 509 | 0 | reference.setForeignColumn(foreignKeys.getString(4)); |
| 510 | 0 | references.add(reference); |
| 511 | 0 | } |
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
protected Map<String, ForeignKey> getForeignKeys(DatabaseMetaData dbMeta, String tableName) throws SQLException { |
| 524 | 0 | Map<String, ForeignKey> fks = new HashMap<String, ForeignKey>(); |
| 525 | 0 | ResultSet foreignKeys = null; |
| 526 | |
try { |
| 527 | 0 | foreignKeys = dbMeta.getImportedKeys(null, schema, tableName); |
| 528 | 0 | while (foreignKeys.next()) { |
| 529 | 0 | String refTableName = foreignKeys.getString(3); |
| 530 | 0 | String fkName = getForeignKeyName(foreignKeys, refTableName); |
| 531 | 0 | String onDelete = getOnDelete(foreignKeys); |
| 532 | 0 | addForeignKey(fks, fkName, refTableName, onDelete, foreignKeys); |
| 533 | 0 | } |
| 534 | 0 | } catch (SQLException e) { |
| 535 | |
|
| 536 | |
|
| 537 | 0 | log("Could not read foreign keys for Table " + tableName + " : " + e.getMessage(), Project.MSG_WARN); |
| 538 | |
} finally { |
| 539 | 0 | closeQuietly(foreignKeys); |
| 540 | 0 | } |
| 541 | 0 | return fks; |
| 542 | |
} |
| 543 | |
|
| 544 | |
protected String getPrimaryKeyName(String tableName, DatabaseMetaData dbMeta) throws SQLException { |
| 545 | 0 | ResultSet pkInfo = null; |
| 546 | |
try { |
| 547 | 0 | pkInfo = dbMeta.getPrimaryKeys(null, schema, tableName); |
| 548 | 0 | if (pkInfo.next()) { |
| 549 | 0 | return pkInfo.getString("PK_NAME"); |
| 550 | |
} |
| 551 | 0 | } catch (SQLException e) { |
| 552 | 0 | log("Could not locate primary key info for " + tableName + " : " + e.getMessage(), Project.MSG_WARN); |
| 553 | |
} finally { |
| 554 | 0 | closeQuietly(pkInfo); |
| 555 | 0 | } |
| 556 | 0 | return null; |
| 557 | |
} |
| 558 | |
|
| 559 | |
protected Index getTableIndex(ResultSet indexInfo, String pkName) throws SQLException { |
| 560 | 0 | Index index = new Index(); |
| 561 | 0 | index.setName(indexInfo.getString("INDEX_NAME")); |
| 562 | 0 | index.setUnique(!indexInfo.getBoolean("NON_UNIQUE")); |
| 563 | 0 | return index; |
| 564 | |
} |
| 565 | |
|
| 566 | |
protected void addIndexIfNotPK(Index index, String pkName, List<Index> indexes) { |
| 567 | |
|
| 568 | 0 | if (pkName == null || !pkName.equals(index.getName())) { |
| 569 | 0 | indexes.add(index); |
| 570 | 0 | log("Added " + index.getName() + " to index list", Project.MSG_DEBUG); |
| 571 | |
} else { |
| 572 | 0 | log("Skipping PK: " + index.getName(), Project.MSG_DEBUG); |
| 573 | |
} |
| 574 | 0 | } |
| 575 | |
|
| 576 | |
public List<Index> getIndexes(DatabaseMetaData dbMeta, String tableName) throws SQLException { |
| 577 | 0 | List<Index> indexes = new ArrayList<Index>(); |
| 578 | |
|
| 579 | |
|
| 580 | 0 | String pkName = getPrimaryKeyName(tableName, dbMeta); |
| 581 | |
|
| 582 | 0 | ResultSet indexInfo = null; |
| 583 | |
try { |
| 584 | 0 | indexInfo = dbMeta.getIndexInfo(null, schema, tableName, false, true); |
| 585 | 0 | Index currIndex = null; |
| 586 | 0 | while (indexInfo.next()) { |
| 587 | |
|
| 588 | |
|
| 589 | 0 | String name = indexInfo.getString("INDEX_NAME"); |
| 590 | 0 | if (name == null) { |
| 591 | |
|
| 592 | 0 | continue; |
| 593 | |
} |
| 594 | |
|
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | 0 | if (currIndex == null || !name.equals(currIndex.getName())) { |
| 599 | |
|
| 600 | 0 | currIndex = getTableIndex(indexInfo, pkName); |
| 601 | |
|
| 602 | |
|
| 603 | 0 | addIndexIfNotPK(currIndex, pkName, indexes); |
| 604 | |
} |
| 605 | |
|
| 606 | |
|
| 607 | 0 | currIndex.getColumns().add(indexInfo.getString("COLUMN_NAME")); |
| 608 | 0 | } |
| 609 | 0 | } catch (SQLException e) { |
| 610 | 0 | log("Could not read indexes for Table " + tableName + " : " + e.getMessage(), Project.MSG_WARN); |
| 611 | |
} finally { |
| 612 | 0 | closeQuietly(indexInfo); |
| 613 | 0 | } |
| 614 | 0 | return indexes; |
| 615 | |
} |
| 616 | |
|
| 617 | |
public DocumentImpl getDoc() { |
| 618 | 0 | return doc; |
| 619 | |
} |
| 620 | |
|
| 621 | |
public void setDoc(DocumentImpl doc) { |
| 622 | 0 | this.doc = doc; |
| 623 | 0 | } |
| 624 | |
|
| 625 | |
public Element getDatabaseNode() { |
| 626 | 0 | return databaseNode; |
| 627 | |
} |
| 628 | |
|
| 629 | |
public void setDatabaseNode(Element databaseNode) { |
| 630 | 0 | this.databaseNode = databaseNode; |
| 631 | 0 | } |
| 632 | |
|
| 633 | |
public Map<String, String> getPrimaryKeys() { |
| 634 | 0 | return primaryKeys; |
| 635 | |
} |
| 636 | |
|
| 637 | |
public void setPrimaryKeys(Map<String, String> primaryKeys) { |
| 638 | 0 | this.primaryKeys = primaryKeys; |
| 639 | 0 | } |
| 640 | |
|
| 641 | |
public boolean isProcessTables() { |
| 642 | 0 | return processTables; |
| 643 | |
} |
| 644 | |
|
| 645 | |
public boolean isProcessViews() { |
| 646 | 0 | return processViews; |
| 647 | |
} |
| 648 | |
|
| 649 | |
public boolean isProcessSequences() { |
| 650 | 0 | return processSequences; |
| 651 | |
} |
| 652 | |
|
| 653 | |
public File getSchemaXMLFile() { |
| 654 | 0 | return schemaXMLFile; |
| 655 | |
} |
| 656 | |
|
| 657 | |
public void setSchemaXMLFile(File schemaXMLFile) { |
| 658 | 0 | this.schemaXMLFile = schemaXMLFile; |
| 659 | 0 | } |
| 660 | |
|
| 661 | |
public void setProcessTables(boolean processTables) { |
| 662 | 0 | this.processTables = processTables; |
| 663 | 0 | } |
| 664 | |
|
| 665 | |
public void setProcessViews(boolean processViews) { |
| 666 | 0 | this.processViews = processViews; |
| 667 | 0 | } |
| 668 | |
|
| 669 | |
public void setProcessSequences(boolean processSequences) { |
| 670 | 0 | this.processSequences = processSequences; |
| 671 | 0 | } |
| 672 | |
} |