Search in sources :

Example 1 with RowMapper

use of org.jumpmind.db.sql.mapper.RowMapper 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) {
                /* Ignore auto index columns */
                if (!indexColumn.getName().startsWith("sqlite_autoindex_")) {
                    index.addColumn(indexColumn);
                    indexColumn.setColumn(table.getColumnWithName(indexColumn.getName()));
                }
            }
            if (!(index.hasAllPrimaryKeys() && 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)

Aggregations

HashMap (java.util.HashMap)1 Column (org.jumpmind.db.model.Column)1 ForeignKey (org.jumpmind.db.model.ForeignKey)1 IIndex (org.jumpmind.db.model.IIndex)1 IndexColumn (org.jumpmind.db.model.IndexColumn)1 Reference (org.jumpmind.db.model.Reference)1 Table (org.jumpmind.db.model.Table)1 Row (org.jumpmind.db.sql.Row)1 RowMapper (org.jumpmind.db.sql.mapper.RowMapper)1