use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class DatabaseUtil method createForeignKeyIndices.
public int createForeignKeyIndices(ModelEntity entity, int constraintNameClipLength, List<String> messages) {
if (entity == null) {
String message = "ModelEntity was null and is required to create foreign keys indices for a table";
Debug.logError(message, module);
if (messages != null)
messages.add(message);
return 0;
}
if (entity instanceof ModelViewEntity) {
String message = "Cannot create foreign keys indices for a view entity";
Debug.logWarning(message, module);
if (messages != null)
messages.add(message);
return 0;
}
int fkisCreated = 0;
// go through the relationships to see if any foreign keys need to be added
Iterator<ModelRelation> relationsIter = entity.getRelationsIterator();
while (relationsIter.hasNext()) {
ModelRelation modelRelation = relationsIter.next();
if ("one".equals(modelRelation.getType())) {
String retMsg = createForeignKeyIndex(entity, modelRelation, constraintNameClipLength);
if (UtilValidate.isNotEmpty(retMsg)) {
String message = "Could not create foreign key indices for entity [" + entity.getEntityName() + "]: " + retMsg;
Debug.logError(message, module);
if (messages != null)
messages.add(message);
continue;
}
fkisCreated++;
}
}
if (fkisCreated > 0) {
String message = "Created " + fkisCreated + " foreign key indices for entity [" + entity.getEntityName() + "]";
Debug.logImportant(message, module);
if (messages != null)
messages.add(message);
}
return fkisCreated;
}
use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class DatabaseUtil method renameColumn.
public String renameColumn(ModelEntity entity, ModelField field, String newName) {
if (entity == null || field == null)
return "ModelEntity or ModelField where null, cannot rename column";
if (entity instanceof ModelViewEntity) {
return "ERROR: Cannot rename column for a view entity";
}
ModelFieldType type = modelFieldTypeReader.getModelFieldType(field.getType());
if (type == null) {
return "Field type [" + type + "] not found for field [" + field.getName() + "] of entity [" + entity.getEntityName() + "], not renaming column.";
}
StringBuilder sqlBuf = new StringBuilder("ALTER TABLE ");
sqlBuf.append(entity.getTableName(datasourceInfo));
sqlBuf.append(" RENAME ");
sqlBuf.append(field.getColName());
sqlBuf.append(" TO ");
sqlBuf.append(newName);
String sql = sqlBuf.toString();
if (Debug.infoOn())
Debug.logInfo("[renameColumn] sql=" + sql, module);
try (Connection connection = getConnection();
Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sql);
} catch (SQLException e) {
String errMsg = "SQL Exception while executing the following:\n" + sql + "\nError was: " + e.toString();
Debug.logError(e, errMsg, module);
return errMsg;
} catch (GenericEntityException e) {
String errMsg = "Unable to establish a connection with the database for helperName [" + this.helperInfo.getHelperFullName() + "]... Error was: " + e.toString();
Debug.logError(e, errMsg, module);
return errMsg;
}
return null;
}
use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class DatabaseUtil method createTable.
/* ====================================================================== */
/* ====================================================================== */
public String createTable(ModelEntity entity, Map<String, ModelEntity> modelEntities, boolean addFks) {
if (entity == null) {
return "ModelEntity was null and is required to create a table";
}
if (entity instanceof ModelViewEntity) {
return "ERROR: Cannot create table for a view entity";
}
StringBuilder sqlBuf = new StringBuilder("CREATE TABLE ");
sqlBuf.append(entity.getTableName(this.datasourceInfo));
sqlBuf.append(" (");
Iterator<ModelField> fieldIter = entity.getFieldsIterator();
while (fieldIter.hasNext()) {
ModelField field = fieldIter.next();
ModelFieldType type = modelFieldTypeReader.getModelFieldType(field.getType());
if (type == null) {
return "Field type [" + type + "] not found for field [" + field.getName() + "] of entity [" + entity.getEntityName() + "], not creating table.";
}
sqlBuf.append(field.getColName());
sqlBuf.append(" ");
sqlBuf.append(type.getSqlType());
if ("String".equals(type.getJavaType()) || "java.lang.String".equals(type.getJavaType())) {
// if there is a characterSet, add the CHARACTER SET arg here
if (UtilValidate.isNotEmpty(this.datasourceInfo.getCharacterSet())) {
sqlBuf.append(" CHARACTER SET ");
sqlBuf.append(this.datasourceInfo.getCharacterSet());
}
// if there is a collate, add the COLLATE arg here
if (UtilValidate.isNotEmpty(this.datasourceInfo.getCollate())) {
sqlBuf.append(" COLLATE ");
sqlBuf.append(this.datasourceInfo.getCollate());
}
}
if (field.getIsNotNull() || field.getIsPk()) {
if (this.datasourceInfo.getAlwaysUseConstraintKeyword()) {
sqlBuf.append(" CONSTRAINT NOT NULL, ");
} else {
sqlBuf.append(" NOT NULL, ");
}
} else {
sqlBuf.append(", ");
}
}
String pkName = makePkConstraintName(entity, this.datasourceInfo.getConstraintNameClipLength());
if (this.datasourceInfo.getUsePkConstraintNames()) {
sqlBuf.append("CONSTRAINT ");
sqlBuf.append(pkName);
}
sqlBuf.append(" PRIMARY KEY (");
entity.colNameString(entity.getPkFieldsUnmodifiable(), sqlBuf, "");
sqlBuf.append(")");
if (addFks) {
// NOTE: This is kind of a bad idea anyway since ordering table creations is crazy, if not impossible
// go through the relationships to see if any foreign keys need to be added
Iterator<ModelRelation> relationsIter = entity.getRelationsIterator();
while (relationsIter.hasNext()) {
ModelRelation modelRelation = relationsIter.next();
if ("one".equals(modelRelation.getType())) {
ModelEntity relModelEntity = modelEntities.get(modelRelation.getRelEntityName());
if (relModelEntity == null) {
Debug.logError("Error adding foreign key: ModelEntity was null for related entity name " + modelRelation.getRelEntityName(), module);
continue;
}
if (relModelEntity instanceof ModelViewEntity) {
Debug.logError("Error adding foreign key: related entity is a view entity for related entity name " + modelRelation.getRelEntityName(), module);
continue;
}
String fkConstraintClause = makeFkConstraintClause(entity, modelRelation, relModelEntity, this.datasourceInfo.getConstraintNameClipLength(), this.datasourceInfo.getFkStyle(), this.datasourceInfo.getUseFkInitiallyDeferred());
if (UtilValidate.isNotEmpty(fkConstraintClause)) {
sqlBuf.append(", ");
sqlBuf.append(fkConstraintClause);
} else {
continue;
}
}
}
}
sqlBuf.append(")");
// if there is a tableType, add the TYPE arg here
if (UtilValidate.isNotEmpty(this.datasourceInfo.getTableType())) {
// jaz:20101229 - This appears to be only used by mysql and now mysql has
// deprecated (and in 5.5.x removed) the use of the TYPE keyword. This is
// changed to ENGINE which is supported starting at 4.1
sqlBuf.append(" ENGINE ");
// sqlBuf.append(" TYPE ");
sqlBuf.append(this.datasourceInfo.getTableType());
}
// if there is a characterSet, add the CHARACTER SET arg here
if (UtilValidate.isNotEmpty(this.datasourceInfo.getCharacterSet())) {
sqlBuf.append(" CHARACTER SET ");
sqlBuf.append(this.datasourceInfo.getCharacterSet());
}
// if there is a collate, add the COLLATE arg here
if (UtilValidate.isNotEmpty(this.datasourceInfo.getCollate())) {
sqlBuf.append(" COLLATE ");
sqlBuf.append(this.datasourceInfo.getCollate());
}
if (Debug.verboseOn())
Debug.logVerbose("[createTable] sql=" + sqlBuf.toString(), module);
try (Connection connection = getConnection();
Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sqlBuf.toString());
} catch (SQLException e) {
String errMsg = "SQL Exception while executing the following:\n" + sqlBuf.toString() + "\nError was: " + e.toString();
Debug.logError(e, errMsg, module);
return errMsg;
} catch (GenericEntityException e) {
String errMsg = "Unable to establish a connection with the database for helperName [" + this.helperInfo.getHelperFullName() + "]... Error was: " + e.toString();
Debug.logError(e, errMsg, module);
return errMsg;
}
return null;
}
use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class DatabaseUtil method deleteDeclaredIndices.
public String deleteDeclaredIndices(ModelEntity entity) {
if (entity == null) {
return "ModelEntity was null and is required to delete declared indices for a table";
}
if (entity instanceof ModelViewEntity) {
return "ERROR: Cannot delete declared indices for a view entity";
}
StringBuilder retMsgsBuffer = new StringBuilder();
// go through the relationships to see if any foreign keys need to be added
Iterator<ModelIndex> indexesIter = entity.getIndexesIterator();
while (indexesIter.hasNext()) {
ModelIndex modelIndex = indexesIter.next();
String retMsg = deleteDeclaredIndex(entity, modelIndex);
if (UtilValidate.isNotEmpty(retMsg)) {
if (retMsgsBuffer.length() > 0) {
retMsgsBuffer.append("\n");
}
retMsgsBuffer.append(retMsg);
if (Debug.infoOn())
Debug.logInfo(retMsg, module);
}
}
if (retMsgsBuffer.length() > 0) {
return retMsgsBuffer.toString();
} else {
return null;
}
}
use of org.apache.ofbiz.entity.model.ModelViewEntity in project ofbiz-framework by apache.
the class DatabaseUtil method createForeignKeys.
public int createForeignKeys(ModelEntity entity, Map<String, ModelEntity> modelEntities, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred, List<String> messages) {
if (entity == null) {
String errMsg = "ModelEntity was null and is required to create foreign keys for a table";
Debug.logError(errMsg, module);
if (messages != null)
messages.add(errMsg);
return 0;
}
if (entity instanceof ModelViewEntity) {
// if (messages != null) messages.add(errMsg);
return 0;
}
int fksCreated = 0;
// go through the relationships to see if any foreign keys need to be added
Iterator<ModelRelation> relationsIter = entity.getRelationsIterator();
while (relationsIter.hasNext()) {
ModelRelation modelRelation = relationsIter.next();
if ("one".equals(modelRelation.getType())) {
ModelEntity relModelEntity = modelEntities.get(modelRelation.getRelEntityName());
if (relModelEntity == null) {
String errMsg = "Error adding foreign key: ModelEntity was null for related entity name " + modelRelation.getRelEntityName();
Debug.logError(errMsg, module);
if (messages != null)
messages.add(errMsg);
continue;
}
if (relModelEntity instanceof ModelViewEntity) {
String errMsg = "Error adding foreign key: related entity is a view entity for related entity name " + modelRelation.getRelEntityName();
Debug.logError(errMsg, module);
if (messages != null)
messages.add(errMsg);
continue;
}
String retMsg = createForeignKey(entity, modelRelation, relModelEntity, constraintNameClipLength, fkStyle, useFkInitiallyDeferred);
if (UtilValidate.isNotEmpty(retMsg)) {
Debug.logError(retMsg, module);
if (messages != null)
messages.add(retMsg);
continue;
}
fksCreated++;
}
}
if (fksCreated > 0) {
String message = "Created " + fksCreated + " foreign keys for entity [" + entity.getEntityName() + "]";
Debug.logImportant(message, module);
if (messages != null)
messages.add(message);
}
return fksCreated;
}
Aggregations