Package org.jboss.dna.common.jdbc.model.api

Examples of org.jboss.dna.common.jdbc.model.api.ForeignKey


    }

    public void testAddForeignKey() {
        String NAME = "My FK";
        // create FK
        ForeignKey fk = factory.createForeignKey();
        // set name
        fk.setName(NAME);
        // add
        bean.addForeignKey(fk);
        // check
        assertFalse("FK set should not be empty", bean.getForeignKeys().isEmpty());
    }
View Full Code Here


    }

    public void testDeleteForeignKey() {
        String NAME = "My FK";
        // create FK
        ForeignKey fk = factory.createForeignKey();
        // set name
        fk.setName(NAME);
        // add
        bean.addForeignKey(fk);
        // check
        assertFalse("FK set should not be empty", bean.getForeignKeys().isEmpty());
View Full Code Here

    }

    public void testFindForeignKeyByName() {
        String NAME = "My FK";
        // create FK
        ForeignKey fk = factory.createForeignKey();
        // set name
        fk.setName(NAME);
        // add
        bean.addForeignKey(fk);
        // check
        assertFalse("FK set should not be empty", bean.getForeignKeys().isEmpty());
        // check
View Full Code Here

        String schemaName = (table.getSchema() == null) ? null : table.getSchema().getName();
        // get table name
        String tableName = table.getName();

        // create table's foreign key
        ForeignKey fk = factory.createForeignKey();

        // process all columns included into foreign key
        while (resultSet.next()) {
            // primary key table catalog being imported
            String pkTableCatalogName = getString(resultSet, "PKTABLE_CAT", false);
            // primary key table schema being imported
            String pkTableSchemaName = getString(resultSet, "PKTABLE_SCHEM", false);
            // primary key table name being imported
            String pkTableName = getString(resultSet, "PKTABLE_NAME", false);
            // primary key column name being imported
            String pkColumnName = getString(resultSet, "PKCOLUMN_NAME", false);

            // FK table name, schema and catalog are already known, so it is useless to fetch

            // foreign key column name
            String fkColumnName = getString(resultSet, "FKCOLUMN_NAME", false);
            // sequence number within foreign key
            Integer ordinalPosition = getInteger(resultSet, "KEY_SEQ", false);
            // update rule - What happens to a foreign key when the primary key is updated
            Integer updateRule = getInteger(resultSet, "UPDATE_RULE", false);
            // delete rule - What happens to the foreign key when primary is deleted
            Integer deleteRule = getInteger(resultSet, "DELETE_RULE", false);
            // foreign key name
            String foreignKeyName = getString(resultSet, "FK_NAME", false);
            // primary key name
            //String primaryKeyName = getString(resultSet, "PK_NAME", false);
            // can the evaluation of foreign key constraints be deferred until commit
            Integer defferability = getInteger(resultSet, "DEFERRABILITY", false);

            //
            // check if FK name has been set earlier and current record shows different FK -
            // so we need to add FK (previous) to the table
            //
            if ((fk.getName() != null) && (!fk.getName().equals(foreignKeyName))) {
                // add previous FK to the table
                table.addForeignKey(fk);

                // log
                if (traceLog.isTraceEnabled()) {
                    traceLog.trace(String.format("[Database %s] The table '%s' (schema %s, catalog %s) foreign key %s has been added.",
                                                 database.getName(),
                                                 tableName,
                                                 schemaName,
                                                 catalogName,
                                                 foreignKeyName));
                }

                // create new FK if a record is not last
                fk = factory.createForeignKey();
            } else {

                // ***************************
                // *** DatabaseNamedObject ***
                // ***************************

                // set FK name
                fk.setName(foreignKeyName);
                // set remarks
                // fk.setRemarks (remarks); // N/A

                // TODO set extra properties
                // fk.addExtraProperty (String key, Object value);

                // ********************
                // *** SchemaObject ***
                // ********************

                // set catalog
                fk.setCatalog(table.getCatalog());
                // set schema
                fk.setSchema(table.getSchema());

                // ***************************
                // *** ForeignKey ***
                // ***************************

                // trying to find table column with specified name
                TableColumn tableColumn = table.findColumnByName(fkColumnName);

                String errMessage = null;
                // warn if null
                if (tableColumn == null) {
                    errMessage = String.format("[Database %s] Unable to find table column '%5$s' for the table %s (schema %s, catalog %s)",
                                               database.getName(),
                                               tableName,
                                               schemaName,
                                               catalogName,
                                               fkColumnName);
                    traceLog.debug(errMessage);
                }
                // if fail is enabled
                if (failOnError && (tableColumn == null)) {
                    throw new DatabaseMetaDataMethodException(errMessage, "populateForeignKeys");
                }

                // create FK column
                ForeignKeyColumn fkColumn = factory.createForeignKeyColumn();

                // check if we found the original table column
                if (tableColumn != null) {
                    // mark original table column as part of FK
                    tableColumn.setForeignKeyColumn(Boolean.TRUE);
                    // clone properties from original table column to the fkcolumn
                    PropertyUtils.copyProperties(fkColumn, tableColumn);
                } else { // recovery if table column is not found but we still want to create fk column
                    // set name at least
                    fkColumn.setName(fkColumnName);
                }
                // modify ordinal position that correspond to the position in FK
                fkColumn.setOrdinalPosition(ordinalPosition);

                // check for PK table and corresponding PK column
                Table pkTable = database.findTableByName(pkTableCatalogName, pkTableSchemaName, pkTableName);
                // sets the scope table of a foreign key.
                fk.setSourceTable(pkTable);
                // find PK
                PrimaryKey pk = (pkTable == null) ? null : pkTable.getPrimaryKey();
                // set
                fk.setSourcePrimaryKey(pk);

                // What happens to a foreign key when the primary key is updated
                fk.setUpdateRule(getKeyModifyRuleType(updateRule));
                // What happens to a foreign key when the primary key is deleted
                fk.setDeleteRule(getKeyModifyRuleType(deleteRule));
                // Can the evaluation of foreign key constraints be deferred until commit
                fk.setDeferrability(getKeyDeferrabilityType(defferability));

                // find PK table column
                TableColumn pkColumn = (pkTable == null) ? null : pkTable.findColumnByName(pkColumnName);
                // Sets mapped source column (in PK/source table) for this foreign key column
                fkColumn.setSourceColumn(pkColumn);

                // add FK column to the foreign key
                fk.addColumn(fkColumn);
            }
        }

        // add FK to the table
        table.addForeignKey(fk);
        // log
        if (traceLog.isDebugEnabled()) {
            traceLog.debug(String.format("[Database %s] The table '%s' (schema %s, catalog %s) foreign key %s has been added.",
                                         database.getName(),
                                         tableName,
                                         schemaName,
                                         catalogName,
                                         fk.getName()));
        }

        // return set of created FK
        return table.getForeignKeys();
    }
View Full Code Here

TOP

Related Classes of org.jboss.dna.common.jdbc.model.api.ForeignKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.