-- ============================================================
-- Optional pre-recording reset.
-- Drops only the three local Views, Database Link, and
-- Credential owned by this demo. Host ACL entries are retained.
-- Redmine: #730
-- ============================================================

DECLARE
  v_count PLS_INTEGER;
BEGIN
  FOR object_row IN (
    SELECT object_name
    FROM user_objects
    WHERE object_type = 'VIEW'
      AND object_name IN (
        'HMM_RDS_CARRIERS_V',
        'HMM_RDS_CARRIER_PERF_V',
        'HMM_RDS_CARRIER_LATEST_V'
      )
    ORDER BY CASE object_name
      WHEN 'HMM_RDS_CARRIER_LATEST_V' THEN 1
      WHEN 'HMM_RDS_CARRIER_PERF_V' THEN 2
      ELSE 3
    END
  ) LOOP
    EXECUTE IMMEDIATE 'DROP VIEW ' || object_row.object_name;
  END LOOP;

  SELECT COUNT(*) INTO v_count
  FROM user_db_links
  WHERE db_link = 'HMM_RDS_PG_LINK';

  IF v_count = 1 THEN
    EXECUTE IMMEDIATE 'DROP DATABASE LINK HMM_RDS_PG_LINK';
  END IF;

  SELECT COUNT(*) INTO v_count
  FROM all_credentials
  WHERE credential_name = 'HMM_RDS_PG_CRED';

  IF v_count = 1 THEN
    DBMS_CLOUD.DROP_CREDENTIAL(
      credential_name => 'HMM_RDS_PG_CRED'
    );
  END IF;
END;
/

SELECT credential_name, username
FROM all_credentials
WHERE credential_name = 'HMM_RDS_PG_CRED';

SELECT db_link, username, host
FROM user_db_links
WHERE db_link = 'HMM_RDS_PG_LINK';

SELECT object_name, object_type
FROM user_objects
WHERE object_name IN (
  'HMM_RDS_CARRIERS_V',
  'HMM_RDS_CARRIER_PERF_V',
  'HMM_RDS_CARRIER_LATEST_V'
);
