📄 databaseutil.java
字号:
} if (rcInfo != null) { rcInfoMap.remove(relConstraintName); } else { // if not, create one String noFkMessage = "No Foreign Key Constraint [" + relConstraintName + "] found for entity [" + entityName + "]"; if (messages != null) messages.add(noFkMessage); if (Debug.infoOn()) Debug.logInfo(noFkMessage, module); if (addMissing) { String errMsg = createForeignKey(entity, modelRelation, relModelEntity, datasourceInfo.constraintNameClipLength, datasourceInfo.fkStyle, datasourceInfo.useFkInitiallyDeferred); if (errMsg != null && errMsg.length() > 0) { String message = "Could not create foreign key " + relConstraintName + " for entity [" + entity.getEntityName() + "]: " + errMsg; Debug.logError(message, module); if (messages != null) messages.add(message); } else { String message = "Created foreign key " + relConstraintName + " for entity [" + entity.getEntityName() + "]"; Debug.logVerbose(message, module); if (messages != null) messages.add(message); createdConstraints = true; numFksCreated++; } } } } if (createdConstraints) { String message = "Created foreign key(s) for entity [" + entity.getEntityName() + "]"; Debug.logImportant(message, module); if (messages != null) messages.add(message); } // show foreign key references that exist but are unknown if (rcInfoMap != null) { Iterator rcInfoKeysLeft = rcInfoMap.keySet().iterator(); while (rcInfoKeysLeft.hasNext()) { String rcKeyLeft = (String) rcInfoKeysLeft.next(); String message = "Unknown Foreign Key Constraint " + rcKeyLeft + " found in table " + entity.getTableName(datasourceInfo); Debug.logImportant(message, module); if (messages != null) messages.add(message); } } } } if (Debug.infoOn()) Debug.logInfo("Created " + numFksCreated + " fk refs", module); } // make sure each one-relation has an index if (checkFkIdx) { //if (!justColumns && datasourceInfo.useFkIndices && datasourceInfo.checkFkIndicesOnStart) { int numIndicesCreated = 0; // TODO: check each key-map to make sure it exists in the index, if any differences warn and then remove the index and recreate it // TODO: also check the declared indices on start, if the datasourceInfo.checkIndicesOnStart flag is set // get ALL column info, put into hashmap by table name Map tableIndexListMap = this.getIndexInfo(indexTableNames, messages); // Debug.logVerbose("Ref Info Map: " + refTableInfoMap, module); if (tableIndexListMap == null) { // uh oh, something happened while getting info... if (Debug.verboseOn()) Debug.logVerbose("Ref Table Info Map is null", module); } else { Iterator refModelEntityIter = modelEntityList.iterator(); while (refModelEntityIter.hasNext()) { ModelEntity entity = (ModelEntity) refModelEntityIter.next(); String entityName = entity.getEntityName(); // if this is a view entity, do not check it... if (entity instanceof ModelViewEntity) { String entMessage = "NOT Checking View Entity " + entity.getEntityName(); Debug.logVerbose(entMessage, module); if (messages != null) messages.add(entMessage); continue; } // get existing index list for this table TreeSet tableIndexList = (TreeSet) tableIndexListMap.get(entity.getTableName(datasourceInfo)); // Debug.logVerbose("Got ind info for table " + entity.getTableName(datasourceInfo) + ": " + tableIndexList, module); if (tableIndexList == null) { // evidently no indexes in the database for this table, do the create all this.createForeignKeyIndices(entity, datasourceInfo.constraintNameClipLength, messages); } else { // go through each relation to see if an FK already exists boolean createdConstraints = false; Iterator relations = entity.getRelationsIterator(); while (relations.hasNext()) { ModelRelation modelRelation = (ModelRelation) relations.next(); if (!"one".equals(modelRelation.getType())) { continue; } String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength); if (tableIndexList.contains(relConstraintName)) { tableIndexList.remove(relConstraintName); } else { // if not, create one String noIdxMessage = "No Index [" + relConstraintName + "] found for entity [" + entityName + "]"; if (messages != null) messages.add(noIdxMessage); if (Debug.infoOn()) Debug.logInfo(noIdxMessage, module); if (addMissing) { String errMsg = createForeignKeyIndex(entity, modelRelation, datasourceInfo.constraintNameClipLength); if (errMsg != null && errMsg.length() > 0) { String message = "Could not create foreign key index " + relConstraintName + " for entity [" + entity.getEntityName() + "]: " + errMsg; Debug.logError(message, module); if (messages != null) messages.add(message); } else { String message = "Created foreign key index " + relConstraintName + " for entity [" + entity.getEntityName() + "]"; Debug.logVerbose(message, module); if (messages != null) messages.add(message); createdConstraints = true; numIndicesCreated++; } } } } if (createdConstraints) { String message = "Created foreign key index/indices for entity [" + entity.getEntityName() + "]"; Debug.logImportant(message, module); if (messages != null) messages.add(message); } } // show foreign key references that exist but are unknown if (tableIndexList != null) { Iterator tableIndexListIter = tableIndexList.iterator(); while (tableIndexListIter.hasNext()) { String indexLeft = (String) tableIndexListIter.next(); String message = "Unknown Index " + indexLeft + " found in table " + entity.getTableName(datasourceInfo); Debug.logImportant(message, module); if (messages != null) messages.add(message); } } } } if (Debug.infoOn()) Debug.logInfo("Created " + numIndicesCreated + " indices", module); } timer.timerString("Finished Checking Entity Database"); } /** Creates a list of ModelEntity objects based on meta data from the database */ public List induceModelFromDb(Collection messages) { // get ALL tables from this database TreeSet tableNames = this.getTableNames(messages); // get ALL column info, put into hashmap by table name Map colInfo = this.getColumnInfo(tableNames, true, messages); // go through each table and make a ModelEntity object, add to list // for each entity make corresponding ModelField objects // then print out XML for the entities/fields List newEntList = FastList.newInstance(); boolean isCaseSensitive = false; DatabaseMetaData dbData = this.getDatabaseMetaData(null, messages); if (dbData != null) { try { isCaseSensitive = dbData.supportsMixedCaseIdentifiers(); } catch (SQLException e) { Debug.logError(e, "Error getting db meta data about case sensitive", module); } } // iterate over the table names is alphabetical order Iterator tableNamesIter = new TreeSet(colInfo.keySet()).iterator(); while (tableNamesIter.hasNext()) { String tableName = (String) tableNamesIter.next(); Map colMap = (Map) colInfo.get(tableName); ModelEntity newEntity = new ModelEntity(tableName, colMap, modelFieldTypeReader, isCaseSensitive); newEntList.add(newEntity); } return newEntList; } public Document induceModelFromDb(String packageName) { Document document = UtilXml.makeEmptyXmlDocument("entitymodel"); Element root = document.getDocumentElement(); root.appendChild(document.createElement("title")); root.appendChild(document.createElement("description")); root.appendChild(document.createElement("copyright")); root.appendChild(document.createElement("author")); root.appendChild(document.createElement("version")); // messages list List messages = new ArrayList(); // get ALL tables from this database TreeSet tableNames = this.getTableNames(messages); // get ALL column info, put into hashmap by table name Map colInfo = this.getColumnInfo(tableNames, true, messages); boolean isCaseSensitive = false; DatabaseMetaData dbData = this.getDatabaseMetaData(null, messages); if (dbData != null) { try { isCaseSensitive = dbData.supportsMixedCaseIdentifiers(); } catch (SQLException e) { Debug.logError(e, "Error getting db meta data about case sensitive", module); } } if (UtilValidate.isNotEmpty(packageName)) { String catalogName = null; try { catalogName = this.getConnection().getCatalog(); } catch (Exception e) { // ignore } packageName = "org.ofbiz.ext." + (catalogName != null ? catalogName : "unknown"); } // iterate over the table names is alphabetical order Iterator tableNamesIter = new TreeSet(colInfo.keySet()).iterator(); while (tableNamesIter.hasNext()) { String tableName = (String) tableNamesIter.next(); Map colMap = (Map) colInfo.get(tableName); ModelEntity newEntity = new ModelEntity(tableName, colMap, modelFieldTypeReader, isCaseSensitive); root.appendChild(newEntity.toXmlElement(document, "org.ofbiz.ext." + packageName)); } // print the messages to the console for (int i = 0; i < messages.size(); i++) { Debug.logInfo((String) messages.get(i), module); } return document; } public Document induceModelFromDb() { return this.induceModelFromDb(""); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -