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:com.nextep.designer.sqlgen.helpers.CaptureHelper.java

/**
 * Converts a JDBC foreign key action code into a neXtep {@link ForeignKeyAction} enumeration.
 * //w  ww  .  java2s.  co  m
 * @param rule JDBC code of the FK action
 * @return a {@link ForeignKeyAction} corresponding to the specified JDBC action code if it
 *         match one of the {@link DatabaseMetaData} known values,
 *         {@link ForeignKeyAction#NO_ACTION} otherwise
 */
public static ForeignKeyAction getForeignKeyAction(short rule) {
    switch (rule) {
    case DatabaseMetaData.importedKeyCascade:
        return ForeignKeyAction.CASCADE;
    case DatabaseMetaData.importedKeyRestrict:
        // FIXME [BGA] Very ugly fix to handle the jTDS JDBC driver bug that returns
        // importedKeyRestrict instead of importedKeyNoAction.
        DBVendor currVendor = DBGMHelper.getCurrentVendor();
        if (currVendor.equals(DBVendor.MSSQL)) {
            return ForeignKeyAction.NO_ACTION;
        } else {
            return ForeignKeyAction.RESTRICT;
        }
    case DatabaseMetaData.importedKeySetDefault:
        return ForeignKeyAction.SET_DEFAULT;
    case DatabaseMetaData.importedKeySetNull:
        return ForeignKeyAction.SET_NULL;
    case DatabaseMetaData.importedKeyNoAction:
    default:
        return ForeignKeyAction.NO_ACTION;
    }
}

From source file:de.erdesignerng.dialect.JDBCReverseEngineeringStrategy.java

/**
 * Convert a JDBC Cascade Type to the Mogwai CascadeType.
 * <p/>/*from  ww  w . j  a  v a2 s  .c o  m*/
 * Default is CASCADE.
 *
 * @param aValue the JDBC type
 * @return the CascadeType
 */
protected CascadeType getCascadeType(int aValue) {
    switch (aValue) {
    case DatabaseMetaData.importedKeyNoAction:
        return CascadeType.NOTHING;
    case DatabaseMetaData.importedKeySetNull:
        return CascadeType.SETNULL;
    case DatabaseMetaData.importedKeyCascade:
        return CascadeType.CASCADE;
    case DatabaseMetaData.importedKeyRestrict:
        return CascadeType.RESTRICT;
    default:
        return CascadeType.CASCADE;
    }
}

From source file:io.vitess.jdbc.VitessMySQLDatabaseMetadata.java

/**
 * Parses the cascade option string and returns the DBMD constant that
 * represents it (for deletes)//from www  .j  av a  2  s.c o m
 *
 * @param cascadeOptions
 *            the comment from 'SHOW TABLE STATUS'
 * @return the DBMD constant that represents the cascade option
 */
private int getCascadeDeleteOption(String cascadeOptions) {
    int onDeletePos = cascadeOptions.indexOf("ON DELETE");
    if (onDeletePos != -1) {
        String deleteOptions = cascadeOptions.substring(onDeletePos, cascadeOptions.length());
        if (deleteOptions.startsWith("ON DELETE CASCADE")) {
            return java.sql.DatabaseMetaData.importedKeyCascade;
        } else if (deleteOptions.startsWith("ON DELETE SET NULL")) {
            return java.sql.DatabaseMetaData.importedKeySetNull;
        } else if (deleteOptions.startsWith("ON DELETE RESTRICT")) {
            return java.sql.DatabaseMetaData.importedKeyRestrict;
        } else if (deleteOptions.startsWith("ON DELETE NO ACTION")) {
            return java.sql.DatabaseMetaData.importedKeyNoAction;
        }
    }
    return java.sql.DatabaseMetaData.importedKeyNoAction;
}

From source file:io.vitess.jdbc.VitessMySQLDatabaseMetadata.java

/**
 * Parses the cascade option string and returns the DBMD constant that
 * represents it (for Updates)//from   w  ww . ja  va2  s.co  m
 *
 * @param cascadeOptions
 *            the comment from 'SHOW TABLE STATUS'
 * @return the DBMD constant that represents the cascade option
 */
private int getCascadeUpdateOption(String cascadeOptions) {
    int onUpdatePos = cascadeOptions.indexOf("ON UPDATE");
    if (onUpdatePos != -1) {
        String updateOptions = cascadeOptions.substring(onUpdatePos, cascadeOptions.length());
        if (updateOptions.startsWith("ON UPDATE CASCADE")) {
            return java.sql.DatabaseMetaData.importedKeyCascade;
        } else if (updateOptions.startsWith("ON UPDATE SET NULL")) {
            return java.sql.DatabaseMetaData.importedKeySetNull;
        } else if (updateOptions.startsWith("ON UPDATE RESTRICT")) {
            return java.sql.DatabaseMetaData.importedKeyRestrict;
        } else if (updateOptions.startsWith("ON UPDATE NO ACTION")) {
            return java.sql.DatabaseMetaData.importedKeyNoAction;
        }
    }
    return java.sql.DatabaseMetaData.importedKeyNoAction;
}

From source file:net.sourceforge.squirrel_sql.fw.dialects.DialectUtils.java

private static String constructFKContraintActionClause(boolean override, String conditionClause,
        String overrideAction, int rule) {
    // Bug 2531193: Oracle create table script the "ON UPDATE" is wrong
    final StringBuilder tmp = new StringBuilder();
    if (override) {
        if ("NO ACTION".equals(overrideAction)) {
            return "";
        } else {//w  w  w.ja  va  2 s.  com
            tmp.append(conditionClause);
            tmp.append(overrideAction);
            return tmp.toString();
        }
    }

    switch (rule) {
    case DatabaseMetaData.importedKeyCascade:
        tmp.append(conditionClause);
        if (override) {
            tmp.append(overrideAction);
        } else {
            tmp.append("CASCADE");
        }
        break;
    case DatabaseMetaData.importedKeySetNull:
        if (override) {
            tmp.append(overrideAction);
        } else {
            tmp.append("SET NULL");
        }
        break;
    case DatabaseMetaData.importedKeySetDefault:
        if (override) {
            tmp.append(overrideAction);
        } else {
            tmp.append("SET DEFAULT");
        }
        break;
    case DatabaseMetaData.importedKeyRestrict:
    case DatabaseMetaData.importedKeyNoAction:
    default:
        // Append nothing - standard says this is equivalent to NO ACTION
        // and some DBs
        // (e.g. Oracle don't accept ... NO ACTION)
    }
    return tmp.toString();
}

From source file:org.apache.ddlutils.platform.JdbcModelReader.java

/**
 * Converts the JDBC action value (one of the <code>importKey</code> constants in the
 * {@link DatabaseMetaData} class) to a {@link CascadeActionEnum}.
 * //from   www  .jav a2s  .  co m
 * @param jdbcActionValue The jdbc action value
 * @return The enum value
 */
protected CascadeActionEnum convertAction(Short jdbcActionValue) {
    CascadeActionEnum action = null;

    if (jdbcActionValue != null) {
        switch (jdbcActionValue.shortValue()) {
        case DatabaseMetaData.importedKeyCascade:
            action = CascadeActionEnum.CASCADE;
            break;
        case DatabaseMetaData.importedKeySetNull:
            action = CascadeActionEnum.SET_NULL;
            break;
        case DatabaseMetaData.importedKeySetDefault:
            action = CascadeActionEnum.SET_DEFAULT;
            break;
        case DatabaseMetaData.importedKeyRestrict:
            action = CascadeActionEnum.RESTRICT;
            break;
        }
    }
    return action;
}

From source file:org.apache.ddlutils.task.DumpMetadataTask.java

/**
 * Dumps the foreign key columns of the indicated table to other tables.
 * /*from   www.j a v a  2  s .co m*/
 * @param xmlWriter   The xml writer to write to
 * @param metaData    The database metadata
 * @param catalogName The catalog name
 * @param schemaName  The schema name
 * @param tableName   The table name
 */
