📄 databaseutil.java
字号:
if (messages != null) messages.add(message);
}
// -list all fields that do not have a corresponding column
Iterator fcnIter = fieldColNames.keySet().iterator();
while (fcnIter.hasNext()) {
String colName = (String) fcnIter.next();
ModelField field = (ModelField) fieldColNames.get(colName);
String message =
"Field \"" + field.getName() + "\" of entity \"" + entity.getEntityName() + "\" is missing its corresponding column \"" + field.getColName() + "\"";
Debug.logWarning(message, module);
if (messages != null) messages.add(message);
if (addMissing) {
// add the column
String errMsg = addColumn(entity, field);
if (errMsg != null && errMsg.length() > 0) {
message = "Could not add column \"" + field.getColName() + "\" to table \"" + entity.getTableName(datasourceInfo) + "\": " + errMsg;
Debug.logError(message, module);
if (messages != null) messages.add(message);
} else {
message = "Added column \"" + field.getColName() + "\" to table \"" + entity.getTableName(datasourceInfo) + "\"";
Debug.logImportant(message, module);
if (messages != null) messages.add(message);
}
}
}
}
} else {
String message = "Entity \"" + entity.getEntityName() + "\" has no table in the database";
Debug.logWarning(message, module);
if (messages != null) messages.add(message);
if (addMissing) {
// create the table
String errMsg = createTable(entity, modelEntities, false, datasourceInfo.usePkConstraintNames, datasourceInfo.constraintNameClipLength, datasourceInfo.fkStyle, datasourceInfo.useFkInitiallyDeferred);
if (errMsg != null && errMsg.length() > 0) {
message = "Could not create table \"" + entity.getTableName(datasourceInfo) + "\": " + errMsg;
Debug.logError(message, module);
if (messages != null) messages.add(message);
} else {
entitiesAdded.add(entity);
message = "Created table \"" + entity.getTableName(datasourceInfo) + "\"";
Debug.logImportant(message, module);
if (messages != null) messages.add(message);
}
}
}
}
timer.timerString("After Individual Table/Column Check");
// -list all tables that do not have a corresponding entity
Iterator tableNamesIter = tableNames.iterator();
while (tableNamesIter != null && tableNamesIter.hasNext()) {
String tableName = (String) tableNamesIter.next();
String message = "Table named \"" + tableName + "\" exists in the database but has no corresponding entity";
Debug.logWarning(message, module);
if (messages != null) messages.add(message);
}
// for each newly added table, add fks
if (datasourceInfo.useFks) {
Iterator eaIter = entitiesAdded.iterator();
while (eaIter.hasNext()) {
ModelEntity curEntity = (ModelEntity) eaIter.next();
this.createForeignKeys(curEntity, modelEntities, datasourceInfo.constraintNameClipLength, datasourceInfo.fkStyle, datasourceInfo.useFkInitiallyDeferred, messages);
}
}
// for each newly added table, add fk indices
if (datasourceInfo.useFkIndices) {
Iterator eaIter = entitiesAdded.iterator();
while (eaIter.hasNext()) {
ModelEntity curEntity = (ModelEntity) eaIter.next();
String indErrMsg = this.createForeignKeyIndices(curEntity, datasourceInfo.constraintNameClipLength);
if (indErrMsg != null && indErrMsg.length() > 0) {
String message = "Could not create foreign key indices for entity \"" + curEntity.getEntityName() + "\": " + indErrMsg;
Debug.logError(message, module);
if (messages != null) messages.add(message);
} else {
String message = "Created foreign key indices for entity \"" + curEntity.getEntityName() + "\"";
Debug.logImportant(message, module);
if (messages != null) messages.add(message);
}
}
}
// for each newly added table, add declared indexes
if (datasourceInfo.useIndices) {
Iterator eaIter = entitiesAdded.iterator();
while (eaIter.hasNext()) {
ModelEntity curEntity = (ModelEntity) eaIter.next();
String indErrMsg = this.createDeclaredIndices(curEntity);
if (indErrMsg != null && indErrMsg.length() > 0) {
String message = "Could not create declared indices for entity \"" + curEntity.getEntityName() + "\": " + indErrMsg;
Debug.logError(message, module);
if (messages != null) messages.add(message);
} else {
String message = "Created declared indices for entity \"" + curEntity.getEntityName() + "\"";
Debug.logImportant(message, module);
if (messages != null) messages.add(message);
}
}
}
// make sure each one-relation has an FK
if (datasourceInfo.useFks && datasourceInfo.checkForeignKeysOnStart) {
// NOTE: This ISN'T working for Postgres or MySQL, who knows about others, may be from JDBC driver bugs...
int numFksCreated = 0;
// TODO: check each key-map to make sure it exists in the FK, if any differences warn and then remove FK and recreate it
// get ALL column info, put into hashmap by table name
Map refTableInfoMap = this.getReferenceInfo(fkTableNames, messages);
// Debug.logVerbose("Ref Info Map: " + refTableInfoMap, module);
if (refTableInfoMap == 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 FK map for this table
Map rcInfoMap = (Map) refTableInfoMap.get(entity.getTableName(datasourceInfo));
// Debug.logVerbose("Got ref info for table " + entity.getTableName(datasourceInfo) + ": " + rcInfoMap, module);
// go through each relation to see if an FK already exists
Iterator relations = entity.getRelationsIterator();
boolean createdConstraints = false;
while (relations.hasNext()) {
ModelRelation modelRelation = (ModelRelation) relations.next();
if (!"one".equals(modelRelation.getType())) {
continue;
}
ModelEntity relModelEntity = (ModelEntity) modelEntities.get(modelRelation.getRelEntityName());
String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength);
ReferenceCheckInfo rcInfo = null;
if (rcInfoMap != null) {
rcInfo = (ReferenceCheckInfo) rcInfoMap.get(relConstraintName);
}
if (rcInfo != null) {
rcInfoMap.remove(relConstraintName);
} else {
// if not, create one
if (Debug.verboseOn()) Debug.logVerbose("No Foreign Key Constraint " + relConstraintName + " found in entity " + entityName, module);
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 (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
String indErrMsg = this.createForeignKeyIndices(entity, datasourceInfo.constraintNameClipLength);
if (indErrMsg != null && indErrMsg.length() > 0) {
String message = "Could not create foreign key indices for entity \"" + entity.getEntityName() + "\": " + indErrMsg;
Debug.logError(message, module);
if (messages != null) messages.add(message);
} else {
String message = "Created foreign key indices for entity \"" + entity.getEntityName() + "\"";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -