Beispiel #1
0
 public void refreshPrimaryKeys(DatabaseMetaData metaData) throws SQLException {
   primaryKeys.clear();
   ResultSet rs = null;
   try {
     rs = metaData.getPrimaryKeys(catalog, schema, name);
     while (rs.next()) {
       PrimaryKey pk = new PrimaryKey();
       primaryKeys.add(pk);
       pk.setOwner(this);
       pk.setColumnName(rs.getString("COLUMN_NAME"));
       pk.setKeySequence(rs.getShort("KEY_SEQ"));
       pk.setName(rs.getString("PK_NAME"));
     }
   } catch (SQLException sqle) {
     throw sqle;
   } finally {
     try {
       rs.close();
     } catch (Exception ex) {
     }
   }
 }
  @Override
  protected void readPrimaryKeys(
      DatabaseSnapshot snapshot, String schema, DatabaseMetaData databaseMetaData)
      throws DatabaseException, SQLException {
    Database database = snapshot.getDatabase();
    updateListeners("Reading primary keys for " + database.toString() + " ...");

    // we can't add directly to the this.primaryKeys hashSet because adding columns to an exising PK
    // changes the hashCode and .contains() fails
    List<PrimaryKey> foundPKs = new ArrayList<PrimaryKey>();
    // Setting default schema name. Needed for correct statement generation
    if (schema == null) schema = database.convertRequestedSchemaToSchema(schema);

    String query =
        "select uc.table_name TABLE_NAME,ucc.column_name COLUMN_NAME,ucc.position KEY_SEQ,uc.constraint_name PK_NAME,ui.tablespace_name TABLESPACE from all_constraints uc,all_indexes ui,all_cons_columns ucc where uc.constraint_type = 'P' and uc.index_name = ui.index_name and uc.constraint_name = ucc.constraint_name and uc.owner = '"
            + schema
            + "' and ui.table_owner = '"
            + schema
            + "' and ucc.owner = '"
            + schema
            + "' and uc.table_name = ui.table_name and ui.table_name = ucc.table_name";
    Statement statement = null;
    ResultSet rs = null;
    try {
      statement =
          ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
      rs = statement.executeQuery(query);

      while (rs.next()) {
        String tableName = convertFromDatabaseName(rs.getString("TABLE_NAME"));
        String tablespace = convertFromDatabaseName(rs.getString("TABLESPACE"));
        String columnName = convertFromDatabaseName(rs.getString("COLUMN_NAME"));
        short position = rs.getShort("KEY_SEQ");

        boolean foundExistingPK = false;
        for (PrimaryKey pk : foundPKs) {
          if (pk.getTable().getName().equals(tableName)) {
            pk.addColumnName(position - 1, columnName);

            foundExistingPK = true;
          }
        }

        if (!foundExistingPK && !database.isLiquibaseTable(tableName)) {
          PrimaryKey primaryKey = new PrimaryKey();
          primaryKey.setTablespace(tablespace);
          Table table = snapshot.getTable(tableName);
          if (table == null) {
            continue; // probably a different schema
          }
          primaryKey.setTable(table);
          primaryKey.addColumnName(position - 1, columnName);
          primaryKey.setName(convertPrimaryKeyName(rs.getString("PK_NAME")));

          foundPKs.add(primaryKey);
        }
      }
    } finally {
      JdbcUtils.closeResultSet(rs);
      JdbcUtils.closeStatement(statement);
    }

    snapshot.getPrimaryKeys().addAll(foundPKs);
  }