private ResultSet getForeignKeys(
      final String client, String parentCatalog,
      String parentSchema,
      final String parentTable, final String foreignCatalog,
      final String foreignSchema,
      final String foreignTable) throws SQLException
  {
    raiseSQLExceptionIfConnectionIsClosed();
    Statement statement = connection.createStatement();
    StringBuilder commandBuilder = new StringBuilder();

    // apply session context when catalog is unspecified
    SFPair<String, String> resPair = applySessionContext(parentCatalog,
                                                         parentSchema);
    parentCatalog = resPair.left;
    parentSchema = resPair.right;

    if (client.equals("export") || client.equals("cross"))
    {
      commandBuilder.append("show /* JDBC:DatabaseMetaData.getForeignKeys() */ " +
                            "exported keys in ");
    }
    else if (client.equals("import"))
    {
      commandBuilder.append("show /* JDBC:DatabaseMetaData.getForeignKeys() */ " +
                            "imported keys in ");
    }

    if (parentCatalog == null)
    {
      commandBuilder.append("account");
    }
    else if (parentCatalog.isEmpty())
    {
      return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_FOREIGN_KEYS, statement);
    }
    else
    {
      if (parentSchema == null)
      {
        commandBuilder.append("database \"" + parentCatalog + "\"");
      }
      else if (parentSchema.isEmpty())
      {
        return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_FOREIGN_KEYS, statement);
      }
      else
      {
        if (parentTable == null)
        {
          commandBuilder.append("schema \"" + parentCatalog + "\".\"" +
                                parentSchema + "\"");
        }
        else if (parentTable.isEmpty())
        {
          return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_FOREIGN_KEYS, statement);
        }
        else
        {
          commandBuilder.append("table \"" + parentCatalog + "\".\"" +
                                parentSchema + "\".\"" + parentTable + "\"");
        }
      }
    }

    final String finalParentCatalog = parentCatalog;
    final String finalParentSchema = parentSchema;

    String command = commandBuilder.toString();

    ResultSet resultSet = executeAndReturnEmptyResultIfNotFound(statement, command, GET_FOREIGN_KEYS);

    return new SnowflakeDatabaseMetaDataQueryResultSet(GET_FOREIGN_KEYS, resultSet, statement)
    {
      @Override
      public boolean next() throws SQLException
      {
        logger.debug("public boolean next()");
        increamentRow();

        while (showObjectResultSet.next())
        {
          // Get the value for each field to display
          String pktable_cat = showObjectResultSet.getString(2);
          String pktable_schem = showObjectResultSet.getString(3);
          String pktable_name = showObjectResultSet.getString(4);
          String pkcolumn_name = showObjectResultSet.getString(5);
          String fktable_cat = showObjectResultSet.getString(6);
          String fktable_schem = showObjectResultSet.getString(7);
          String fktable_name = showObjectResultSet.getString(8);
          String fkcolumn_name = showObjectResultSet.getString(9);
          int key_seq = showObjectResultSet.getInt(10);
          short updateRule = getForeignKeyConstraintProperty(
              "update", showObjectResultSet.getString(11));
          short deleteRule = getForeignKeyConstraintProperty(
              "delete", showObjectResultSet.getString(12));
          String fk_name = showObjectResultSet.getString(13);
          String pk_name = showObjectResultSet.getString(14);
          short deferrability = getForeignKeyConstraintProperty(
              "deferrability", showObjectResultSet.getString(15));

          boolean passedFilter = false;

          // Post filter the results based on the clinet type
          if (client.equals("import"))
          {
            // For imported dkeys, filter on the foreign key table
            if ((finalParentCatalog == null || finalParentCatalog.equals(fktable_cat)) &&
                (finalParentSchema == null || finalParentSchema.equals(fktable_schem)) &&
                (parentTable == null || parentTable.equals(fktable_name)))
            {
              passedFilter = true;
            }
          }
          else if (client.equals("export"))
          {
            // For exported keys, filter on the primary key table
            if ((finalParentCatalog == null || finalParentCatalog.equals(pktable_cat)) &&
                (finalParentSchema == null || finalParentSchema.equals(pktable_schem)) &&
                (parentTable == null || parentTable.equals(pktable_name)))
            {
              passedFilter = true;
            }
          }
          else if (client.equals("cross"))
          {
            // For cross references, filter on both the primary key and foreign
            // key table
            if ((finalParentCatalog == null || finalParentCatalog.equals(pktable_cat)) &&
                (finalParentSchema == null || finalParentSchema.equals(pktable_schem)) &&
                (parentTable == null || parentTable.equals(pktable_name)) &&
                (foreignCatalog == null ||
                 foreignCatalog.equals(fktable_cat)) &&
                (foreignSchema == null ||
                 foreignSchema.equals(fktable_schem)) &&
                (foreignTable == null ||
                 foreignTable.equals(fktable_name)))
            {
              passedFilter = true;
            }
          }

          if (passedFilter)
          {
            nextRow[0] = pktable_cat;
            nextRow[1] = pktable_schem;
            nextRow[2] = pktable_name;
            nextRow[3] = pkcolumn_name;
            nextRow[4] = fktable_cat;
            nextRow[5] = fktable_schem;
            nextRow[6] = fktable_name;
            nextRow[7] = fkcolumn_name;
            nextRow[8] = key_seq;
            nextRow[9] = updateRule;
            nextRow[10] = deleteRule;
            nextRow[11] = fk_name;
            nextRow[12] = pk_name;
            nextRow[13] = deferrability;
            return true;
          }
        }
        close();
        return false;
      }
    };
  }
