public boolean loadDbEntities(DataMap map, List tables) throws SQLException {
this.dbEntityList = new ArrayList();
Iterator iter = tables.iterator();
while (iter.hasNext()) {
Table table = (Table) iter.next();
// Check if there already is a DbEntity under such name
// if so, consult the delegate what to do
DbEntity oldEnt = map.getDbEntity(table.getName());
if (oldEnt != null) {
if (delegate == null) {
// no delegate, don't know what to do, cancel import
break;
}
try {
if (delegate.overwriteDbEntity(oldEnt)) {
logObj.debug("Overwrite: " + oldEnt.getName());
map.removeDbEntity(oldEnt.getName(), true);
delegate.dbEntityRemoved(oldEnt);
}
else {
logObj.debug("Keep old: " + oldEnt.getName());
// cay-479 - need to track entities that were not loaded for
// relationships exported to entities that were
skippedEntities.add(oldEnt);
continue;
}
}
catch (CayenneException ex) {
logObj.debug("Load canceled.");
// cancel immediately
return false;
}
}
DbEntity dbEntity = new DbEntity();
dbEntity.setName(table.getName());
dbEntity.setSchema(table.getSchema());
dbEntity.setCatalog(table.getCatalog());
// Create DbAttributes from column information --
ResultSet rs = getMetaData().getColumns(
table.getCatalog(),
table.getSchema(),
table.getName(),
"%");
try {
while (rs.next()) {
// for a reason not quiet apparent to me, Oracle sometimes
// returns duplicate record sets for the same table, messing up table
// names. E.g. for the system table "WK$_ATTR_MAPPING" columns are
// returned twice - as "WK$_ATTR_MAPPING" and "WK$$_ATTR_MAPPING"...
// Go figure
String tableName = rs.getString("TABLE_NAME");
if (!dbEntity.getName().equals(tableName)) {
logObj.info("Incorrectly returned columns for '"
+ tableName
+ ", skipping.");
continue;
}
// gets attribute's (column's) information
String columnName = rs.getString("COLUMN_NAME");
boolean allowNulls = rs.getBoolean("NULLABLE");
int columnType = rs.getInt("DATA_TYPE");
int columnSize = rs.getInt("COLUMN_SIZE");
String typeName = rs.getString("TYPE_NAME");
// ignore precision of non-decimal columns
int decimalDigits = -1;
if (TypesMapping.isDecimal(columnType)) {
decimalDigits = rs.getInt("DECIMAL_DIGITS");
if (rs.wasNull()) {
decimalDigits = -1;
}
}
// create attribute delegating this task to adapter
DbAttribute attr = adapter.buildAttribute(
columnName,
typeName,
columnType,
columnSize,
decimalDigits,
allowNulls);
attr.setEntity(dbEntity);
dbEntity.addAttribute(attr);
}
}
finally {
rs.close();
}
map.addDbEntity(dbEntity);
// notify delegate
if (delegate != null) {
delegate.dbEntityAdded(dbEntity);
}
// delegate might have thrown this entity out... so check if it is still
// around
// before continuing processing
if (map.getDbEntity(table.getName()) == dbEntity) {
this.dbEntityList.add(dbEntity);
}
}
// get primary keys for each table and store it in dbEntity