Example usage for java.sql DatabaseMetaData importedKeySetNull

List of usage examples for java.sql DatabaseMetaData importedKeySetNull

Introduction

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

Prototype

int importedKeySetNull

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

Click Source Link

Document

For the columns UPDATE_RULE and DELETE_RULE, indicates that when the primary key is updated or deleted, the foreign key (imported key) is changed to NULL.

Usage

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 ww  w  . j av  a  2s.  c  om
        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  .j av a2s  . 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;
}