Java Code Examples for org.apache.ddlutils.model.Table#getIndexCount()

The following examples show how to use org.apache.ddlutils.model.Table#getIndexCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AxionModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void removeSystemIndices(DatabaseMetaDataWrapper metaData, Table table) throws SQLException
{
    // Axion's JDBC driver does not support primary key reading, so we have to filter at this level
    for (int indexIdx = 0; indexIdx < table.getIndexCount();)
    {
        Index index = table.getIndex(indexIdx);

        // also, Axion's internal indices are not unique
        if (index.getName().startsWith("SYS_"))
        {
            table.removeIndex(indexIdx);
        }
        else
        {
            indexIdx++;
        }
    }
}
 
Example 2
Source File: JdbcModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to remove the internal index for the table's primary key.
 * 
 * @param metaData The database meta data
 * @param table    The table
 */
protected void removeInternalPrimaryKeyIndex(DatabaseMetaDataWrapper metaData, Table table) throws SQLException
{
    Column[] pks         = table.getPrimaryKeyColumns();
    List     columnNames = new ArrayList();

    for (int columnIdx = 0; columnIdx < pks.length; columnIdx++)
    {
        columnNames.add(pks[columnIdx].getName());
    }

    for (int indexIdx = 0; indexIdx < table.getIndexCount();)
    {
        Index index = table.getIndex(indexIdx);

        if (index.isUnique() && matches(index, columnNames) && 
            isInternalPrimaryKeyIndex(metaData, table, index))
        {
            table.removeIndex(indexIdx);
        }
        else
        {
            indexIdx++;
        }
    }
}
 
Example 3
Source File: JdbcModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to remove the internal index for the table's primary key.
 * 
 * @param metaData The database meta data
 * @param table    The table
 */
protected void removeInternalPrimaryKeyIndex(DatabaseMetaDataWrapper metaData, Table table) throws SQLException
{
    Column[] pks         = table.getPrimaryKeyColumns();
    List     columnNames = new ArrayList();

    for (int columnIdx = 0; columnIdx < pks.length; columnIdx++)
    {
        columnNames.add(pks[columnIdx].getName());
    }

    for (int indexIdx = 0; indexIdx < table.getIndexCount();)
    {
        Index index = table.getIndex(indexIdx);

        if (index.isUnique() && matches(index, columnNames) && 
            isInternalPrimaryKeyIndex(metaData, table, index))
        {
            table.removeIndex(indexIdx);
        }
        else
        {
            indexIdx++;
        }
    }
}
 
Example 4
Source File: AxionModelReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void removeSystemIndices(DatabaseMetaDataWrapper metaData, Table table) throws SQLException
{
    // Axion's JDBC driver does not support primary key reading, so we have to filter at this level
    for (int indexIdx = 0; indexIdx < table.getIndexCount();)
    {
        Index index = table.getIndex(indexIdx);

        // also, Axion's internal indices are not unique
        if (index.getName().startsWith("SYS_"))
        {
            table.removeIndex(indexIdx);
        }
        else
        {
            indexIdx++;
        }
    }
}
 
Example 5
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the indexes embedded within the create table statement.
 * 
 * @param table The table
 */
protected void writeEmbeddedIndicesStmt(Table table) throws IOException 
{
    if (getPlatformInfo().isIndicesSupported())
    {
        for (int idx = 0; idx < table.getIndexCount(); idx++)
        {
            printStartOfEmbeddedStatement();
            writeEmbeddedIndexCreateStmt(table, table.getIndex(idx));
        }
    }
}
 
Example 6
Source File: JdbcModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to remove the internal index for the given foreign key.
 * 
 * @param metaData The database meta data
 * @param table    The table where the table is defined
 * @param fk       The foreign key
 */
