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;
}
Aggregations