Example usage for java.sql DatabaseMetaData importedKeyCascade

List of usage examples for java.sql DatabaseMetaData importedKeyCascade

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData importedKeyCascade.

Prototype

int importedKeyCascade

To view the source code for java.sql DatabaseMetaData importedKeyCascade.

Click Source Link

Document

For the column UPDATE_RULE, indicates that when the primary key is updated, the foreign key (imported key) is changed to agree with it.

Usage

From source file:org.executequery.databaseobjects.impl.TableColumnConstraint.java

private String translateUpdateRule(Short value) {

    String translated = String.valueOf(value);
    if (isForeignKey()) {

        switch (value) {
        case DatabaseMetaData.importedKeyNoAction:
            return translated + " - importedKeyNoAction";

        case DatabaseMetaData.importedKeyCascade:
            return translated + " - importedKeyCascade";

        case DatabaseMetaData.importedKeySetNull:
            return translated + " - importedKeySetNull";

        case DatabaseMetaData.importedKeyRestrict:
            return translated + " - importedKeyRestrict";

        case DatabaseMetaData.importedKeySetDefault:
            return translated + " - importedKeySetDefault";
        }//from w ww. j  av a2  s.co  m

    }
    return translated;
}

From source file:org.executequery.sql.spi.LiquibaseStatementGenerator.java

public String createTableWithConstraints(String databaseName, DatabaseTable table) {

    Database database = databaseFromName(connectionFromObject(table), databaseName);
    CreateTableChange tableChange = createTableChange(table, database);

    List<DatabaseColumn> columns = table.getColumns();
    for (DatabaseColumn column : columns) {

        if (column.hasConstraints()) {

            ColumnConfig columnConfig = columnConfigForColumn(tableChange, column);
            ConstraintsConfig constraintConfig = new ConstraintsConfig();
            for (ColumnConstraint constraint : column.getConstraints()) {

                if (constraint.isPrimaryKey()) {

                    constraintConfig.setPrimaryKey(Boolean.TRUE);
                }//  w  ww. j av a 2 s .  c  o  m

                if (constraint.isForeignKey()) {

                    constraintConfig.setForeignKeyName(constraint.getName());

                    constraintConfig.setReferences(
                            constraint.getReferencedTable() + "(" + constraint.getReferencedColumn() + ")");

                    constraintConfig.setDeleteCascade(
                            constraint.getDeleteRule() == DatabaseMetaData.importedKeyCascade);
                    constraintConfig.setInitiallyDeferred(
                            constraint.getDeferrability() == DatabaseMetaData.importedKeyInitiallyDeferred);
                }

                if (constraint.isUniqueKey()) {

                    constraintConfig.setUnique(Boolean.TRUE);
                    constraintConfig.setUniqueConstraintName(constraint.getName());
                }

            }

            columnConfig.setConstraints(constraintConfig);
        }

    }

    return generateStatements(tableChange, database);
}

From source file:org.executequery.sql.spi.LiquibaseStatementGenerator.java

private ForeignKeyConstraintType getForeignKeyConstraintType(short rule) {

    switch (rule) {

    case DatabaseMetaData.importedKeyCascade:
        return ForeignKeyConstraintType.importedKeyCascade;

    case DatabaseMetaData.importedKeySetNull:
        return ForeignKeyConstraintType.importedKeySetNull;

    case DatabaseMetaData.importedKeyRestrict:
        return ForeignKeyConstraintType.importedKeyRestrict;

    case DatabaseMetaData.importedKeySetDefault:
        return ForeignKeyConstraintType.importedKeySetDefault;

    case DatabaseMetaData.importedKeyNoAction:
    default:/*from  w ww  .  j  a  v a 2s.  c  o m*/
        return ForeignKeyConstraintType.importedKeyNoAction;

    }

}

From source file:org.kuali.core.db.torque.KualiTorqueJDBCTransformTask.java

/**
 * Retrieves a list of foreign key columns for a given table.
 *
 * @param dbMeta JDBC metadata.// w ww . ja  v a 2s.  co m
 * @param tableName Table from which to retrieve FK information.
 * @return A list of foreign keys in <code>tableName</code>.
 * @throws SQLException
 */