private ResultSet getForeignKeys ( final String client , String parentCatalog , String parentSchema , final String parentTable , final String foreignCatalog , final String foreignSchema , final String foreignTable ) throws SQLException { raiseSQLExceptionIfConnectionIsClosed ( ) ; Statement statement = connection . createStatement ( ) ; StringBuilder commandBuilder = new StringBuilder ( ) ; SFPair < String , String > resPair = applySessionContext ( parentCatalog , parentSchema ) ; parentCatalog = resPair . left ; parentSchema = resPair . right ; if ( client . equals ( "export" ) || client . equals ( "cross" ) ) { commandBuilder . append ( "show /* JDBC:DatabaseMetaData.getForeignKeys() */ " + "exported keys in " ) ; } else if ( client . equals ( "import" ) ) { commandBuilder . append ( "show /* JDBC:DatabaseMetaData.getForeignKeys() */ " + "imported keys in " ) ; } if ( parentCatalog == null ) { commandBuilder . append ( "account" ) ; } else if ( parentCatalog . isEmpty ( ) ) { return SnowflakeDatabaseMetaDataResultSet . getEmptyResultSet ( GET_FOREIGN_KEYS , statement ) ; } else { if ( parentSchema == null ) { commandBuilder . append ( "database \"" + parentCatalog + "\"" ) ; } else if ( parentSchema . isEmpty ( ) ) { return SnowflakeDatabaseMetaDataResultSet . getEmptyResultSet ( GET_FOREIGN_KEYS , statement ) ; } else { if ( parentTable == null ) { commandBuilder . append ( "schema \"" + parentCatalog + "\".\"" + parentSchema + "\"" ) ; } else if ( parentTable . isEmpty ( ) ) { return SnowflakeDatabaseMetaDataResultSet . getEmptyResultSet ( GET_FOREIGN_KEYS , statement ) ; } else { commandBuilder . append ( "table \"" + parentCatalog + "\".\"" + parentSchema + "\".\"" + parentTable + "\"" ) ; } } } final String finalParentCatalog = parentCatalog ; final String finalParentSchema = parentSchema ; String command = commandBuilder . toString ( ) ; ResultSet resultSet = executeAndReturnEmptyResultIfNotFound ( statement , command , GET_FOREIGN_KEYS ) ; return new SnowflakeDatabaseMetaDataQueryResultSet ( GET_FOREIGN_KEYS , resultSet , statement ) { @ Override public boolean next ( ) throws SQLException { logger . debug ( "public boolean next()" ) ; increamentRow ( ) ; while ( showObjectResultSet . next ( ) ) { String pktable_cat = showObjectResultSet . getString ( 2 ) ; String pktable_schem = showObjectResultSet . getString ( 3 ) ; String pktable_name = showObjectResultSet . getString ( 4 ) ; String pkcolumn_name = showObjectResultSet . getString ( 5 ) ; String fktable_cat = showObjectResultSet . getString ( 6 ) ; String fktable_schem = showObjectResultSet . getString ( 7 ) ; String fktable_name = showObjectResultSet . getString ( 8 ) ; String fkcolumn_name = showObjectResultSet . getString ( 9 ) ; int key_seq = showObjectResultSet . getInt ( 10 ) ; short updateRule = getForeignKeyConstraintProperty ( "update" , showObjectResultSet . getString ( 11 ) ) ; short deleteRule = getForeignKeyConstraintProperty ( "delete" , showObjectResultSet . getString ( 12 ) ) ; String fk_name = showObjectResultSet . getString ( 13 ) ; String pk_name = showObjectResultSet . getString ( 14 ) ; short deferrability = getForeignKeyConstraintProperty ( "deferrability" , showObjectResultSet . getString ( 15 ) ) ; boolean passedFilter = false ; if ( client . equals ( "import" ) ) { if ( ( finalParentCatalog == null || finalParentCatalog . equals ( fktable_cat ) ) && ( finalParentSchema == null || finalParentSchema . equals ( fktable_schem ) ) && ( parentTable == null || parentTable . equals ( fktable_name ) ) ) { passedFilter = true ; } } else if ( client . equals ( "export" ) ) { if ( ( finalParentCatalog == null || finalParentCatalog . equals ( pktable_cat ) ) && ( finalParentSchema == null || finalParentSchema . equals ( pktable_schem ) ) && ( parentTable == null || parentTable . equals ( pktable_name ) ) ) { passedFilter = true ; } } else if ( client . equals ( "cross" ) ) { if ( ( finalParentCatalog == null || finalParentCatalog . equals ( pktable_cat ) ) && ( finalParentSchema == null || finalParentSchema . equals ( pktable_schem ) ) && ( parentTable == null || parentTable . equals ( pktable_name ) ) && ( foreignCatalog == null || foreignCatalog . equals ( fktable_cat ) ) && ( foreignSchema == null || foreignSchema . equals ( fktable_schem ) ) && ( foreignTable == null || foreignTable . equals ( fktable_name ) ) ) { passedFilter = true ; } } if ( passedFilter ) { nextRow [ 0 ] = pktable_cat ; nextRow [ 1 ] = pktable_schem ; nextRow [ 2 ] = pktable_name ; nextRow [ 3 ] = pkcolumn_name ; nextRow [ 4 ] = fktable_cat ; nextRow [ 5 ] = fktable_schem ; nextRow [ 6 ] = fktable_name ; nextRow [ 7 ] = fkcolumn_name ; nextRow [ 8 ] = key_seq ; nextRow [ 9 ] = updateRule ; nextRow [ 10 ] = deleteRule ; nextRow [ 11 ] = fk_name ; nextRow [ 12 ] = pk_name ; nextRow [ 13 ] = deferrability ; return true ; } } close ( ) ; return false ; } } ; }
Retrieves the foreign keys

@param client         type of foreign key
@param parentCatalog  database name
@param parentSchema   schema name
@param parentTable    table name
@param foreignCatalog other database name
@param foreignSchema  other schema name
@param foreignTable   other table name
@return foreign key columns in result set