rem NAME : ckcompat.sql
rem
rem
rem  Change History

DECLARE

-- This is the base patch level for 6.5 production (4.0.12.30)
-- Source exported repositories with this level or greater are 
-- assumed to be compatible with the target Repository
-- It is assumed that noone will be importing into a pre-production
-- Repository.

patch_level  constant Number := 30;

-- Exceptions trapped in RON 

mismatched_repositories   EXCEPTION;

-- Error conditions placed on to the PL/SQL error stack 
cda_error            constant varchar2(3):= 'CDA'; 
get_repos_vn_fail    constant number     := 6075;
chk_repos_vn_fail    constant number     := 6076;

unhdl_orcl_err    constant number := 2105;

PROCEDURE check_repos_version (extra_condition IN OUT Varchar2) IS
  -- This procedure executes a number of compatibility checks between
  -- the source and target repositories
  --  The 'MODEL' versions must be equal
  --  The API code and schema version numbers must be equal
  --  The patch release number (dd.dd.dd.Pnum.dd) must be compatible
  -- A default compatibility check is supplied 
  -- (source patch release number >= PATCH_LEVEL)
  -- but this can be overriden by passing in an 'extra_condition' via 
  -- the Import interface
  -- If required the patch level check could be made to check for specific
  -- patch numbers.
  utility_name   varchar2(27)  := 'Repository 6.5 Import:';
  proc   varchar2(100) := 'check_repos_version ';
  -- Check the details of the Repository instance.
  -- The source and target must be compatible versions.

  csr NUMBER;
  exe number;

BEGIN
  IF extra_condition IS NULL OR extra_condition = '' THEN 
    extra_condition := ' AND ' ||
     'substr(t.repository_version,instr(t.repository_version,''.'',1, 3)+1   ' ||
     '                             ,instr(t.repository_version,''.'',1, 4)-1 ' ||
     '                            - instr(t.repository_version,''.'',1, 3) ) ' ||
     ' >= ' || patch_level;
  END IF;

  csr := DBMS_SQL.OPEN_CURSOR;
  rmdbg.trace(utility_name||proc || 'loc 3.1');
  rmdbg.trace(' SELECT null FROM rm$repositories t, xtsys_rm$repositories s '||
     ' WHERE substr(s.repository_version,1,instr(s.repository_version,''.'',1, 3)) '||
     '     = substr(t.repository_version,1,instr(t.repository_version,''.'',1, 3)) '||
     '   AND s.code_version       = t.code_version ' ||
     '   AND s.schema_version     = t.schema_version'||
     extra_condition);
  DBMS_SQL.PARSE(csr,
     ' SELECT null FROM rm$repositories t, xtsys_rm$repositories s '||
     ' WHERE substr(s.repository_version,1,instr(s.repository_version,''.'',1, 3)) '||
     '     = substr(t.repository_version,1,instr(t.repository_version,''.'',1, 3)) '||
     '   AND s.code_version       = t.code_version ' ||
     '   AND s.schema_version     = t.schema_version'||
     extra_condition, DBMS_SQL.NATIVE);
  
  rmdbg.trace(utility_name||proc || 'loc 3.2');
  IF DBMS_SQL.EXECUTE_AND_FETCH(csr) = 1 THEN 
                                             -- one and only one row should be returned
    DBMS_SQL.CLOSE_CURSOR(csr);
    rmdbg.trace(utility_name||proc || ' passed test');

  ELSE 
    rmdbg.trace(utility_name||proc || ' failed test');
    RAISE mismatched_repositories;
  END IF;

  EXCEPTION
    WHEN mismatched_repositories THEN
      rmdbg.trace(utility_name||proc || 'loc 3.3 ' ||SQLCODE);
      DBMS_SQL.CLOSE_CURSOR(csr);
      rmmes.post(cda_error, chk_repos_vn_fail, 'RM$REPOSITORIES');
      rmmes.post('ORA', SQLCODE);
      RAISE ;

    WHEN OTHERS THEN
      rmdbg.trace(utility_name||proc || 'loc 3.4 ' ||SQLCODE);
      DBMS_SQL.CLOSE_CURSOR(csr);
      rmmes.post('ORA', SQLCODE);
      RAISE;

END check_repos_version ;

BEGIN

-- interface section

  rmmes.clear;
  check_repos_version(:extra_condition);

--  :iserror := 0;
  EXCEPTION 
   WHEN OTHERS THEN
   :iserror := 1;
   :sqlcode := sqlcode;

END;