private void dumpFKs(PrettyPrintingXmlWriter xmlWriter, final DatabaseMetaData metaData,
        final String catalogName, final String schemaName, final String tableName) throws SQLException {
    performResultSetXmlOperation(xmlWriter, null, new ResultSetXmlOperation() {
        public ResultSet getResultSet() throws SQLException {
            return metaData.getImportedKeys(catalogName, schemaName, tableName);
        }

        public void handleRow(PrettyPrintingXmlWriter xmlWriter, ResultSet result) throws SQLException {
            Set columns = getColumnsInResultSet(result);

            xmlWriter.writeElementStart(null, "foreignKey");

            addStringAttribute(xmlWriter, "name", result, columns, "FK_NAME");
            addStringAttribute(xmlWriter, "primaryKeyName", result, columns, "PK_NAME");
            addStringAttribute(xmlWriter, "column", result, columns, "PKCOLUMN_NAME");
            addStringAttribute(xmlWriter, "foreignCatalog", result, columns, "FKTABLE_CAT");
            addStringAttribute(xmlWriter, "foreignSchema", result, columns, "FKTABLE_SCHEM");
            addStringAttribute(xmlWriter, "foreignTable", result, columns, "FKTABLE_NAME");
            addStringAttribute(xmlWriter, "foreignColumn", result, columns, "FKCOLUMN_NAME");
            addShortAttribute(xmlWriter, "sequenceNumberInFK", result, columns, "KEY_SEQ");
            if (columns.contains("UPDATE_RULE")) {
                try {
                    switch (result.getShort("UPDATE_RULE")) {
                    case DatabaseMetaData.importedKeyNoAction:
                        xmlWriter.writeAttribute(null, "updateRule", "no action");
                        break;
                    case DatabaseMetaData.importedKeyCascade:
                        xmlWriter.writeAttribute(null, "updateRule", "cascade PK change");
                        break;
                    case DatabaseMetaData.importedKeySetNull:
                        xmlWriter.writeAttribute(null, "updateRule", "set FK to NULL");
                        break;
                    case DatabaseMetaData.importedKeySetDefault:
                        xmlWriter.writeAttribute(null, "updateRule", "set FK to default");
                        break;
                    default:
                        xmlWriter.writeAttribute(null, "updateRule", "unknown");
                        break;
                    }
                } catch (SQLException ex) {
                    log("Could not read the UPDATE_RULE value for a foreign key of table '" + tableName
                            + "' from the result set: " + ex.getStackTrace(), Project.MSG_ERR);
                }
            }
            if (columns.contains("DELETE_RULE")) {
                try {
                    switch (result.getShort("DELETE_RULE")) {
                    case DatabaseMetaData.importedKeyNoAction:
                    case DatabaseMetaData.importedKeyRestrict:
                        xmlWriter.writeAttribute(null, "deleteRule", "no action");
                        break;
                    case DatabaseMetaData.importedKeyCascade:
                        xmlWriter.writeAttribute(null, "deleteRule", "cascade PK change");
                        break;
                    case DatabaseMetaData.importedKeySetNull:
                        xmlWriter.writeAttribute(null, "deleteRule", "set FK to NULL");
                        break;
                    case DatabaseMetaData.importedKeySetDefault:
                        xmlWriter.writeAttribute(null, "deleteRule", "set FK to default");
                        break;
                    default:
                        xmlWriter.writeAttribute(null, "deleteRule", "unknown");
                        break;
                    }
                } catch (SQLException ex) {
                    log("Could not read the DELETE_RULE value for a foreign key of table '" + tableName
                            + "' from the result set: " + ex.getStackTrace(), Project.MSG_ERR);
                }
            }
            if (columns.contains("DEFERRABILITY")) {
                try {
                    switch (result.getShort("DEFERRABILITY")) {
                    case DatabaseMetaData.importedKeyInitiallyDeferred:
                        xmlWriter.writeAttribute(null, "deferrability", "initially deferred");
                        break;
                    case DatabaseMetaData.importedKeyInitiallyImmediate:
                        xmlWriter.writeAttribute(null, "deferrability", "immediately deferred");
                        break;
                    case DatabaseMetaData.importedKeyNotDeferrable:
                        xmlWriter.writeAttribute(null, "deferrability", "not deferred");
                        break;
                    default:
                        xmlWriter.writeAttribute(null, "deferrability", "unknown");
                        break;
                    }
                } catch (SQLException ex) {
                    log("Could not read the DEFERRABILITY value for a foreign key of table '" + tableName
                            + "' from the result set: " + ex.getStackTrace(), Project.MSG_ERR);
                }
            }
            xmlWriter.writeElementEnd();
        }

        public void handleError(SQLException ex) {
            log("Could not determine the foreign keys for table '" + tableName + "': " + ex.getStackTrace(),
                    Project.MSG_ERR);
        }
    });
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Create a new foreign key from the information in the schema metadata.
 *///from w  w  w .  ja  va 2 s  .  c  o m
protected ForeignKey newForeignKey(ResultSet fkMeta) throws SQLException {
    ForeignKey fk = new ForeignKey();
    fk.setSchemaIdentifier(fromDBName(fkMeta.getString("FKTABLE_SCHEM"), DBIdentifierType.SCHEMA));
    fk.setTableIdentifier(fromDBName(fkMeta.getString("FKTABLE_NAME"), DBIdentifierType.TABLE));
    fk.setColumnIdentifier(fromDBName(fkMeta.getString("FKCOLUMN_NAME"), DBIdentifierType.COLUMN));
    fk.setIdentifier(fromDBName(fkMeta.getString("FK_NAME"), DBIdentifierType.FOREIGN_KEY));
    fk.setPrimaryKeySchemaIdentifier(fromDBName(fkMeta.getString("PKTABLE_SCHEM"), DBIdentifierType.SCHEMA));
    fk.setPrimaryKeyTableIdentifier(fromDBName(fkMeta.getString("PKTABLE_NAME"), DBIdentifierType.TABLE));
    fk.setPrimaryKeyColumnIdentifier(fromDBName(fkMeta.getString("PKCOLUMN_NAME"), DBIdentifierType.COLUMN));
    fk.setKeySequence(fkMeta.getShort("KEY_SEQ"));
    fk.setDeferred(fkMeta.getShort("DEFERRABILITY") == DatabaseMetaData.importedKeyInitiallyDeferred);

    int del = fkMeta.getShort("DELETE_RULE");
    switch (del) {
    case DatabaseMetaData.importedKeySetNull:
        fk.setDeleteAction(ForeignKey.ACTION_NULL);
        break;
    case DatabaseMetaData.importedKeySetDefault:
        fk.setDeleteAction(ForeignKey.ACTION_DEFAULT);
        break;
    case DatabaseMetaData.importedKeyCascade:
        fk.setDeleteAction(ForeignKey.ACTION_CASCADE);
        break;
    default:
        fk.setDeleteAction(ForeignKey.ACTION_RESTRICT);
        break;
    }
    return fk;
}

From source file:org.efaps.db.databases.AbstractDatabase.java

/**
 * <p>/*from w w  w  .  j  ava  2 s.c  o m*/
 * Fetches all foreign keys for all tables. If a SQL statement is given,
 * this SQL statement is used instead of using the JDBC meta data methods.
 * The SQL select statement must define this six columns
 * <ul>
 * <li><b><code>TABLE_NAME</code></b> for the real name of the table,</li>
 * <li><b><code>FK_NAME</code></b> for the real name of the foreign key
 * name,</li>
 * <li><b><code>FKCOLUMN_NAME</code></b> for the name of the column for
 * which the foreign key is defined,</li>
 * <li><b><code>PKTABLE_NAME</code></b> for the name of the referenced
 * table,</li>
 * <li><b><code>PKCOLUMN_NAME</code></b> for the name of column within the
 * referenced table and</li>
 * <li><b><code>DELETE_RULE</code></b> defining the rule what happens in the
 * case a row of the table is deleted (with value
 * {@link DatabaseMetaData#importedKeyCascade} in the case the delete is
 * cascaded).</li>
 * </ul>
 * </p>
 *
 * @param _con SQL connection
 * @param _sql SQL statement which must be executed if the JDBC
 *            functionality does not work (or null if JDBC meta data is used
 *            to fetch the foreign keys)
 * @param _cache4Name map used to fetch depending on the table name the
 *            related table information
 * @throws SQLException if foreign keys could not be fetched
 */
protected void initTableInfoForeignKeys(final Connection _con, final String _sql,
        final Map<String, TableInformation> _cache4Name) throws SQLException {
    Statement stmt = null;
    final ResultSet rs;
    if (_sql == null) {
        rs = _con.getMetaData().getImportedKeys(null, null, "%");
    } else {
        stmt = _con.createStatement();
        rs = stmt.executeQuery(_sql);
    }
    try {
        while (rs.next()) {
            final String tableName = rs.getString("TABLE_NAME").toUpperCase();
            if (_cache4Name.containsKey(tableName)) {
                final String fkName = rs.getString("FK_NAME").toUpperCase();
                final String colName = rs.getString("FKCOLUMN_NAME").toUpperCase();
                final String refTableName = rs.getString("PKTABLE_NAME").toUpperCase();
                final String refColName = rs.getString("PKCOLUMN_NAME").toUpperCase();
                final boolean cascade = rs.getInt("DELETE_RULE") == DatabaseMetaData.importedKeyCascade;
                _cache4Name.get(tableName).addForeignKey(fkName, colName, refTableName, refColName, cascade);
            }
        }
    } finally {
        rs.close();
        if (stmt != null) {
            stmt.close();
        }
    }
}

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

private String translateDeletedRule(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  w w . ja  v  a  2s. c  om*/

    }
    return translated;
}