Search in sources :

Example 21 with ForeignKey

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;
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Table(org.jumpmind.db.model.Table) HashMap(java.util.HashMap) Reference(org.jumpmind.db.model.Reference) ForeignKey(org.jumpmind.db.model.ForeignKey) IndexColumn(org.jumpmind.db.model.IndexColumn) IndexColumn(org.jumpmind.db.model.IndexColumn) Column(org.jumpmind.db.model.Column) Row(org.jumpmind.db.sql.Row) RowMapper(org.jumpmind.db.sql.mapper.RowMapper) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper)

Example 22 with ForeignKey

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;
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) ForeignKey(org.jumpmind.db.model.ForeignKey) HashSet(java.util.HashSet)

Example 23 with ForeignKey

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);
}
Also used : Reference(org.jumpmind.db.model.Reference) ForeignKey(org.jumpmind.db.model.ForeignKey)

Example 24 with ForeignKey

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();
}
Also used : ResultSet(java.sql.ResultSet) ForeignKey(org.jumpmind.db.model.ForeignKey) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ForeignKey (org.jumpmind.db.model.ForeignKey)24 Reference (org.jumpmind.db.model.Reference)13 Table (org.jumpmind.db.model.Table)12 ArrayList (java.util.ArrayList)9 Column (org.jumpmind.db.model.Column)8 IIndex (org.jumpmind.db.model.IIndex)6 ResultSet (java.sql.ResultSet)4 HashMap (java.util.HashMap)4 IndexColumn (org.jumpmind.db.model.IndexColumn)4 LinkedHashMap (java.util.LinkedHashMap)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 TableChange (org.jumpmind.db.alter.TableChange)2 Database (org.jumpmind.db.model.Database)2 PlatformColumn (org.jumpmind.db.model.PlatformColumn)2 Row (org.jumpmind.db.sql.Row)2 IoException (org.jumpmind.exception.IoException)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1