Search in sources :

Example 6 with IndexColumn

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

Example 7 with IndexColumn

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;
}
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 8 with IndexColumn

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);
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) NonUniqueIndex(org.jumpmind.db.model.NonUniqueIndex) NonUniqueIndex(org.jumpmind.db.model.NonUniqueIndex) UniqueIndex(org.jumpmind.db.model.UniqueIndex) IndexColumn(org.jumpmind.db.model.IndexColumn)

Aggregations

IndexColumn (org.jumpmind.db.model.IndexColumn)8 Column (org.jumpmind.db.model.Column)5 IIndex (org.jumpmind.db.model.IIndex)5 ForeignKey (org.jumpmind.db.model.ForeignKey)4 Reference (org.jumpmind.db.model.Reference)4 PlatformColumn (org.jumpmind.db.model.PlatformColumn)3 IOException (java.io.IOException)2 NonUniqueIndex (org.jumpmind.db.model.NonUniqueIndex)2 Table (org.jumpmind.db.model.Table)2 UniqueIndex (org.jumpmind.db.model.UniqueIndex)2 IoException (org.jumpmind.exception.IoException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ModelException (org.jumpmind.db.model.ModelException)1 ISqlRowMapper (org.jumpmind.db.sql.ISqlRowMapper)1 Row (org.jumpmind.db.sql.Row)1 RowMapper (org.jumpmind.db.sql.mapper.RowMapper)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1