📄 databaseutil.java
字号:
columnSize = Integer.parseInt(csStr); } catch (NumberFormatException e) { Debug.logError(e, module); } String ddStr = fullTypeStr.substring(comma + 1, closeParen); try { decimalDigits = Integer.parseInt(ddStr); } catch (NumberFormatException e) { Debug.logError(e, module); } } else { String csStr = fullTypeStr.substring(openParen + 1, closeParen); try { columnSize = Integer.parseInt(csStr); } catch (NumberFormatException e) { Debug.logError(e, module); } } } else { typeName = fullTypeStr; } // override the default typeName with the sqlTypeAlias if it is specified if (UtilValidate.isNotEmpty(modelFieldType.getSqlTypeAlias())) { typeName = modelFieldType.getSqlTypeAlias(); } // NOTE: this may need a toUpperCase in some cases, keep an eye on it, okay just compare with ignore case if (!ccInfo.typeName.equalsIgnoreCase(typeName)) { String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] is of type [" + ccInfo.typeName + "] in the database, but is defined as type [" + typeName + "] in the entity definition."; Debug.logError(message, module); if (messages != null) messages.add(message); } if (columnSize != -1 && ccInfo.columnSize != -1 && columnSize != ccInfo.columnSize && (columnSize * 3) != ccInfo.columnSize) { String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] has a column size of [" + ccInfo.columnSize + "] in the database, but is defined to have a column size of [" + columnSize + "] in the entity definition."; Debug.logWarning(message, module); if (messages != null) messages.add(message); if (columnSize > ccInfo.columnSize && colWrongSize != null) { // add item to list of wrong sized columns; only if the entity is larger colWrongSize.add(entity.getEntityName() + "." + field.getName()); } } if (decimalDigits != -1 && decimalDigits != ccInfo.decimalDigits) { String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] has a decimalDigits of [" + ccInfo.decimalDigits + "] in the database, but is defined to have a decimalDigits of [" + decimalDigits + "] in the entity definition."; Debug.logWarning(message, module); if (messages != null) messages.add(message); } // do primary key matching check if (checkPks && ccInfo.isPk && !field.getIsPk()) { String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] IS a primary key in the database, but IS NOT a primary key in the entity definition. The primary key for this table needs to be re-created or modified so that this column is NOT party of the primary key."; Debug.logError(message, module); if (messages != null) messages.add(message); } if (checkPks && !ccInfo.isPk && field.getIsPk()) { String message = "WARNING: Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] IS NOT a primary key in the database, but IS a primary key in the entity definition. The primary key for this table needs to be re-created or modified to add this column to the primary key. Note that data may need to be added first as a primary key column cannot have an null values."; Debug.logError(message, module); if (messages != null) messages.add(message); } } else { String message = "Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] has a field type name of [" + field.getType() + "] which is not found in the field type definitions"; Debug.logError(message, module); if (messages != null) messages.add(message); } } else { String message = "Column [" + ccInfo.columnName + "] of table [" + entity.getTableName(datasourceInfo) + "] of entity [" + entity.getEntityName() + "] exists in the database but has no corresponding field" + ((checkPks && ccInfo.isPk) ? " (and it is a PRIMARY KEY COLUMN)" : ""); Debug.logWarning(message, module); if (messages != null) messages.add(message); } } // -display message if number of table columns does not match number of entity fields if (colMap.size() != entity.getFieldsSize()) { String message = "Entity [" + entity.getEntityName() + "] has " + entity.getFieldsSize() + " fields but table [" + entity.getTableName(datasourceInfo) + "] has " + colMap.size() + " columns."; Debug.logWarning(message, module); 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() + "]" + (field.getIsPk() ? " (and it is a PRIMARY KEY FIELD)" : ""); 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) + "]" + (field.getIsPk() ? " (NOTE: this is a PRIMARY KEY FIELD, but the primary key was not updated automatically (not considered a safe operation), be sure to fill in any needed data and re-create the primary key)" : ""); 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); 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 fk indices if (datasourceInfo.useFkIndices) { int totalFkIndices = 0; Iterator eaIter = entitiesAdded.iterator(); while (eaIter.hasNext()) { ModelEntity curEntity = (ModelEntity) eaIter.next(); if (curEntity.getRelationsOneSize() > 0) { totalFkIndices += this.createForeignKeyIndices(curEntity, datasourceInfo.constraintNameClipLength, messages); } } if (totalFkIndices > 0) Debug.logImportant("==== TOTAL Foreign Key Indices Created: " + totalFkIndices, module); } // for each newly added table, add fks if (datasourceInfo.useFks) { int totalFks = 0; Iterator eaIter = entitiesAdded.iterator(); while (eaIter.hasNext()) { ModelEntity curEntity = (ModelEntity) eaIter.next(); totalFks += this.createForeignKeys(curEntity, modelEntities, datasourceInfo.constraintNameClipLength, datasourceInfo.fkStyle, datasourceInfo.useFkInitiallyDeferred, messages); } if (totalFks > 0) Debug.logImportant("==== TOTAL Foreign Keys Created: " + totalFks, module); } // for each newly added table, add declared indexes if (datasourceInfo.useIndices) { int totalDis = 0; Iterator eaIter = entitiesAdded.iterator(); while (eaIter.hasNext()) { ModelEntity curEntity = (ModelEntity) eaIter.next(); if (curEntity.getIndexesSize() > 0) { totalDis += this.createDeclaredIndices(curEntity, messages); } } if (totalDis > 0) Debug.logImportant("==== TOTAL Declared Indices Created: " + totalDis, module); } // make sure each one-relation has an FK if (checkFks) { //if (!justColumns && 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()); if (relModelEntity == null) { Debug.logError("No such relation: " + entity.getEntityName() + " -> " + modelRelation.getRelEntityName(), module); continue; } String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength); ReferenceCheckInfo rcInfo = null; if (rcInfoMap != null) { rcInfo = (ReferenceCheckInfo) rcInfoMap.get(relConstraintName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -