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

The following examples show how to use org.apache.ddlutils.model.Table#getPrimaryKeyColumns() . 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: 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 2
Source File: PrimaryKeyChange.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void apply(Database model, boolean caseSensitive)
{
    Table    table  = findChangedTable(model, caseSensitive);
    Column[] pkCols = table.getPrimaryKeyColumns();

    for (int idx = 0; idx < pkCols.length; idx++)
    {
        pkCols[idx].setPrimaryKey(false);
    }
    for (int idx = 0; idx < _newPrimaryKeyColumns.length; idx++)
    {
        Column column = table.findColumn(_newPrimaryKeyColumns[idx], caseSensitive);

        column.setPrimaryKey(true);
    }
}
 
Example 3
Source File: PrimaryKeyChange.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void apply(Database model, boolean caseSensitive)
{
    Table    table  = findChangedTable(model, caseSensitive);
    Column[] pkCols = table.getPrimaryKeyColumns();

    for (int idx = 0; idx < pkCols.length; idx++)
    {
        pkCols[idx].setPrimaryKey(false);
    }
    for (int idx = 0; idx < _newPrimaryKeyColumns.length; idx++)
    {
        Column column = table.findColumn(_newPrimaryKeyColumns[idx], caseSensitive);

        column.setPrimaryKey(true);
    }
}
 
Example 4
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 5
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the specified row from the table.
 * 
 * @param tableName      The name of the table (case insensitive)
 * @param pkColumnValues The values for the pk columns in order of definition
 */
protected void deleteRow(String tableName, Object[] pkColumnValues)
{
    Table    table     = getModel().findTable(tableName);
    DynaBean bean      = getModel().createDynaBeanFor(table);
    Column[] pkColumns = table.getPrimaryKeyColumns();

    for (int idx = 0; (idx < pkColumns.length) && (idx < pkColumnValues.length); idx++)
    {
        bean.set(pkColumns[idx].getName(), pkColumnValues[idx]);
    }
    getPlatform().delete(getModel(), bean);
}
 
Example 6
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the specified row from the table.
 * 
 * @param tableName      The name of the table (case insensitive)
 * @param pkColumnValues The values for the pk columns in order of definition
 */
protected void deleteRow(String tableName, Object[] pkColumnValues)
{
    Table    table     = getModel().findTable(tableName);
    DynaBean bean      = getModel().createDynaBeanFor(table);
    Column[] pkColumns = table.getPrimaryKeyColumns();

    for (int idx = 0; (idx < pkColumns.length) && (idx < pkColumnValues.length); idx++)
    {
        bean.set(pkColumns[idx].getName(), pkColumnValues[idx]);
    }
    getPlatform().delete(getModel(), bean);
}
 
Example 7
Source File: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Builds an identity object from the primary keys of the specified table using the
 * column values of the supplied bean.
 * 
 * @param table The table
 * @param bean  The bean
 * @return The identity
 */
private Identity buildIdentityFromPKs(Table table, DynaBean bean)
{
    Identity identity  = new Identity(table);
    Column[] pkColumns = table.getPrimaryKeyColumns();

    for (int idx = 0; idx < pkColumns.length; idx++)
    {
        identity.setColumnValue(pkColumns[idx].getName(), bean.get(pkColumns[idx].getName()));
    }
    return identity;
}
 
Example 8
Source File: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new sink instance.
 * 
 * @param platform The database platform
 * @param model    The database model
 */
public DataToDatabaseSink(Platform platform, Database model)
{
    _platform = platform;
    _model    = model;
    for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
    {
        Table      table     = model.getTable(tableIdx);
        ForeignKey selfRefFk = table.getSelfReferencingForeignKey();

        if (selfRefFk != null)
        {
            Column[] pkColumns = table.getPrimaryKeyColumns();

            for (int idx = 0; idx < pkColumns.length; idx++)
            {
                if (pkColumns[idx].isAutoIncrement())
                {
                    _tablesWithSelfIdentityReference.add(table);
                    break;
                }
            }
            for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++)
            {
                if (selfRefFk.getReference(idx).getLocalColumn().isRequired())
                {
                    _tablesWithRequiredSelfReference.add(table);
                    break;
                }
            }
        }
    }
}
 
Example 9
Source File: RemovePrimaryKeyChange.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void apply(Database model, boolean caseSensitive)
{
    Table    table  = findChangedTable(model, caseSensitive);
    Column[] pkCols = table.getPrimaryKeyColumns();

    for (int idx = 0; idx < pkCols.length; idx++)
    {
        pkCols[idx].setPrimaryKey(false);
    }
}
 
Example 10
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the primary key constraints of the table inside its definition.
 * 
 * @param table The table
 */
protected void writeEmbeddedPrimaryKeysStmt(Table table) throws IOException
{
    Column[] primaryKeyColumns = table.getPrimaryKeyColumns();

    if ((primaryKeyColumns.length > 0) && shouldGeneratePrimaryKeys(primaryKeyColumns))
    {
        printStartOfEmbeddedStatement();
        writePrimaryKeyStmt(table, primaryKeyColumns);
    }
}
 
Example 11
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the SQL for deleting an object from the specified table. Depending on
 * the value of <code>genPlaceholders</code>, the generated SQL will contain
 * prepared statement place holders or concrete values. Only those primary key
 * columns wil be used that are present in the given map. If the map is null or
 * completely empty, then the SQL will not have a WHERE clause. The SQL will contain
 * the columns in the order defined in the table.
 * 
 * @param table           The table
 * @param pkValues        The primary key columns to use, and optionally their values
 * @param genPlaceholders Whether to generate value placeholders for a
 *                        prepared statement
 * @return The delete sql
 */
public String getDeleteSql(Table table, Map pkValues, boolean genPlaceholders)
{
    StringBuilder buffer = new StringBuilder("DELETE FROM ");
    boolean      addSep  = false;

    buffer.append(getDelimitedIdentifier(getTableName(table)));
    if ((pkValues != null) && !pkValues.isEmpty())
    {
        buffer.append(" WHERE ");

        Column[] pkCols = table.getPrimaryKeyColumns();

        for (int pkColIdx = 0; pkColIdx < pkCols.length; pkColIdx++)
        {
            Column column = pkCols[pkColIdx];

            if (pkValues.containsKey(column.getName())) {
                if (addSep)
                {
                    buffer.append(" AND ");
                }
                buffer.append(getDelimitedIdentifier(column.getName()));
                buffer.append(" = ");
                if (genPlaceholders)
                {
                    buffer.append("?");
                }
                else
                {
                    buffer.append(getValueAsString(column, pkValues.get(column.getName())));
                }
                addSep = true;
            }
        }
    }
    return buffer.toString();
}
 
Example 12
Source File: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Builds an identity object from the primary keys of the specified table using the
 * column values of the supplied bean.
 * 
 * @param table The table
 * @param bean  The bean
 * @return The identity
 */
private Identity buildIdentityFromPKs(Table table, DynaBean bean)
{
    Identity identity  = new Identity(table);
    Column[] pkColumns = table.getPrimaryKeyColumns();

    for (int idx = 0; idx < pkColumns.length; idx++)
    {
        identity.setColumnValue(pkColumns[idx].getName(), bean.get(pkColumns[idx].getName()));
    }
    return identity;
}
 
Example 13
Source File: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new sink instance.
 * 
 * @param platform The database platform
 * @param model    The database model
 */
public DataToDatabaseSink(Platform platform, Database model)
{
    _platform = platform;
    _model    = model;
    for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
    {
        Table      table     = model.getTable(tableIdx);
        ForeignKey selfRefFk = table.getSelfReferencingForeignKey();

        if (selfRefFk != null)
        {
            Column[] pkColumns = table.getPrimaryKeyColumns();

            for (int idx = 0; idx < pkColumns.length; idx++)
            {
                if (pkColumns[idx].isAutoIncrement())
                {
                    _tablesWithSelfIdentityReference.add(table);
                    break;
                }
            }
            for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++)
            {
                if (selfRefFk.getReference(idx).getLocalColumn().isRequired())
                {
                    _tablesWithRequiredSelfReference.add(table);
                    break;
                }
            }
        }
    }
}
 
Example 14
Source File: RemovePrimaryKeyChange.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void apply(Database model, boolean caseSensitive)
{
    Table    table  = findChangedTable(model, caseSensitive);
    Column[] pkCols = table.getPrimaryKeyColumns();

    for (int idx = 0; idx < pkCols.length; idx++)
    {
        pkCols[idx].setPrimaryKey(false);
    }
}
 
Example 15
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the primary key constraints of the table inside its definition.
 * 
 * @param table The table
 */
protected void writeEmbeddedPrimaryKeysStmt(Table table) throws IOException
{
    Column[] primaryKeyColumns = table.getPrimaryKeyColumns();

    if ((primaryKeyColumns.length > 0) && shouldGeneratePrimaryKeys(primaryKeyColumns))
    {
        printStartOfEmbeddedStatement();
        writePrimaryKeyStmt(table, primaryKeyColumns);
    }
}
 
Example 16
Source File: SqlBuilder.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the SQL for deleting an object from the specified table. Depending on
 * the value of <code>genPlaceholders</code>, the generated SQL will contain
 * prepared statement place holders or concrete values. Only those primary key
 * columns wil be used that are present in the given map. If the map is null or
 * completely empty, then the SQL will not have a WHERE clause. The SQL will contain
 * the columns in the order defined in the table.
 * 
 * @param table           The table
 * @param pkValues        The primary key columns to use, and optionally their values
 * @param genPlaceholders Whether to generate value placeholders for a
 *                        prepared statement
 * @return The delete sql
 */
public String getDeleteSql(Table table, Map pkValues, boolean genPlaceholders)
{
    StringBuilder buffer = new StringBuilder("DELETE FROM ");
    boolean      addSep  = false;

    buffer.append(getDelimitedIdentifier(getTableName(table)));
    if ((pkValues != null) && !pkValues.isEmpty())
    {
        buffer.append(" WHERE ");

        Column[] pkCols = table.getPrimaryKeyColumns();

        for (int pkColIdx = 0; pkColIdx < pkCols.length; pkColIdx++)
        {
            Column column = pkCols[pkColIdx];

            if (pkValues.containsKey(column.getName())) {
                if (addSep)
                {
                    buffer.append(" AND ");
                }
                buffer.append(getDelimitedIdentifier(column.getName()));
                buffer.append(" = ");
                if (genPlaceholders)
                {
                    buffer.append("?");
                }
                else
                {
                    buffer.append(getValueAsString(column, pkValues.get(column.getName())));
                }
                addSep = true;
            }
        }
    }
    return buffer.toString();
}
 
Example 17
Source File: SqlServerTemplate.java    From DataLink with Apache License 2.0 4 votes vote down vote up
private boolean isAutoIncrement(String schemaName, String tableName) {
    Table table = getDbDialect().findTable(schemaName, tableName);
    Column[] pks = table.getPrimaryKeyColumns();
    return pks.length == 1 && pks[0].isAutoIncrement();
}
 
Example 18
Source File: DatabaseTestHelper.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that the data in the tables described by the given model is the same in the
 * database accessed by the second platform as is in the database accessed by the first platform.
 * Note that it is not tested whether the second database has more data.<br/>
 * All differences will be printed via logging in DEBUG level.
 * 
 * @param failureMsg        The failure message to issue if the data is not the same
 * @param model             The database model to check
 * @param origDbPlatform    The first platform
 * @param testedDbPlatform  The second platform
 */
public void assertHasSameData(String failureMsg, Database model, Platform origDbPlatform, Platform testedDbPlatform)
{
    boolean hasError = false;

    for (int idx = 0; idx < model.getTableCount(); idx++)
    {
        Table    table  = model.getTable(idx);
        Column[] pkCols = table.getPrimaryKeyColumns();

        for (Iterator it = origDbPlatform.query(model, buildQueryString(origDbPlatform, table, null, null), new Table[] { table }); it.hasNext();)
        {
            DynaBean   obj    = (DynaBean)it.next();
            Collection result = testedDbPlatform.fetch(model, buildQueryString(origDbPlatform, table, pkCols, obj), new Table[] { table });

            if (result.isEmpty())
            {
                if (_log.isDebugEnabled())
                {
                    hasError = true;
                    _log.debug("Row "+obj.toString()+" is not present in second database");
                }
                else
                {
                    throw new AssertionFailedError(failureMsg);
                }
            }
            else if (result.size() > 1)
            {
                if (_log.isDebugEnabled())
                {
                    hasError = true;

                    StringBuilder debugMsg = new StringBuilder();

                    debugMsg.append("Row ");
                    debugMsg.append(obj.toString());
                    debugMsg.append(" is present more than once in the second database:\n");
                    for (Iterator resultIt = result.iterator(); resultIt.hasNext();)
                    {
                        debugMsg.append("  ");
                        debugMsg.append(resultIt.next().toString());
                    }
                    _log.debug(debugMsg.toString());
                }
                else
                {
                    throw new AssertionFailedError(failureMsg);
                }
            }
            else
            {
                DynaBean otherObj = (DynaBean)result.iterator().next();

                if (!obj.equals(otherObj))
                {
                    if (_log.isDebugEnabled())
                    {
                        hasError = true;

                        _log.debug("Row "+obj.toString()+" is different in the second database: "+otherObj.toString());
                    }
                    else
                    {
                        throw new AssertionFailedError(failureMsg);
                    }
                }
            }
        }
    }
    if (hasError)
    {
        throw new AssertionFailedError(failureMsg);
    }
}
 
Example 19
Source File: DatabaseTestHelper.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that the data in the tables described by the given model is the same in the
 * database accessed by the second platform as is in the database accessed by the first platform.
 * Note that it is not tested whether the second database has more data.<br/>
 * All differences will be printed via logging in DEBUG level.
 * 
 * @param failureMsg        The failure message to issue if the data is not the same
 * @param model             The database model to check
 * @param origDbPlatform    The first platform
 * @param testedDbPlatform  The second platform
 */
public void assertHasSameData(String failureMsg, Database model, Platform origDbPlatform, Platform testedDbPlatform)
{
    boolean hasError = false;

    for (int idx = 0; idx < model.getTableCount(); idx++)
    {
        Table    table  = model.getTable(idx);
        Column[] pkCols = table.getPrimaryKeyColumns();

        for (Iterator it = origDbPlatform.query(model, buildQueryString(origDbPlatform, table, null, null), new Table[] { table }); it.hasNext();)
        {
            DynaBean   obj    = (DynaBean)it.next();
            Collection result = testedDbPlatform.fetch(model, buildQueryString(origDbPlatform, table, pkCols, obj), new Table[] { table });

            if (result.isEmpty())
            {
                if (_log.isDebugEnabled())
                {
                    hasError = true;
                    _log.debug("Row "+obj.toString()+" is not present in second database");
                }
                else
                {
                    throw new AssertionFailedError(failureMsg);
                }
            }
            else if (result.size() > 1)
            {
                if (_log.isDebugEnabled())
                {
                    hasError = true;

                    StringBuilder debugMsg = new StringBuilder();

                    debugMsg.append("Row ");
                    debugMsg.append(obj.toString());
                    debugMsg.append(" is present more than once in the second database:\n");
                    for (Iterator resultIt = result.iterator(); resultIt.hasNext();)
                    {
                        debugMsg.append("  ");
                        debugMsg.append(resultIt.next().toString());
                    }
                    _log.debug(debugMsg.toString());
                }
                else
                {
                    throw new AssertionFailedError(failureMsg);
                }
            }
            else
            {
                DynaBean otherObj = (DynaBean)result.iterator().next();

                if (!obj.equals(otherObj))
                {
                    if (_log.isDebugEnabled())
                    {
                        hasError = true;

                        _log.debug("Row "+obj.toString()+" is different in the second database: "+otherObj.toString());
                    }
                    else
                    {
                        throw new AssertionFailedError(failureMsg);
                    }
                }
            }
        }
    }
    if (hasError)
    {
        throw new AssertionFailedError(failureMsg);
    }
}