if (!bIgnore)
{
try
{
index.addIndexColumn(new IndexColumn(table.getColumn(sColumnName), bAscending));
}
catch (MetadataException e)
{
s_logger.error("Cannot find column \"" + sColumnName +
"\", ignoring index \"" + sIndexName + "\"", e);
bIgnore = true;
}
}
}
if (index != null && !bIgnore)
{
if (addIndex(table, index))
{
bPortable &= isPortable(index);
}
}
rs.close();
rs = null;
// Read the primary key
rs = getPrimaryKeys(names[0], names[1], table.getTableName());
index = null;
bIgnore = false;
try
{
while (rs.next())
{
String sColumnName = toMetadataCase(rs.getString("COLUMN_NAME"));
if (index == null)
{
String sIndexName = rs.getString("PK_NAME");
if (sIndexName == null)
{
sIndexName = table.getName() + ".PK";
index = new Index(sIndexName, Index.BTREE, table);
}
else
{
sIndexName = generateIndexName(table.getTableName(), sIndexName, "PK");
index = table.findIndex(sIndexName);
if (index == null)
{
index = new Index(sIndexName, Index.BTREE, table);
}
else
{
table.setPrimaryKey(index);
bIgnore = true;
break;
}
}
}
if (s_logger.isDebugEnabled())
{
s_logger.debug("Read primary key column \"" + index.getName() + "." + sColumnName + "\"");
}
index.setUnique(true);
index.addIndexColumn(new IndexColumn(table.getColumn(sColumnName), true));
}
}
catch (MetadataException e)
{
s_logger.error("Cannot add primary key to table \"" + table.getName() + "\"", e);
bIgnore = true;
}
if (index != null)
{
if (!bIgnore)
{
if (addIndex(table, index))
{
table.setPrimaryKey(index);
bPortable &= isPortable(index);
}
}
}
else
{
for (int i = 0; i < table.getIndexCount(); ++i)
{
index = table.getIndex(i);
if (index.isUnique())
{
table.setPrimaryKey(index);
break;
}
}
}
if (s_logger.isDebugEnabled())
{
if (table.getPrimaryKey() != null)
{
s_logger.debug("The primary key of table \"" + table.getName() +
"\" is \"" + table.getPrimaryKey().getName() + "\"");
}
else
{
s_logger.debug("Table \"" + table.getName() + "\" has no primary key");
}
}
rs.close();
rs = null;
}
// Read the foreign keys
nCurTable = 0;
for (Iterator itr = schema.getTableIterator(); itr.hasNext();)
{
Table table = (Table)itr.next();
String[] names = (String[])tableMap.get(table);
Index index = null;
boolean bIgnore = false;
if (progress != null)
{
progress.progress("info.sql.schemaManager.readingForeignKeys", new Object[]{table.getName()},
0.70 + 0.30 * (nCurTable++ / nTableCount));
}
rs = dbmeta.getExportedKeys(names[0], names[1], table.getTableName());
while (rs.next())
{
String sColumnName = toMetadataCase(rs.getString("FKCOLUMN_NAME"));
String sSchemaName = rs.getString("FKTABLE_SCHEM");
String sTableName = rs.getString("FKTABLE_NAME");
String sIndexName = rs.getString("FK_NAME");
Table foreignTable = schema.findTable(getFullTableName(sSchemaName, sTableName));
if (foreignTable == null)
{
continue;
}
sIndexName = generateIndexName(foreignTable.getTableName(), sIndexName, "FK" + (foreignTable.getIndexCount() + 1));
if (index != null && !index.getName().equals(sIndexName))
{
if (!bIgnore)
{
if (addIndex(index.getTable(), index))
{
addRelatedKey(table, index);
bPortable &= isPortable(index);
}
}
index = null;
bIgnore = false;
}
if (index == null)
{
if (rs.getString("FK_NAME") == null)
{
index = new Index(sIndexName, Index.BTREE, foreignTable);
}
else
{
index = foreignTable.findIndex(sIndexName);
if (index == null)
{
index = new Index(sIndexName, Index.BTREE, foreignTable);
}
else
{
addRelatedKey(table, index);
bIgnore = true;
}
}
}
if (s_logger.isDebugEnabled())
{
s_logger.debug("Read foreign key column \"" + index.getName() + "." + sColumnName + "\"");
}
if (!bIgnore)
{
index.setType(Index.BTREE);
index.setUnique(false);
try
{
index.addIndexColumn(new IndexColumn(index.getTable().getColumn(sColumnName),
table.getPrimaryKey().getIndexColumn(index.getIndexColumnCount()).isAscending()));
}
catch (MetadataException e)
{
s_logger.error("Cannot add foreign key column to index \"" + index.getName() + "\"", e);