protected void removeInternalForeignKeyIndex(DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk) throws SQLException
{
    List    columnNames  = new ArrayList();
    boolean mustBeUnique = !getPlatformInfo().isSystemForeignKeyIndicesAlwaysNonUnique();

    for (int columnIdx = 0; columnIdx < fk.getReferenceCount(); columnIdx++)
    {
        String name        = fk.getReference(columnIdx).getLocalColumnName();
        Column localColumn = table.findColumn(name,
                                              getPlatform().isDelimitedIdentifierModeOn());

        if (mustBeUnique && !localColumn.isPrimaryKey())
        {
            mustBeUnique = false;
        }
        columnNames.add(name);
    }

    for (int indexIdx = 0; indexIdx < table.getIndexCount();)
    {
        Index index = table.getIndex(indexIdx);

        if ((!mustBeUnique || index.isUnique()) && matches(index, columnNames) && 
            isInternalForeignKeyIndex(metaData, table, fk, index))
        {
            fk.setAutoIndexPresent(true);
            table.removeIndex(indexIdx);
        }
        else
        {
            indexIdx++;
        }
    }
}
 
Example 7
Source File: MSSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
       String tableName = (String)values.get("TABLE_NAME");

       for (int idx = 0; idx < KNOWN_SYSTEM_TABLES.length; idx++)
       {
           if (KNOWN_SYSTEM_TABLES[idx].equals(tableName))
           {
               return null;
           }
       }

       Table table = super.readTable(metaData, values);

       if (table != null)
       {
           // Sql Server does not return the auto-increment status via the database metadata
           determineAutoIncrementFromResultSetMetaData(table, table.getColumns());

           // TODO: Replace this manual filtering using named pks once they are available
           //       This is then probably of interest to every platform
           for (int idx = 0; idx < table.getIndexCount();)
           {
               Index index = table.getIndex(idx);

               if (index.isUnique() && existsPKWithName(metaData, table, index.getName()))
               {
                   table.removeIndex(idx);
               }
               else
               {
                   idx++;
               }
           }
       }
       return table;
}
 
Example 8
Source File: PostgreSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Table table = super.readTable(metaData, values);

    if (table != null)
    {
        // PostgreSQL also returns unique indexes for pk and non-pk auto-increment columns
        // which are of the form "[table]_[column]_key"
        HashMap uniquesByName = new HashMap();

        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
        {
            Index index = table.getIndex(indexIdx);

            if (index.isUnique() && (index.getName() != null))
            {
                uniquesByName.put(index.getName(), index);
            }
        }
        for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
        {
            Column column = table.getColumn(columnIdx);

            if (column.isAutoIncrement())
            {
                String indexName = table.getName() + "_" + column.getName() + "_key";

                if (uniquesByName.containsKey(indexName))
                {
                    table.removeIndex((Index)uniquesByName.get(indexName));
                    uniquesByName.remove(indexName);
                }
            }
        }
    }
    return table;
}
 
Example 9
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the indexes embedded within the create table statement.
 * 
 * @param table The table
 */
protected void writeEmbeddedIndicesStmt(Table table) throws IOException 
{
    if (getPlatformInfo().isIndicesSupported())
    {
        for (int idx = 0; idx < table.getIndexCount(); idx++)
        {
            printStartOfEmbeddedStatement();
            writeEmbeddedIndexCreateStmt(table, table.getIndex(idx));
        }
    }
}
 
Example 10
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the indexes for the given table using external index creation statements.
 * 
 * @param table The table
 */
public void createIndexes(Table table) throws IOException
{
    for (int idx = 0; idx < table.getIndexCount(); idx++)
    {
        Index index = table.getIndex(idx);

        if (!index.isUnique() && !getPlatformInfo().isIndicesSupported())
        {
            throw new ModelException("Platform does not support non-unique indices");
        }
        createIndex(table, index);
    }
}
 
Example 11
Source File: DatabaseIO.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
     * Writes the table object to the given XML writer.
     * 
     * @param table     The table object
     * @param xmlWriter The XML writer
     */
    private void writeTableElement(Table table, PrettyPrintingXmlWriter xmlWriter) throws DdlUtilsXMLException
    {
        xmlWriter.indentIfPrettyPrinting(1);
        writeElementStart(xmlWriter, QNAME_ELEMENT_TABLE);
// GemStone changes BEGIN
        // write fully qualified table name
        final String tableName = table.getQualifiedName();
        writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, tableName);
        /* (original code)
        writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME,        table.getName());
        */
// GemStone changes END
        writeAttribute(xmlWriter, QNAME_ATTRIBUTE_DESCRIPTION, table.getDescription());
        if ((table.getColumnCount() > 0) || (table.getForeignKeyCount() > 0) || (table.getIndexCount() > 0))
        {
            xmlWriter.printlnIfPrettyPrinting();
            for (int idx = 0; idx < table.getColumnCount(); idx++)
            {
                writeColumnElement(table.getColumn(idx), xmlWriter);
            }
            for (int idx = 0; idx < table.getForeignKeyCount(); idx++)
            {
                writeForeignKeyElement(table.getForeignKey(idx), xmlWriter);
            }
            for (int idx = 0; idx < table.getIndexCount(); idx++)
            {
                writeIndexElement(table.getIndex(idx), xmlWriter);
            }
            xmlWriter.indentIfPrettyPrinting(1);
        }
        writeElementEnd(xmlWriter);
    }
 
Example 12
Source File: DatabaseIO.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
     * Writes the table object to the given XML writer.
     * 
     * @param table     The table object
     * @param xmlWriter The XML writer
     */
    private void writeTableElement(Table table, PrettyPrintingXmlWriter xmlWriter) throws DdlUtilsXMLException
    {
        xmlWriter.indentIfPrettyPrinting(1);
        writeElementStart(xmlWriter, QNAME_ELEMENT_TABLE);
// GemStone changes BEGIN
        // write fully qualified table name
        final String tableName = table.getQualifiedName();
        writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, tableName);
        /* (original code)
        writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME,        table.getName());
        */
// GemStone changes END
        writeAttribute(xmlWriter, QNAME_ATTRIBUTE_DESCRIPTION, table.getDescription());
        if ((table.getColumnCount() > 0) || (table.getForeignKeyCount() > 0) || (table.getIndexCount() > 0))
        {
            xmlWriter.printlnIfPrettyPrinting();
            for (int idx = 0; idx < table.getColumnCount(); idx++)
            {
                writeColumnElement(table.getColumn(idx), xmlWriter);
            }
            for (int idx = 0; idx < table.getForeignKeyCount(); idx++)
            {
                writeForeignKeyElement(table.getForeignKey(idx), xmlWriter);
            }
            for (int idx = 0; idx < table.getIndexCount(); idx++)
            {
                writeIndexElement(table.getIndex(idx), xmlWriter);
            }
            xmlWriter.indentIfPrettyPrinting(1);
        }
        writeElementEnd(xmlWriter);
    }
 
Example 13
Source File: ModelComparator.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Searches in the given table for a corresponding index. If the given index
 * has no name, then a index to the same table with the same columns in the
 * same order is searched. If the given index has a name, then the a corresponding
 * index also needs to have the same name, or no name at all, but not a different one. 
 * 
 * @param table The table to search in
 * @param index The original index
 * @return The corresponding index if found
 */
protected Index findCorrespondingIndex(Table table, Index index)
{
    for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
    {
        Index curIndex = table.getIndex(indexIdx);

        if ((_caseSensitive  && index.equals(curIndex)) ||
            (!_caseSensitive && index.equalsIgnoreCase(curIndex)))
        {
            return curIndex;
        }
    }
    return null;
}
 
Example 14
Source File: ModelComparator.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates change objects for indexes that are not present in the given source table but are in the target
 * table, and applies them to the given intermediate model.
 * 
 * @param sourceModel       The source model
 * @param sourceTable       The source table
 * @param intermediateModel The intermediate model to apply the changes to
 * @param intermediateTable The table from the intermediate model corresponding to the source table
 * @param targetModel       The target model
 * @param targetTable       The target table
 * @return The changes
 */