public Map<String, Object[]> getForeignKeys(DatabaseMetaData dbMeta, String tableName) throws SQLException {
    TreeMap<String, Object[]> fks = new TreeMap<String, Object[]>();
    ResultSet foreignKeys = null;
    try {
        foreignKeys = dbMeta.getImportedKeys(null, dbSchema, tableName);
        while (foreignKeys.next()) {
            String refTableName = foreignKeys.getString(3);
            String fkName = foreignKeys.getString(12);
            int deleteRule = foreignKeys.getInt(11);
            String onDelete = "none";
            if (deleteRule == DatabaseMetaData.importedKeyCascade) {
                onDelete = "cascade";
            } else if (deleteRule == DatabaseMetaData.importedKeyRestrict) {
                onDelete = "restrict";
            } else if (deleteRule == DatabaseMetaData.importedKeySetNull) {
                onDelete = "setnull";
            }
            // if FK has no name - make it up (use tablename instead)
            if (fkName == null) {
                fkName = refTableName;
            }
            Object[] fk = (Object[]) fks.get(fkName);
            List<String[]> refs;
            if (fk == null) {
                fk = new Object[3];
                fk[0] = refTableName; //referenced table name
                refs = new ArrayList<String[]>();
                fk[1] = refs;
                fks.put(fkName, fk);
                fk[2] = onDelete;
            } else {
                refs = (ArrayList<String[]>) fk[1];
            }
            String[] ref = new String[2];
            ref[0] = foreignKeys.getString(8); //local column
            ref[1] = foreignKeys.getString(4); //foreign column
            refs.add(ref);
        }
    } catch (SQLException e) {
        // this seems to be happening in some db drivers (sybase)
        // when retrieving foreign keys from views.
        log("WARN: Could not read foreign keys for Table " + tableName + " : " + e.getMessage(),
                Project.MSG_WARN);
    } finally {
        if (foreignKeys != null) {
            foreignKeys.close();
        }
    }
    return fks;
}

From source file:org.nuclos.server.dblayer.impl.standard.MetaDataSchemaExtractor.java

protected Collection<DbForeignKeyConstraint> getForeignKeys(String tableName) throws SQLException {
    List<DbForeignKeyConstraint> foreignKeys = new ArrayList<DbForeignKeyConstraint>();
    ResultSet rs = metaData.getImportedKeys(catalog, schema, tableName);
    try {//from   w  w w.  jav  a  2s.c om
        if (rs.next()) {
            String fkName = null, refTable = null, refKey = null;
            List<String> fkColumns = null, refColumns = null;
            boolean onDeleteCascade = false;
            int nextKeySeq = 0;
            do {
                boolean sameSchema = ObjectUtils.equals(rs.getString("PKTABLE_CAT"),
                        rs.getString("FKTABLE_CAT"))
                        && ObjectUtils.equals(rs.getString("PKTABLE_SCHEM"), rs.getString("FKTABLE_SCHEM"));
                if (!sameSchema)
                    continue;
                int keySeq = rs.getInt("KEY_SEQ");
                if (nextKeySeq != keySeq) {
                    if (nextKeySeq > 0)
                        foreignKeys.add(new DbForeignKeyConstraint(tableName, fkName, fkColumns, refTable,
                                refKey, refColumns, onDeleteCascade));

                    fkName = rs.getString("FK_NAME");
                    refTable = rs.getString("PKTABLE_NAME");
                    refKey = rs.getString("PK_NAME");
                    onDeleteCascade = false;
                    int deleteRule = rs.getShort("DELETE_RULE");
                    if (rs.wasNull())
                        deleteRule = -1;
                    if (deleteRule == DatabaseMetaData.importedKeyCascade) {
                        onDeleteCascade = true;
                    }
                    fkColumns = new ArrayList<String>();
                    refColumns = new ArrayList<String>();
                }

                fkColumns.add(rs.getString("FKCOLUMN_NAME"));
                refColumns.add(rs.getString("PKCOLUMN_NAME"));
                nextKeySeq = keySeq + 1;
            } while (rs.next());
            foreignKeys.add(new DbForeignKeyConstraint(tableName, fkName, fkColumns, refTable, refKey,
                    refColumns, onDeleteCascade));
        }
    } finally {
        rs.close();
    }
    return foreignKeys;
}