ArrayList oldNames = oldColumns.getColumnNames();
ArrayList oldTypes = oldColumns.getTypeNames();
ArrayList oldSizes = oldColumns.getColumnSizes();
SQLUtil.OldIndexes oldIndexes = null;
ArrayList newNames = new ArrayList();
JDBCFieldBridge fields[] = entity.getTableFields();
String tableName = entity.getQualifiedTableName();
for(int i = 0; i < fields.length; i++)
{
JDBCFieldBridge field = fields[i];
JDBCType jdbcType = field.getJDBCType();
String[] columnNames = jdbcType.getColumnNames();
String[] sqlTypes = jdbcType.getSQLTypes();
boolean[] notNull = jdbcType.getNotNull();
for(int j = 0; j < columnNames.length; j++)
{
String name = columnNames[j];
String ucName = name.toUpperCase();
newNames.add( ucName );
int oldIndex = oldNames.indexOf( ucName );
if(oldIndex == -1)
{
// add new column
StringBuffer buf = new StringBuffer( sqlTypes[j] );
if( notNull[j] )
{
buf.append(SQLUtil.NOT).append(SQLUtil.NULL);
}
alterTable(entity.getDataSource(),
entityMetaData.getTypeMapping().getAddColumnTemplate(),
tableName, name, buf.toString());
}
else
{
// alter existing columns
// only CHAR and VARCHAR fields are altered, and only when they are longer then before
String type = (String) oldTypes.get(oldIndex);
if(type.equals("CHAR") || type.equals("VARCHAR"))
{
try
{
// get new length
String l = sqlTypes[j];
l = l.substring(l.indexOf('(') + 1, l.length() - 1);
Integer oldLength = (Integer) oldSizes.get(oldIndex);
if(Integer.parseInt(l) > oldLength.intValue())
{
alterTable(entity.getDataSource(),
entityMetaData.getTypeMapping().getAlterColumnTemplate(),
tableName, name, sqlTypes[j] );
}
}
catch(Exception e)
{
log.warn("EXCEPTION ALTER :" + e.toString());
}
}
}
}
// see if we have to add an index for the field
JDBCCMPFieldMetaData fieldMD = entity.getMetaData().getCMPFieldByName(field.getFieldName());
if(fieldMD != null && fieldMD.isIndexed())
{
if(oldIndexes == null)
{
oldIndexes = SQLUtil.getOldIndexes(entity.getQualifiedTableName(), entity.getDataSource());
idxCount = oldIndexes.getIndexNames().size();
}
if(!hasIndex(oldIndexes, field))
{
createCMPIndex( entity.getDataSource(), field, oldIndexes.getIndexNames() );
}
}
} // for int i;
// delete old columns
Iterator it = oldNames.iterator();
while(it.hasNext())
{
String name = (String) (it.next());
if(!newNames.contains(name))
{
alterTable(entity.getDataSource(),
entityMetaData.getTypeMapping().getDropColumnTemplate(),
tableName, name, "");
}
}
}
}
// Create table if necessary
Set createdTables = getCreatedTables(manager);
if(entityMetaData.getCreateTable() && !createdTables.contains(entity.getEntityName()))
{
DataSource dataSource = entity.getDataSource();
createTable(dataSource, entity.getQualifiedTableName(), getEntityCreateTableSQL(dataSource));
// create indices only if table did not yet exist.
if(!tableExisted)
{
createCMPIndices( dataSource,
SQLUtil.getOldIndexes( entity.getQualifiedTableName(),
entity.getDataSource() ).getIndexNames() );
}
else
{
if(log.isDebugEnabled())
{
log.debug("Indices for table " + entity.getQualifiedTableName() + "not created as table existed");
}
}
// issue extra (user-defined) sql for table
if(!tableExisted)
{
issuePostCreateSQL(dataSource,
entity.getMetaData().getDefaultTablePostCreateCmd(),
entity.getQualifiedTableName());
}
createdTables.add(entity.getEntityName());
}
else
{
log.debug("Table not create as requested: " + entity.getQualifiedTableName());
}
// create relation tables
JDBCAbstractCMRFieldBridge[] cmrFields = entity.getCMRFields();
for(int i = 0; i < cmrFields.length; ++i)
{
JDBCAbstractCMRFieldBridge cmrField = cmrFields[i];
JDBCRelationMetaData relationMetaData = cmrField.getMetaData().getRelationMetaData();
DataSource dataSource = relationMetaData.getDataSource();
// if the table for the related entity has been created
final EntityBridge relatedEntity = cmrField.getRelatedEntity();
if(relationMetaData.isTableMappingStyle() && createdTables.contains(relatedEntity.getEntityName()))
{
boolean relTableExisted = SQLUtil.tableExists(cmrField.getQualifiedTableName(), entity.getDataSource());
if(relTableExisted)
{
if(relationMetaData.getAlterTable())
{
ArrayList oldNames = SQLUtil.getOldColumns(cmrField.getQualifiedTableName(), dataSource).getColumnNames();
ArrayList newNames = new ArrayList();
JDBCFieldBridge[] leftKeys = cmrField.getTableKeyFields();
JDBCFieldBridge[] rightKeys = cmrField.getRelatedCMRField().getTableKeyFields();
JDBCFieldBridge[] fields = new JDBCFieldBridge[leftKeys.length + rightKeys.length];
System.arraycopy(leftKeys, 0, fields, 0, leftKeys.length);
System.arraycopy(rightKeys, 0, fields, leftKeys.length, rightKeys.length);
// have to append field names to leftKeys, rightKeys...
boolean different = false;
for(int j = 0; j < fields.length; j++)
{
JDBCFieldBridge field = fields[j];
String name = field.getJDBCType().getColumnNames()[0].toUpperCase();
newNames.add(name);
if(!oldNames.contains(name))
{
different = true;