protected List checkForAddedIndexes(Database sourceModel,
                                    Table    sourceTable,
                                    Database intermediateModel,
                                    Table    intermediateTable,
                                    Database targetModel,
                                    Table    targetTable)
{
    List changes = new ArrayList();

    for (int indexIdx = 0; indexIdx < targetTable.getIndexCount(); indexIdx++)
    {
        Index targetIndex       = targetTable.getIndex(indexIdx);
        Index intermediateIndex = findCorrespondingIndex(intermediateTable, targetIndex);
        Index sourceIndex       = findCorrespondingIndex(sourceTable, targetIndex);

        if ((sourceIndex == null) && (intermediateIndex == null))
        {
            if (_log.isInfoEnabled())
            {
                _log.info("Index " + targetIndex.getName() + " needs to be created for table " + intermediateTable.getQualifiedName());
            }

            Index          clonedIndex = _cloneHelper.clone(targetIndex, intermediateTable, _caseSensitive);
            AddIndexChange change      = new AddIndexChange(intermediateTable.getQualifiedName(), clonedIndex);

            changes.add(change);
            change.apply(intermediateModel, _caseSensitive);
        }
    }
    return changes;
}
 
Example 15
Source File: IndexChangeImplBase.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Index findChangedIndex(Database model, boolean caseSensitive)
{
    Table table = findChangedTable(model, caseSensitive);

    if (table != null)
    {
        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
        {
            Index curIndex = table.getIndex(indexIdx);

            if (curIndex.getColumnCount() == _columnNames.size())
            {
                for (int colIdx = 0; colIdx < curIndex.getColumnCount(); colIdx++)
                {
                    String curColName      = curIndex.getColumn(colIdx).getName();
                    String expectedColName = (String)_columnNames.get(colIdx);

                    if ((caseSensitive  && curColName.equals(expectedColName)) ||
                        (!caseSensitive && curColName.equalsIgnoreCase(expectedColName)))
                    {
                        return curIndex;
                    }
                }
            }
        }
    }
    return null;
}
 
Example 16
Source File: JdbcModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to remove the internal index for the given foreign key.
 * 
 * @param metaData The database meta data
 * @param table    The table where the table is defined
 * @param fk       The foreign key
 */
protected void removeInternalForeignKeyIndex(DatabaseMetaDataWrapper metaData, Table table, ForeignKey fk) throws SQLException
{
    List    columnNames  = new ArrayList();
    boolean mustBeUnique = !getPlatformInfo().isSystemForeignKeyIndicesAlwaysNonUnique();

    for (int columnIdx = 0; columnIdx < fk.getReferenceCount(); columnIdx++)
    {
        String name        = fk.getReference(columnIdx).getLocalColumnName();
        Column localColumn = table.findColumn(name,
                                              getPlatform().isDelimitedIdentifierModeOn());

        if (mustBeUnique && !localColumn.isPrimaryKey())
        {
            mustBeUnique = false;
        }
        columnNames.add(name);
    }

    for (int indexIdx = 0; indexIdx < table.getIndexCount();)
    {
        Index index = table.getIndex(indexIdx);

        if ((!mustBeUnique || index.isUnique()) && matches(index, columnNames) && 
            isInternalForeignKeyIndex(metaData, table, fk, index))
        {
            fk.setAutoIndexPresent(true);
            table.removeIndex(indexIdx);
        }
        else
        {
            indexIdx++;
        }
    }
}
 
Example 17
Source File: MSSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
       String tableName = (String)values.get("TABLE_NAME");

       for (int idx = 0; idx < KNOWN_SYSTEM_TABLES.length; idx++)
       {
           if (KNOWN_SYSTEM_TABLES[idx].equals(tableName))
           {
               return null;
           }
       }

       Table table = super.readTable(metaData, values);

       if (table != null)
       {
           // Sql Server does not return the auto-increment status via the database metadata
           determineAutoIncrementFromResultSetMetaData(table, table.getColumns());

           // TODO: Replace this manual filtering using named pks once they are available
           //       This is then probably of interest to every platform
           for (int idx = 0; idx < table.getIndexCount();)
           {
               Index index = table.getIndex(idx);

               if (index.isUnique() && existsPKWithName(metaData, table, index.getName()))
               {
                   table.removeIndex(idx);
               }
               else
               {
                   idx++;
               }
           }
       }
       return table;
}
 
