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;
}
ModelFieldType type = modelFieldTypeReader.getModelFieldType(field.getType());
if (type == null) {
return "Field type [" + type + "] not found for field [" + field.getName() + "] of entity [" + entity.getEntityName() + "], not adding column.";
}
StringBuilder sqlBuf = new StringBuilder("ALTER TABLE ");
sqlBuf.append(entity.getTableName(datasourceInfo));
sqlBuf.append(" ADD ");
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.characterSet)) {
sqlBuf.append(" CHARACTER SET ");
sqlBuf.append(this.datasourceInfo.characterSet);
}
// if there is a collate, add the COLLATE arg here
if (UtilValidate.isNotEmpty(this.datasourceInfo.collate)) {
sqlBuf.append(" COLLATE ");
sqlBuf.append(this.datasourceInfo.collate);
}
}
String sql = sqlBuf.toString();
if (Debug.infoOn()) Debug.logInfo("[addColumn] sql=" + sql, module);
try {
stmt = connection.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
// if that failed try the alternate syntax real quick
StringBuilder sql2Buf = new StringBuilder("ALTER TABLE ");
sql2Buf.append(entity.getTableName(datasourceInfo));
sql2Buf.append(" ADD COLUMN ");
sql2Buf.append(field.getColName());
sql2Buf.append(" ");
sql2Buf.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.characterSet)) {
sql2Buf.append(" CHARACTER SET ");
sql2Buf.append(this.datasourceInfo.characterSet);
}