use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class SqliteDdlReader method readTable.
public Table readTable(String catalog, String schema, String tableName) {
Table table = null;
List<Column> columns = platform.getSqlTemplate().query("pragma table_info(" + tableName + ")", COLUMN_MAPPER);
checkForAutoIncrementColumn(columns, tableName);
if (columns != null && columns.size() > 0) {
table = new Table(tableName);
for (Column column : columns) {
table.addColumn(column);
}
List<IIndex> indexes = platform.getSqlTemplate().query("pragma index_list(" + tableName + ")", INDEX_MAPPER);
for (IIndex index : indexes) {
List<IndexColumn> indexColumns = platform.getSqlTemplate().query("pragma index_info(" + index.getName() + ")", INDEX_COLUMN_MAPPER);
for (IndexColumn indexColumn : indexColumns) {
if (!indexColumn.getName().startsWith("sqlite_autoindex_")) {
index.addColumn(indexColumn);
indexColumn.setColumn(table.getColumnWithName(indexColumn.getName()));
}
}
if (index.isUnique() && index.getName().toLowerCase().contains("autoindex") && !index.hasAllPrimaryKeys()) {
for (IndexColumn indexColumn : indexColumns) {
table.getColumnWithName(indexColumn.getName()).setUnique(true);
}
} else if (!((index.hasAllPrimaryKeys() || index.isUnique()) && index.getName().toLowerCase().contains("autoindex"))) {
table.addIndex(index);
}
}
Map<Integer, ForeignKey> keys = new HashMap<Integer, ForeignKey>();
List<Row> rows = platform.getSqlTemplate().query("pragma foreign_key_list(" + tableName + ")", new RowMapper());
for (Row row : rows) {
Integer id = row.getInt("id");
ForeignKey fk = keys.get(id);
if (fk == null) {
fk = new ForeignKey();
fk.setForeignTable(new Table(row.getString("table")));
keys.put(id, fk);
table.addForeignKey(fk);
}
fk.addReference(new Reference(new Column(row.getString("from")), new Column(row.getString("to"))));
}
}
return table;
}
use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class DbFill method getForeignKeyTables.
/**
* Identify the tables not included in the given list that the initial tables have FK relationships to.
*
* @param tables
*
* @return The table array argument and the tables that the initial table array argument depend on.
*/
protected List<Table> getForeignKeyTables(List<Table> tables, Set<Table> visited) {
Set<Table> fkDepSet = new HashSet<Table>(tables);
List<Table> fkDepList = new ArrayList<Table>();
for (Table table : tables) {
if (visited.add(table)) {
for (ForeignKey fk : table.getForeignKeys()) {
Table foreignTable = getDbTable(fk.getForeignTableName());
if (fkDepSet.add(foreignTable)) {
fkDepList.add(foreignTable);
}
}
}
}
if (fkDepList.size() > 0) {
fkDepList.addAll(getForeignKeyTables(fkDepList, visited));
}
Collections.reverse(fkDepList);
return fkDepList;
}
use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class AbstractJdbcDdlReader method readForeignKey.
/*
* Reads the next foreign key spec from the result set.
*
* @param metaData The database meta data
*
* @param values The foreign key meta data as defined by {@link
* #getColumnsForFK()}
*
* @param knownFks The already read foreign keys for the current table
*/
protected void readForeignKey(DatabaseMetaDataWrapper metaData, Map<String, Object> values, Map<String, ForeignKey> knownFks) throws SQLException {
String fkName = (String) values.get("FK_NAME");
ForeignKey fk = (ForeignKey) knownFks.get(fkName);
if (fk == null) {
fk = new ForeignKey(fkName);
fk.setForeignTableName((String) values.get("PKTABLE_NAME"));
knownFks.put(fkName, fk);
}
Reference ref = new Reference();
ref.setForeignColumnName((String) values.get("PKCOLUMN_NAME"));
ref.setLocalColumnName((String) values.get("FKCOLUMN_NAME"));
if (values.containsKey("KEY_SEQ")) {
ref.setSequenceValue(((Short) values.get("KEY_SEQ")).intValue());
}
fk.addReference(ref);
}
use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class AbstractJdbcDdlReader method readForeignKeys.
/*
* Retrieves the foreign keys of the indicated table.
*
* @param metaData The database meta data
*
* @param tableName The name of the table from which to retrieve FK
* information
*
* @return The foreign keys
*/
protected Collection<ForeignKey> readForeignKeys(Connection connection, DatabaseMetaDataWrapper metaData, String tableName) throws SQLException {
Map<String, ForeignKey> fks = new LinkedHashMap<String, ForeignKey>();
if (getPlatformInfo().isForeignKeysSupported()) {
ResultSet fkData = null;
try {
fkData = metaData.getForeignKeys(getTableNamePatternForConstraints(tableName));
while (fkData.next()) {
Map<String, Object> values = readMetaData(fkData, getColumnsForFK());
String fkTableName = (String) values.get("FKTABLE_NAME");
if (isBlank(fkTableName) || fkTableName.equalsIgnoreCase(tableName)) {
readForeignKey(metaData, values, fks);
}
}
} finally {
close(fkData);
}
}
return fks.values();
}
Aggregations