Example 18
Source File: PostgreSqlModelReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
    Table table = super.readTable(metaData, values);

    if (table != null)
    {
        // PostgreSQL also returns unique indexes for pk and non-pk auto-increment columns
        // which are of the form "[table]_[column]_key"
        HashMap uniquesByName = new HashMap();

        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
        {
            Index index = table.getIndex(indexIdx);

            if (index.isUnique() && (index.getName() != null))
            {
                uniquesByName.put(index.getName(), index);
            }
        }
        for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
        {
            Column column = table.getColumn(columnIdx);

            if (column.isAutoIncrement())
            {
                String indexName = table.getName() + "_" + column.getName() + "_key";

                if (uniquesByName.containsKey(indexName))
                {
                    table.removeIndex((Index)uniquesByName.get(indexName));
                    uniquesByName.remove(indexName);
                }
            }
        }
    }
    return table;
}
 
Example 19
Source File: ModelComparator.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Searches in the given table for a corresponding index. If the given index
 * has no name, then a index to the same table with the same columns in the
 * same order is searched. If the given index has a name, then the a corresponding
 * index also needs to have the same name, or no name at all, but not a different one. 
 * 
 * @param table The table to search in
 * @param index The original index
 * @return The corresponding index if found
 */
protected Index findCorrespondingIndex(Table table, Index index)
{
    for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
    {
        Index curIndex = table.getIndex(indexIdx);

        if ((_caseSensitive  && index.equals(curIndex)) ||
            (!_caseSensitive && index.equalsIgnoreCase(curIndex)))
        {
            return curIndex;
        }
    }
    return null;
}
 
Example 20
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that the two given database tables are equal.
 * 
 * @param expected      The expected table
 * @param actual        The actual table
 * @param caseSensitive Whether case matters when comparing
 */
protected void assertEquals(Table expected, Table actual, boolean caseSensitive)
{
    if (caseSensitive)
    {
        assertEquals("Table names do not match.",
                     getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxTableNameLength()),
                     getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxTableNameLength()));
    }
    else
    {
        assertEquals("Table names do not match (ignoring case).",
                     getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength()),
                     getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength()));
    }
    assertEquals("Not the same number of columns in table "+actual.getName()+".",
                 expected.getColumnCount(),
                 actual.getColumnCount());
    for (int columnIdx = 0; columnIdx < actual.getColumnCount(); columnIdx++)
    {
        assertEquals(expected.getColumn(columnIdx),
                     actual.getColumn(columnIdx),
                     caseSensitive);
    }
    assertEquals("Not the same number of foreign keys in table "+actual.getName()+".",
                 expected.getForeignKeyCount(),
                 actual.getForeignKeyCount());
    // order is not assumed with the way foreignkeys are returned.
    for (int expectedFkIdx = 0; expectedFkIdx < expected.getForeignKeyCount(); expectedFkIdx++)
    {
        ForeignKey expectedFk   = expected.getForeignKey(expectedFkIdx);
        String     expectedName = getPlatform().getSqlBuilder().shortenName(expectedFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength());

        for (int actualFkIdx = 0; actualFkIdx < actual.getForeignKeyCount(); actualFkIdx++)
        {
            ForeignKey actualFk   = actual.getForeignKey(actualFkIdx);
            String     actualName = getPlatform().getSqlBuilder().shortenName(actualFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength());

            if (StringUtilsExt.equals(expectedName, actualName, caseSensitive))
            {
                assertEquals(expectedFk,
                             actualFk,
                             caseSensitive);
            }
        }
    }
    assertEquals("Not the same number of indices in table "+actual.getName()+".",
                 expected.getIndexCount(),
                 actual.getIndexCount());
    for (int indexIdx = 0; indexIdx < actual.getIndexCount(); indexIdx++)
    {
        assertEquals(expected.getIndex(indexIdx),
                     actual.getIndex(indexIdx),
                     caseSensitive);
    }
}