Example usage for java.sql DatabaseMetaData importedKeyNotDeferrable

List of usage examples for java.sql DatabaseMetaData importedKeyNotDeferrable

Introduction

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

Prototype

int importedKeyNotDeferrable

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

Click Source Link

Document

Indicates deferrability.

Usage

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

@VisibleForTesting
void extractForeignKeyForTable(List<ArrayList<String>> rows, String createTableString, String catalog,
        String table) throws SQLException {
    StringTokenizer lineTokenizer = new StringTokenizer(createTableString, "\n");

    while (lineTokenizer.hasMoreTokens()) {
        String line = lineTokenizer.nextToken().trim();
        String constraintName = null;
        if (StringUtils.startsWithIgnoreCase(line, "CONSTRAINT")) {
            boolean usingBackTicks = true;
            int beginPos = io.vitess.util.StringUtils.indexOfQuoteDoubleAware(line, this.quotedId, 0);
            if (beginPos == -1) {
                beginPos = line.indexOf("\"");
                usingBackTicks = false;//from  ww  w. j  a  va 2s .  c  om
            }
            if (beginPos != -1) {
                int endPos;
                if (usingBackTicks) {
                    endPos = io.vitess.util.StringUtils.indexOfQuoteDoubleAware(line, this.quotedId,
                            beginPos + 1);
                } else {
                    endPos = io.vitess.util.StringUtils.indexOfQuoteDoubleAware(line, "\"", beginPos + 1);
                }
                if (endPos != -1) {
                    constraintName = line.substring(beginPos + 1, endPos);
                    line = line.substring(endPos + 1, line.length()).trim();
                }
            }
        }

        if (line.startsWith("FOREIGN KEY")) {
            if (line.endsWith(",")) {
                line = line.substring(0, line.length() - 1);
            }
            int indexOfFK = line.indexOf("FOREIGN KEY");
            String localColumnName = null;
            String referencedCatalogName = io.vitess.util.StringUtils.quoteIdentifier(catalog, this.quotedId);
            String referencedTableName = null;
            String referencedColumnName = null;
            if (indexOfFK != -1) {
                int afterFk = indexOfFK + "FOREIGN KEY".length();

                int indexOfRef = io.vitess.util.StringUtils.indexOfIgnoreCase(afterFk, line, "REFERENCES",
                        this.quotedId, this.quotedId);
                if (indexOfRef != -1) {
                    int indexOfParenOpen = line.indexOf('(', afterFk);
                    int indexOfParenClose = io.vitess.util.StringUtils.indexOfIgnoreCase(indexOfParenOpen, line,
                            ")", this.quotedId, this.quotedId);
                    localColumnName = line.substring(indexOfParenOpen + 1, indexOfParenClose);

                    int afterRef = indexOfRef + "REFERENCES".length();
                    int referencedColumnBegin = io.vitess.util.StringUtils.indexOfIgnoreCase(afterRef, line,
                            "(", this.quotedId, this.quotedId);

                    if (referencedColumnBegin != -1) {
                        referencedTableName = line.substring(afterRef, referencedColumnBegin);
                        int referencedColumnEnd = io.vitess.util.StringUtils.indexOfIgnoreCase(
                                referencedColumnBegin + 1, line, ")", this.quotedId, this.quotedId);
                        if (referencedColumnEnd != -1) {
                            referencedColumnName = line.substring(referencedColumnBegin + 1,
                                    referencedColumnEnd);
                        }
                        int indexOfCatalogSep = io.vitess.util.StringUtils.indexOfIgnoreCase(0,
                                referencedTableName, ".", this.quotedId, this.quotedId);
                        if (indexOfCatalogSep != -1) {
                            referencedCatalogName = referencedTableName.substring(0, indexOfCatalogSep);
                            referencedTableName = referencedTableName.substring(indexOfCatalogSep + 1);
                        }
                    }
                }
            }
            if (constraintName == null) {
                constraintName = "not_available";
            }
            List<String> localColumnsList = io.vitess.util.StringUtils.split(localColumnName, ",",
                    this.quotedId, this.quotedId);
            List<String> referColumnsList = io.vitess.util.StringUtils.split(referencedColumnName, ",",
                    this.quotedId, this.quotedId);
            if (localColumnsList.size() != referColumnsList.size()) {
                throw new SQLException("Mismatch columns list for foreign key local and reference columns");
            }
            // Report a separate row for each column in the foreign key. All values the same except the column name.
            for (int i = 0; i < localColumnsList.size(); i++) {
                String localColumn = localColumnsList.get(i);
                String referColumn = referColumnsList.get(i);
                ArrayList<String> row = new ArrayList<>(14);
                row.add(io.vitess.util.StringUtils.unQuoteIdentifier(referencedCatalogName, this.quotedId)); // PKTABLE_CAT
                row.add(null); // PKTABLE_SCHEM
                row.add(io.vitess.util.StringUtils.unQuoteIdentifier(referencedTableName, this.quotedId)); // PKTABLE_NAME
                row.add(io.vitess.util.StringUtils.unQuoteIdentifier(referColumn, this.quotedId)); // PKCOLUMN_NAME
                row.add(catalog); // FKTABLE_CAT
                row.add(null); // FKTABLE_SCHEM
                row.add(table); // FKTABLE_NAME
                row.add(io.vitess.util.StringUtils.unQuoteIdentifier(localColumn, this.quotedId)); // FKCOLUMN_NAME
                row.add(Integer.toString(i + 1)); // KEY_SEQ
                int[] actions = getForeignKeyActions(line);
                row.add(Integer.toString(actions[1])); // UPDATE_RULE
                row.add(Integer.toString(actions[0])); // DELETE_RULE
                row.add(constraintName); // FK_NAME
                row.add(null); // PK_NAME
                row.add(Integer.toString(java.sql.DatabaseMetaData.importedKeyNotDeferrable)); // DEFERRABILITY
                rows.add(row);
            }
        }
    }
}

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

/**
 * Dumps the foreign key columns of the indicated table to other tables.
 * //  w  w w  . j  av a  2s .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.executequery.databaseobjects.impl.TableColumnConstraint.java

private String translateDeferrabilityRule(Short value) {

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

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

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

        case DatabaseMetaData.importedKeyNotDeferrable:
            return translated + " - importedKeyNotDeferrable";
        }/*from  w  w  w .  j  a  va  2  s  .  c  o  m*/

    }
    return translated;
}