// b). If autocreate, create the table if necessary
// c). If validate, validate the table
Iterator i = tablesToValidate.iterator();
while (i.hasNext())
{
TableImpl t = (TableImpl) i.next();
boolean columnsValidated = false;
if (checkExistTablesOrViews)
{
if (ddlWriter != null)
{
try
{
if (t instanceof ClassTable)
{
ddlWriter.write("-- Table " + t.toString() +
" for classes " + StringUtils.objectArrayToString(((ClassTable)t).getManagedClasses()) + "\n");
}
else if (t instanceof JoinTable)
{
ddlWriter.write("-- Table " + t.toString() + " for join relationship\n");
}
}
catch (IOException ioe)
{
JPOXLogger.DATASTORE_SCHEMA.error("error writing DDL into file", ioe);
}
}
if (!tablesCreated.contains(t) && t.exists(getCurrentConnection(), autoCreateTables))
{
// Table has been created so add to our list so we dont process it multiple times
// Any subsequent instance of this table in the list will have the columns checked only
tablesCreated.add(t);
columnsValidated = true;
}
else
{
// Table wasn't just created, so do any autocreate of columns necessary
if (t.isInitializedModified() || autoCreateColumns)
{
// Check for existence of the required columns and add where required
t.validateColumns(getCurrentConnection(), false, autoCreateColumns, autoCreateErrors);
columnsValidated = true;
}
}
}
if (validateTables && !columnsValidated) // Table not just created and validation requested
{
// Check down to the column structure where required
t.validate(getCurrentConnection(), validateColumns, false, autoCreateErrors);
}
else if (!columnsValidated)
{
// Validation not requested but allow initialisation of the column information
String initInfo = omfContext.getPersistenceConfiguration().getStringProperty("org.jpox.rdbms.initializeColumnInfo");
if (initInfo.equalsIgnoreCase("PK"))
{
// Initialise the PK columns only
t.initializeColumnInfoForPrimaryKeyColumns(getCurrentConnection());
}
else if (initInfo.equalsIgnoreCase("ALL"))
{
// Initialise all columns
t.initializeColumnInfoFromDatastore(getCurrentConnection());
}
}
// Discard any cached column info used to validate the table
columnInfoByTableName.remove(t.getDatastoreIdentifierFullyQualified());
}
// Table constraint existence and validation
// a). Check for existence of the constraint
// b). If autocreate, create the constraint if necessary
// c). If validate, validate the constraint
// Constraint processing is done as a separate step from table processing
// since the constraints are dependent on tables being available
i = tablesToValidate.iterator();
while (i.hasNext())
{
TableImpl t = (TableImpl) i.next();
if (validateConstraints || autoCreateConstraints)
{
if (ddlWriter != null)
{
try
{
if (t instanceof ClassTable)
{
ddlWriter.write("-- Constraints for table " + t.toString() +
" for class(es) " + StringUtils.objectArrayToString(((ClassTable)t).getManagedClasses()) + "\n");
}
else
{
ddlWriter.write("-- Constraints for table " + t.toString() + "\n");
}
}
catch (IOException ioe)
{
JPOXLogger.DATASTORE_SCHEMA.error("error writing DDL into file", ioe);
}
}
// TODO : split this method into checkExistsConstraints and validateConstraints
// TODO : if duplicated entries on the list, we need to validate before.
if (tablesCreated.contains(t) && !hasDuplicateTablesFromList(tablesToValidate))
{
if (t.createConstraints(getCurrentConnection(), autoCreateErrors, clr))
{
tableConstraintsCreated.add(t);
}
}
else if (t.validateConstraints(getCurrentConnection(), autoCreateConstraints, autoCreateErrors, clr))
{
tableConstraintsCreated.add(t);
}
if (ddlWriter != null)
{