use of org.jumpmind.db.model.IndexColumn in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method writeExternalIndexCreateStmt.
/**
* Writes the given index of the table.
*/
protected void writeExternalIndexCreateStmt(Table table, IIndex index, StringBuilder ddl) {
if (databaseInfo.isIndicesSupported()) {
if (index.getName() == null) {
log.warn("Cannot write unnamed index " + index);
} else {
ddl.append("CREATE");
if (index.isUnique()) {
ddl.append(" UNIQUE");
}
ddl.append(" INDEX ");
printIdentifier(getIndexName(index), ddl);
ddl.append(" ON ");
ddl.append(getFullyQualifiedTableNameShorten(table));
ddl.append(" (");
for (int idx = 0; idx < index.getColumnCount(); idx++) {
IndexColumn idxColumn = index.getColumn(idx);
if (idx > 0) {
ddl.append(", ");
}
String name = shortenName(idxColumn.getName(), databaseInfo.getMaxColumnNameLength());
if (name.startsWith("(")) {
// When a column name starts with parenthesis, it's a grouping syntax for an aggregate column, so don't quote it
ddl.append(name);
} else {
printIdentifier(name, ddl);
}
}
ddl.append(")");
printEndOfStatement(ddl);
}
}
}
use of org.jumpmind.db.model.IndexColumn 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.IndexColumn in project symmetric-ds by JumpMind.
the class AbstractJdbcDdlReader method readIndex.
/*
* Reads the next index spec from the result set.
*
* @param metaData The database meta data
*
* @param values The index meta data as defined by {@link
* #getColumnsForIndex()}
*
* @param knownIndices The already read indices for the current table
*/
protected void readIndex(DatabaseMetaDataWrapper metaData, Map<String, Object> values, Map<String, IIndex> knownIndices) throws SQLException {
Short indexType = (Short) values.get("TYPE");
// we're ignoring statistic indices
if ((indexType != null) && (indexType.shortValue() == DatabaseMetaData.tableIndexStatistic)) {
return;
}
String indexName = (String) values.get("INDEX_NAME");
if (indexName != null) {
IIndex index = (IIndex) knownIndices.get(indexName);
if (index == null) {
if (((Boolean) values.get("NON_UNIQUE")).booleanValue()) {
index = new NonUniqueIndex();
} else {
index = new UniqueIndex();
}
index.setName(indexName);
knownIndices.put(indexName, index);
}
IndexColumn indexColumn = new IndexColumn();
String columnName = (String) values.get("COLUMN_NAME");
if (columnName.startsWith("\"") && columnName.endsWith("\"")) {
columnName = columnName.substring(1, columnName.length() - 1);
}
indexColumn.setName(columnName);
if (values.containsKey("ORDINAL_POSITION")) {
indexColumn.setOrdinalPosition(((Short) values.get("ORDINAL_POSITION")).intValue());
}
index.addColumn(indexColumn);
}
}
Aggregations