MINI MINI MANI MO

Path : /opt/oracle/product/18c/dbhomeXE/md/admin/
File Upload :
Current File : //opt/oracle/product/18c/dbhomeXE/md/admin/semu112.sql

Rem
Rem $Header: sdo/admin/semu112.sql /main/24 2017/07/05 11:00:10 rjanders Exp $
Rem
Rem semu112.sql
Rem
Rem Copyright (c) 2010, 2017, Oracle and/or its affiliates. 
Rem All rights reserved.
Rem
Rem    NAME
Rem      semu112.sql - <one-line expansion of the name>
Rem
Rem    DESCRIPTION
Rem      <short description of component this file declares/defines>
Rem
Rem    NOTES
Rem      <other useful comments, qualifications, etc.>
Rem
Rem    BEGIN SQL_FILE_METADATA
Rem    SQL_SOURCE_FILE: sdo/admin/semu112.sql
Rem    SQL_SHIPPED_FILE: md/admin/semu112.sql
Rem    SQL_PHASE: UPGRADE
Rem    SQL_STARTUP_MODE: NORMAL
Rem    SQL_IGNORABLE_ERRORS: NONE  
Rem    SQL_CALLING_FILE: NONE 
Rem    END SQL_FILE_METADATA
Rem    
Rem    MODIFIED   (MM/DD/YY)
Rem    rjanders    06/30/17 - Fix SYS/MDSYS prefix
Rem    rjanders    05/10/17 - #26037683: Raise 'when other' exceptions
Rem    rjanders    04/17/17 - More 'exception when others' changes
Rem    rjanders    04/13/17 - #25814260: Allow quoted bumpy-case usernames
Rem    rjanders    03/23/17 - #25437999: Remove 'when others then NULL'
Rem                           handlers
Rem    sravada     01/11/17 - bug 25370681
Rem    matperry    01/25/16 - #22578872: remove calls to number_to_char
Rem    rjanders    02/01/15 - #20418139: Cannot grant SELECT to PUBLIC
Rem    rjanders    01/02/15 - long identifier project phase 1
Rem    matperry    05/12/14 - stack 12.2 upgrade
Rem    rjanders    01/29/14 - #18146131: Use NUMBER_TO_CHAR() for TO_CHAR()
Rem    matperry    11/18/13 - remove set echo off
Rem    rjanders    09/16/13 - OCCS: Remove hardtabs
Rem    rjanders    03/16/13 - #16473696: Start/End _ORACLE_SCRIPT
Rem                           initialization
Rem    matperry    10/26/12 - populate flags column in rdf_model_internal$
Rem                           during upgrade
Rem    matperry    09/25/12 - create tables for imp/exp
Rem    matperry    06/26/12 - refactor for 12.1 upgrade scheme: object type
Rem                           upgrades moved to installation scripts
Rem    matperry    06/21/12 - add 12.1 upgrade
Rem    sdas        05/22/11 - move uniq and pkey indexes on IOTs to proper
Rem                           tablespace
Rem    sdas        05/20/11 - interface change for prllidx_create subprograms
Rem    spsundar    05/13/11 - update extractor interfaces
Rem    matperry    03/31/11 - update link_id column
Rem    matperry    03/10/11 - refactor: move view recreation to post semrelod
Rem                           processing using sdo_rdf_internal
Rem    matperry    03/10/11 - add named graph migration
Rem    matperry    10/15/10 - update datatype indexing for orageo:WKTLiteral
Rem    matperry    04/02/10 - Created
Rem

Rem ********************************************************************
Rem #16473696: Indicate Oracle-Supplied object
@@?/rdbms/admin/sqlsessstart.sql
Rem ********************************************************************

------------------------------------------------------------------------------------------
-- The "u" script for each prior release performs the following actions to upgrade      --
-- from release NNN to NNN+1. If NNN+1 is not the current release, then the "u" script  --
-- invokes the script for release NNN+1, so that the upgrade is "stacked.".             --
------------------------------------------------------------------------------------------

alter session set current_schema=MDSYS;
set define on;

-- make sure we do the upgrade without errors --
whenever sqlerror continue;

-- First mark that we are upgrading --
declare
  valid_exists NUMBER;
  upg_desc     VARCHAR2(100);
  object_not_found exception;
  pragma exception_init(object_not_found, -04043);
  synonym_not_found exception;
  pragma exception_init(synonym_not_found, -01432);
begin

  EXECUTE IMMEDIATE '
    SELECT COUNT(*) 
    FROM MDSYS.RDF_PARAMETER
    WHERE namespace = :1 and
          attribute = :2 and
          description = :3'
  INTO valid_exists
  USING 'MDSYS','SEM_VERSION','VALID';

  if (valid_exists > 0) then
    upg_desc := 'UPGRADING_VALID';
  else
    upg_desc := 'UPGRADING_INVALID';
  end if;

  EXECUTE IMMEDIATE '
    UPDATE MDSYS.RDF_PARAMETER
    SET description = :1
    WHERE namespace = :2 and
          attribute = :3'
  USING upg_desc,'MDSYS','SEM_VERSION';

  EXCEPTION WHEN OTHERS THEN 
    SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
      'SEM_INSTALL: semu112: set upgrade in rdf_parameter: '||SQLERRM);
end;
/

prompt preparing semantic network for named graph support ...

--///////////////////////// Upgrade SEM for Named Graph Support \\\\\\\\\\\\\\\\\\\\\\\\\\--

--//////////// Drop obsolete objects (views, packages, etc.) from prior release \\\\\\\\\\--

-- drop sdo_sem_magic package ... not used anywhere
-- also cleanup sem_hash_internal
declare
  stmt varchar2(1000);
  table_not_found exception;
  pragma exception_init(table_not_found, -00942);
  cannot_revoke exception;
  pragma exception_init(cannot_revoke, -01927);
  object_not_found exception;
  pragma exception_init(object_not_found, -04043);
begin

  begin
    stmt := 'drop package mdsys.sdo_sem_magic'; execute immediate stmt;
    exception
      when object_not_found then NULL;
      when others then 
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: drop sdo_sem_magic: '||SQLERRM);
  end;

  begin
    stmt := 'drop package mdsys.sem_hash_internal'; execute immediate stmt;
    exception
      when object_not_found then NULL;
      when others then 
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: drop sem_hash_internal: '||SQLERRM);
  end;

  begin
    stmt := 'revoke execute on mdsys.ordmd_sem_libs from public'; execute immediate stmt;
    exception
      when cannot_revoke then NULL;
      when others then 
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: revoke ordmd_sem_libs: '||SQLERRM);
  end;

end;
/

--/////////// ALTER tables to add/change columns and constraints for the new release \\\\\\\\\\--

-- Need to backup and truncate existing RDFCTX Indexes before we can update RDF_LINK$ for Named Graph --
prompt backing up existing SemContext indexes ...

declare
  rdfctx_exists    number;
  rdfctx_ids       SYS.ODCINumberList;
  idx_names        SYS.ODCIVarchar2List;
  idx_owners       SYS.ODCIVarchar2List;
  stmt             varchar2(1000);
  tblspc_clause    varchar2(2000);
  tblspc_name      varchar2(128);
  backup_failed    boolean := false;
  ng_exists        number;
  object_exists exception;
  pragma exception_init(object_exists, -00955);
begin
  -- see if rdfctx indexes may be present in DB
  SELECT count(*) INTO rdfctx_exists FROM SYS.ALL_TABLES
  WHERE table_name='RDFCTX_INDEX_POLICIES$' AND owner='MDSYS';

  -- see if named graph already exists
  SELECT count(*) INTO ng_exists FROM sys.all_tab_columns 
  WHERE table_name='RDF_LINK$' AND column_name='G_ID' AND owner='MDSYS';

  -- ensure that we have not already migrated to named graph --
  if (rdfctx_exists > 0 and ng_exists = 0) then

    -- get the rdfctx model ids
    -- skip virtual models
    execute immediate 
      'select distinct link_part_id
       from mdsys.rdfctx_index_policies$
       where link_part_id > 0'
    bulk collect into rdfctx_ids;

    -- if we have some indexes, back them up
    if (rdfctx_ids.count > 0) then

      -- get the tablespace for the network
      tblspc_clause := '';
      begin
        execute immediate '
          select tablespace_name
          from   SYS.all_tables
          where  owner=''MDSYS'' and table_name=''RDF_VALUE$'''
        into tblspc_name;

        if (tblspc_name is not null) then
          tblspc_clause := ' tablespace ' || 
            sys.dbms_assert.enquote_name(tblspc_name, false);
        end if;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: tablespace for rdf_link_png$ tables: '||SQLERRM);
      end;

      -- create table to hold existing index data --
      begin
        execute immediate 
          'CREATE TABLE mdsys.rdf_link_png$ (
             p_value_id            NUMBER NOT NULL
           , start_node_id         NUMBER
           , canon_end_node_id     NUMBER
           , end_node_id           NUMBER
           , model_id              NUMBER
           , cost                  NUMBER
           , ctxt1                 NUMBER
           , ctxt2                 VARCHAR2(4000)
           , distance              NUMBER
           , explain               VARCHAR2(4000)
           , path                  VARCHAR2(4000)
           ) 
           PARTITION BY LIST (model_id) (PARTITION MODEL_0 VALUES (0)) ' ||
           tblspc_clause || 
           ' COMPRESS';

        exception
          when object_exists then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: not creating rdf_link_png$ ... it already exists');
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: create rdf_link_png$: '||SQLERRM);
      end;

      -- pre-create partitions in rdf_link_png$ --
      begin
        for i in rdfctx_ids.first .. rdfctx_ids.last 
        loop
          begin

            execute immediate
              'ALTER TABLE  mdsys.rdf_link_png$ 
               ADD PARTITION MODEL_' || to_char(rdfctx_ids(i),'TM9','NLS_Numeric_Characters=''.,''') || ' 
               values(' || to_char(rdfctx_ids(i),'TM9','NLS_Numeric_Characters=''.,''') || ')';

            exception
              when others then
                SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                  'SEM_INSTALL: semu112: add rdf_link_png$ partition: '||SQLERRM);
          end;
        end loop;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: loop through add rdf_link_png$ partitions: '||SQLERRM);
      end;

      -- insert existing index data into rdf_link_png$ --
      begin
        execute immediate
          'insert /*+ APPEND PARALLEL */ into mdsys.rdf_link_png$
             (p_value_id, start_node_id, canon_end_node_id, model_id, 
              cost, ctxt1, ctxt2, distance, explain, path)
           select /*+ PARALLEL */
              p_value_id, start_node_id, canon_end_node_id, model_id, 
              cost, ctxt1, ctxt2, distance, explain, path
           from mdsys.rdf_link$
           where model_id in (select * from table(cast(:1 as sys.ODCINumberList)))'
        using rdfctx_ids; 

        commit; 

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: insert into rdf_link_png$ '||SQLERRM);
            backup_failed := true;
      end;

      -- mark existing indexes as invalid
      begin
        execute immediate 
          'select index_owner, index_name
           from mdsys.rdfctx_index_policies$'
        bulk collect into idx_owners, idx_names;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: get index names and owners '||SQLERRM);
      end;

      begin
        for i in idx_owners.first .. idx_owners.last
        loop
          begin
            execute immediate
              'ALTER INDEX ' || sys.dbms_assert.enquote_name(idx_owners(i),false) || 
                '.' || sys.dbms_assert.enquote_name(idx_names(i),false) ||
              ' UNUSABLE';

            exception
              when others then
                SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                  'SEM_INSTALL: semu112: alter index '||idx_owners(i)||
                    '.'||idx_names(i)||': '||SQLERRM);
          end;
        end loop; 

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: loop through index names and owners '||SQLERRM);
      end;

      -- truncate link$ partitions for old indexes
      if (not backup_failed) then
        begin
          for i in rdfctx_ids.first .. rdfctx_ids.last
          loop
            begin
              execute immediate
                'ALTER TABLE MDSYS.RDF_LINK$
                 TRUNCATE PARTITION (MODEL_'||to_char(rdfctx_ids(i),'TM9','NLS_Numeric_Characters=''.,''')||')';
              commit;

              exception
                when others then
                  SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                    'SEM_INSTALL: semu112: truncate partition model_'||rdfctx_ids(i)||
                      ': '||SQLERRM);
            end;
          end loop;
            exception
              when others then
                SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                  'SEM_INSTALL: semu112: error truncating index partitions: '||SQLERRM);
        end;
      else
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: not truncating index partitions ... backup failed');
      end if;

    end if;

  end if;
end;
/

prompt ... finished backing up SemContext indexes

------------------------- Update RDF_LINK$ for Named Graph -----------------------
-- First add new column to RDF_LINK$ 
prompt adding new columns to semantic network tables ...

declare
  network_exists number;
  ng_exists      number;
  link_id_ok     boolean;
  stmt           varchar2(1000);
  dd_vc          varchar2(4000);
  dd_vc_len      integer;
  dd_cursor      integer;
  dd_stmt        varchar2(4000);
  junk           integer;
begin
  -- see if RDF Network is present in DB
  SELECT count(*) INTO network_exists FROM SYS.ALL_TABLES
  WHERE table_name='RDF_VALUE$' AND owner='MDSYS';

  if (network_exists > 0) then

    -- see if named graph already exists 
    SELECT count(*) INTO ng_exists FROM sys.all_tab_columns 
    WHERE table_name='RDF_LINK$' AND column_name='G_ID' AND owner='MDSYS';

    if (ng_exists = 0) then
    -- add g_id --
      begin
        stmt := 'alter table mdsys.rdf_link$ add (g_id number default null)';
        execute immediate stmt;
        exception
          when others then 
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: add g_id to link$: '||SQLERRM);
      end;
    end if; 

    -- Update link_id if needed. No harm is done if we redefine link_id,
    -- but this could take a long time for large amounts of data, so 
    -- let's make sure it's actually needed first.
    link_id_ok := false;
    begin
      dd_cursor := dbms_sql.open_cursor();
      dd_stmt := 
        'select data_default
         from sys.all_tab_columns
         where table_name=''RDF_LINK$'' and
               column_name=''LINK_ID'' and
               owner=''MDSYS''';
      dbms_sql.parse(dd_cursor, dd_stmt, dbms_sql.NATIVE);
      dbms_sql.define_column_long(dd_cursor, 1);
      junk := dbms_sql.execute(dd_cursor);

      if (dbms_sql.fetch_rows(dd_cursor) > 0) then
        dbms_sql.column_value_long(
          dd_cursor, 1, 4000, 0, dd_vc, dd_vc_len);

        if (INSTR(NLS_UPPER(dd_vc), 'G_ID') > 0) then
          link_id_ok := true;
        end if;
      end if;

      dbms_sql.close_cursor(dd_cursor);

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: check link_id: '||SQLERRM);
          dbms_sql.close_cursor(dd_cursor);
    end;

    if (not(link_id_ok)) then
      -- drop link_id --
      begin
        stmt := 'alter table mdsys.rdf_link$ drop (link_id)';
        execute immediate stmt;
        exception
          when others then 
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: drop link_id from link$: '||SQLERRM);
      end;

      -- add new link_id --
      begin
        stmt := 
          'alter table mdsys.rdf_link$ add
            (link_id as (
              to_char(model_id,         ''FMXXXXXXXXXXXXXXXX'') ||
              (CASE when g_id IS NOT NULL then
                         ''g''|| to_char(g_id,  ''FMXXXXXXXXXXXXXXXX'')
                    else NULL
               END)                                             ||''_''||
              to_char(start_node_id,    ''FMXXXXXXXXXXXXXXXX'') ||''_''||
              to_char(p_value_id,       ''FMXXXXXXXXXXXXXXXX'') ||''_''||
              to_char(canon_end_node_id,''FMXXXXXXXXXXXXXXXX'')))
          ';
        execute immediate stmt;
        exception
          when others then 
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: add link_id to link$: '||SQLERRM);
      end;
    end if;

  end if;
end;
/

prompt finished adding new columns to semantic network tables ...

-- Next modify unique index
prompt creating new indexes ...

declare
  network_exists number;
  ng_idx_exists      number;
  numIdxCols     pls_integer := 0;
  optCol         varchar2(128);
  optComp        varchar2(128);
  idx_name       varchar2(128);
begin

  -- see if RDF Network is present in DB
  SELECT count(*) INTO network_exists FROM SYS.ALL_TABLES
  WHERE table_name='RDF_VALUE$' AND owner='MDSYS';

  -- see if named graph index already exists
  -- sdordfsa uses pcsgml_idx, so check for that too ...
  SELECT count(*) INTO ng_idx_exists FROM SYS.all_indexes 
  WHERE table_name='RDF_LINK$' 
    AND index_name IN ('RDF_LNK_PCSGM_IDX','RDF_LNK_PCSGML_IDX')
    AND owner='MDSYS';

  if (network_exists > 0 and ng_idx_exists = 0) then
    -- See if existing index is 4 or 5 columns. 
    -- If 5 columns, then it has been modified for optional components
    begin
      execute immediate '
        select count(*)
        from SYS.all_ind_columns
        where index_name = ''RDF_LNK_PVIDCENSNMID_IDX''
          and index_owner = ''MDSYS'''
      into numIdxCols;

      exception
        when others then 
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: get num idx columns$: '||SQLERRM);
    end;

    -- if only 4 columns then create plain PCSGM unique index
    if (numIdxCols = 4) then
      begin
        execute immediate '
          create unique index mdsys.rdf_lnk_pcsgm_idx 
          on mdsys.rdf_link$ (
            p_value_id, canon_end_node_id, start_node_id, g_id, model_id)
          compress 2 nologging local parallel';
        exception
          when others then 
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: create 4-column index: '||SQLERRM);
      end;

    -- if 5 columns then create modified g_id based index 
    elsif (numIdxCols = 5) then
      begin
        -- see what modified index we have
        execute immediate '
          select attribute
          from mdsys.rdf_parameter
          where value = ''ENABLED''
            and attribute in (''RDFCTX'',''RDFOLS'',''RDFOWM'')'
        into optComp;

        idx_name := 'MDSYS.RDF_LNK_PCSGM_IDX';
        if (optComp = 'RDFOLS') then
          optCol := ', CTXT1';
          -- rdfsa uses pcsgml_idx ...
          idx_name := 'MDSYS.RDF_LNK_PCSGML_IDX';
        elsif (optComp = 'RDFOWM') then
          optCol := ', SUBSTR(CTXT2,1,600)';
        else 
          -- RDFCTX uses GID now, not CTXT2
          optCol := '';
        end if;

        -- create new PCSGM index --
        execute immediate '
          create unique index '|| idx_name ||'
          on mdsys.rdf_link$ (
            p_value_id, canon_end_node_id, start_node_id, g_id, model_id'||optCol||')
          compress 2 nologging local parallel';

        exception
          when others then 
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: create 5-column index: '||SQLERRM);
      end;
    -- unique index is screwed up
    else
      SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
        'SEM_INSTALL: semu112: Unexpected number of columns in RDF_LNK_PVIDCENSNMID_IDX index');
    end if;

    -- now drop old unique index --
    begin
      execute immediate '
        drop index mdsys.rdf_lnk_pvidcensnmid_idx';
      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: drop old unique index: '||SQLERRM);
    end;

  end if;

end;
/

prompt ... finished creating new indexes

--/////////////////////// Modify static tables for the new release \\\\\\\\\\\\\\\\\\\\\\--


--//////////// UPDATE existing columns as needed to reflect new algorithms, etc. \\\\\\\\--

    
--////////////////////// Create new types and tables for the release \\\\\\\\\\\\\\\\\\\\--

prompt creating new tables and views ...

-- Create new SEM_PRIV$ view
declare
  network_exists number;
begin
  -- see if RDF Network is present in DB
  SELECT count(*) INTO network_exists FROM SYS.ALL_TABLES
  WHERE table_name='RDF_VALUE$' AND owner='MDSYS';

  if (network_exists > 0) then

    -- create RDF_PRIV$ view if absent
    begin
      EXECUTE IMMEDIATE '
        CREATE OR REPLACE VIEW MDSYS.rdf_priv$ AS
        select model_id dummy_column 
          from rdf_link$ partition (model_0) 
         where 1=0
      ';
      EXECUTE IMMEDIATE 'GRANT READ ON MDSYS.rdf_priv$ TO PUBLIC';
      EXCEPTION WHEN OTHERS THEN 
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: rdf_priv$: '||SQLERRM);
    end;

  end if;
end;
/

-- create new view for datatype indexing support 
-- only create view if an rdf network exists
declare
  network_exists NUMBER;
begin
  -- see if RDF Network is present in DB
  SELECT count(*) INTO network_exists FROM SYS.ALL_TABLES
  WHERE table_name='RDF_VALUE$' AND owner='MDSYS';

  if (network_exists > 0) then 

    begin
      execute immediate
       'CREATE OR REPLACE VIEW RDF_DTYPE_INDEX_INFO (
          DATATYPE, INDEX_NAME, STATUS, TABLESPACE_NAME)
        AS
          SELECT idx_map.dtype DATATYPE, a.index_name INDEX_NAME, a.status STATUS, a.tablespace_name TABLESPACE_NAME
          FROM SYS.all_indexes a,
          (
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#decimal'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#double'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#float'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#integer'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#int'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#nonPositiveInteger'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#negativeInteger'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#long'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#short'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#byte'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#nonNegativeInteger'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#unsignedLong'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#unsignedInt'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#unsignedShort'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#unsignedByte'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$NUMERIC_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#positiveInteger'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$BOOL_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#boolean'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$STR_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#string'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$DATE_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#date'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$TIME_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#time'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$DATETIME_IDX'' index_name, ''http://www.w3.org/2001/XMLSchema#dateTime'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$GEO_IDX'' index_name, ''http://xmlns.oracle.com/rdf/geo/WKTLiteral'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$GEO_IDX'' index_name, ''http://www.opengis.net/ont/geosparql#wktLiteral'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$TEXT_IDX'' index_name, ''http://xmlns.oracle.com/rdf/text'' dtype from SYS.DUAL)
            UNION ALL
           (select ''RDF_V$LIKE_IDX'' index_name, ''http://xmlns.oracle.com/rdf/like'' dtype from sys.dual)
          ) idx_map
          WHERE owner=''MDSYS'' AND table_name=''RDF_VALUE$'' AND
                a.index_name = idx_map.index_name';

      execute immediate 'CREATE OR REPLACE VIEW SEM_DTYPE_INDEX_INFO AS SELECT * FROM MDSYS.RDF_DTYPE_INDEX_INFO';

      execute immediate 'GRANT READ ON SEM_DTYPE_INDEX_INFO     TO PUBLIC';
      execute immediate 'GRANT READ ON RDF_DTYPE_INDEX_INFO     TO PUBLIC';
  
      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: sem_dtype_index_info: '||SQLERRM);
    end;

  end if;
end;
/

-- create stats tables and views
-- only create view if an rdf network exists
declare
  network_exists   NUMBER;
  tblspc_name      VARCHAR2(128);
  tblspc_clause    VARCHAR2(300);
  ps_dol_exists    NUMBER;
  ps_idx_exists    NUMBER;
  rts_dol_exists   NUMBER;
  rts_idx_exists   NUMBER;
begin
  -- see if RDF Network is present in DB
  SELECT count(*) INTO network_exists FROM SYS.ALL_TABLES
  WHERE table_name='RDF_VALUE$' AND owner='MDSYS';

  if (network_exists > 0) then 

    -- see if stats tables/indexes exist --
    SELECT count(*) INTO ps_dol_exists FROM SYS.ALL_TABLES
    WHERE table_name='RDF_PRED_STATS$' AND owner='MDSYS';

    SELECT count(*) INTO ps_idx_exists FROM SYS.ALL_INDEXES
    WHERE index_name='RDF_PRED_STATS$LOCALPK' AND owner='MDSYS';

    SELECT count(*) INTO rts_dol_exists FROM SYS.ALL_TABLES
    WHERE table_name='RDF_TERM_STATS$' AND owner='MDSYS';

    SELECT count(*) INTO rts_idx_exists FROM SYS.ALL_INDEXES
    WHERE index_name='RDF_TERM_STATS$LOCALPK' AND owner='MDSYS';

    -- get the tablespace 
    tblspc_clause := '';
    begin
      execute immediate '
        select tablespace_name
        from   SYS.all_tables
        where  owner=''MDSYS'' and table_name=''RDF_VALUE$'''
      into tblspc_name;

      if (tblspc_name is not null) then
        tblspc_clause := ' tablespace ' || 
          sys.dbms_assert.enquote_name(tblspc_name, false);
      end if;

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: tablespace for stats tables: '||SQLERRM);
    end;

    begin
      if (ps_dol_exists = 0) then
        execute immediate '
        CREATE TABLE rdf_pred_stats$ (
            value_id              NUMBER 
          , instances             NUMBER
          , start_node_fanout     NUMBER
          , end_node_fanout       NUMBER
          , model_id              NUMBER
          , g_id                  NUMBER
          )
          PARTITION BY LIST (model_id) (PARTITION MODEL_0 VALUES (0)) ' ||
          tblspc_clause ||
          ' COMPRESS NOLOGGING';
       end if;

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: tablespace rdf_pred_stats$: '||SQLERRM);
    end;

    begin
      if (ps_idx_exists = 0) then
        -- add local partition index
        execute immediate '
        ALTER TABLE    rdf_pred_stats$
        ADD CONSTRAINT rdf_pred_stats$pk PRIMARY KEY(model_id,value_id)
        USING INDEX (
          CREATE UNIQUE INDEX rdf_pred_stats$localpk 
          ON                  rdf_pred_stats$(model_id,value_id) 
          COMPRESS NOLOGGING LOCAL
        )' ;
      end if;

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: rdf_pred_stats$pk: '||SQLERRM);
    end;

    begin
      if (rts_dol_exists = 0) then
        execute immediate '
        CREATE TABLE rdf_term_stats$ (
            value_id              NUMBER 
          , start_node_instances  NUMBER
          , end_node_instances    NUMBER
          , model_id              NUMBER
          , g_id                  NUMBER
          )
          PARTITION BY LIST (model_id) (PARTITION MODEL_0 VALUES (0)) ' ||
          tblspc_clause ||
          ' COMPRESS NOLOGGING';
       end if;

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: rdf_term_stats$: '||SQLERRM);
    end;

    begin
      if (rts_idx_exists = 0) then
        -- add local partition index
        execute immediate '
        ALTER TABLE    rdf_term_stats$
        ADD CONSTRAINT rdf_term_stats$pk PRIMARY KEY(model_id,value_id)
        USING INDEX (
          CREATE UNIQUE INDEX rdf_term_stats$localpk 
          ON                  rdf_term_stats$(model_id,value_id) 
          COMPRESS NOLOGGING LOCAL
        )' ;
      end if;

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: rdf_term_stats$pk: '||SQLERRM);
    end;

  end if;
end;
/

-- move rdf_network_index_internal$ from sysaux to rdf tablepsce
declare
  idx_tab_exists NUMBER;
  tblspc_name VARCHAR2(128);
  tblspc_clause VARCHAR2(300);
  idx_tblspc_name VARCHAR2(128);
begin

  -- see if index table is present in DB
  SELECT count(*) INTO idx_tab_exists FROM SYS.ALL_TABLES
  WHERE table_name='RDF_NETWORK_INDEX_INTERNAL$' AND owner='MDSYS';

  if (idx_tab_exists > 0) then

    -- get the tablespace 
    tblspc_clause := '';
    begin
      execute immediate '
        select tablespace_name
        from   SYS.all_tables
        where  owner=''MDSYS'' and table_name=''RDF_VALUE$'''
      into tblspc_name;

      if (tblspc_name is not null) then
        tblspc_clause := ' tablespace ' || 
          sys.dbms_assert.enquote_name(tblspc_name, false);
      end if;

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: target tablespace for rdf_network_index_internal$: '||SQLERRM);
    end;

    -- check the tablespace for index_internal$
    idx_tblspc_name := '';
    begin
      execute immediate '
        select tablespace_name
        from   SYS.all_tables
        where  owner=''MDSYS'' and table_name=''RDF_NETWORK_INDEX_INTERNAL$'''
      into idx_tblspc_name;

      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: current tablespace for rdf_network_index_internal$: '||SQLERRM);
    end;

    -- move if current != target
    if (tblspc_name <> idx_tblspc_name) then

      -- move the table
      begin
        execute immediate ' 
          alter table mdsys.rdf_network_index_internal$
          move ' || tblspc_clause;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: move rdf_network_index_internal$: '||SQLERRM);
      end;

      -- move the pkey and uniq indexes on IOT metadata tables
      begin
        execute immediate ' 
          alter table mdsys.sem_indextype_metadata$
          move ' || tblspc_clause;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: move mdsys.sem_indextype_metadata$: '||SQLERRM);
      end;

      begin
        execute immediate ' 
          alter index mdsys.SEM_INDEXTYPE_METADATA_UBT
          rebuild ' || tblspc_clause;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: rebuild index mdsys.SEM_INDEXTYPE_METADATA_UBT: '||SQLERRM);
      end;

      begin
        execute immediate ' 
          alter table mdsys.sem_sim_indextype_metadata$
          move ' || tblspc_clause;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: move mdsys.sem_sim_indextype_metadata$: '||SQLERRM);
      end;

      begin
        execute immediate ' 
          alter index mdsys.SEM_SIM_INDEXTYPE_METADATA_UBT
          rebuild ' || tblspc_clause;

        exception
          when others then
            SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
              'SEM_INSTALL: semu112: rebuild index mdsys.SEM_SIM_INDEXTYPE_METADATA_UBT: '||SQLERRM);
      end;

    end if;
  end if;

end;
/

---------- Update RDFCTX views ------
declare
  cnt          number;
  mdata_exists number;
  cols_exist   number;
  table_not_found exception;
  pragma exception_init(table_not_found, -00942);
begin

  -- first see if rdfctx is installed
  cnt := 0;
  begin
    execute immediate
      'select count(*) 
       from mdsys.rdf_parameter
       where namespace = ''COMPONENT'' 
         and attribute = ''RDFCTX'''
    into cnt;
    exception
      when table_not_found then NULL;
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE,
                               'EXCEPTION[semu112.sql(' || $$PLSQL_LINE || ')1]: ' || SQLERRM);
  end;

  -- if rdfctx is installed then keep going
  if (cnt > 0) then

    -- alter metadata tables --
    begin
      -- make sure table is there and missing columns to be added 
      execute immediate
       'select count(*) 
        from SYS.all_tables
        where owner = ''MDSYS'' and
              table_name = ''RDFCTX_INDEX_POLICIES$'''
      into mdata_exists;

      execute immediate
       'select count(*) 
        from sys.all_tab_columns
        where owner = ''MDSYS'' and
              table_name = ''RDFCTX_INDEX_POLICIES$'' and
              column_name = ''INDEX_PARTITION'''
      into cols_exist;

      if (mdata_exists > 0 and cols_exist = 0) then

        -- add new columns --
        begin
          execute immediate
           'alter table mdsys.rdfctx_index_policies$
             add (index_partition varchar2(130) default ''MY_PARTITION'')
             add (extr_params varchar2(100))';

          exception
            when others then
              SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                'SEM_INSTALL: semu112: alter rdfctx_index_policies$ add columns: '||SQLERRM);
        end;

        -- initialize new column values --
        begin
          execute immediate
           'update mdsys.rdfctx_index_policies$
             set index_partition = index_name';

          commit;

          exception
            when others then
              SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                'SEM_INSTALL: semu112: update rdfctx_index_policies$: '||SQLERRM);
        end;

        -- update with new constraint --
        begin
          execute immediate
           'alter table mdsys.rdfctx_index_policies$
            drop constraint rdfctx_ipol_pkey cascade';

          exception
            when others then
              SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                'SEM_INSTALL: semu112: alter rdfctx_index_policies$ drop constraint: '||SQLERRM);
        end;

        begin
          execute immediate
           'alter table mdsys.rdfctx_index_policies$
            add constraint rdfctx_ipol_pkey primary key (
                   index_owner, index_name, index_partition, policy_rid)';

          exception
            when others then
              SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                'SEM_INSTALL: semu112: alter rdfctx_index_policies$ add constraint: '||SQLERRM);
        end;

        -- recreate metadata view --
        begin
          execute immediate
           'create or replace view mdsys.rdfctx_index_policies
                 (index_owner, index_name, index_partition, policy_name, extr_parameters, is_default, status,
                  rdf_model) as
            (select cip.index_owner, cip.index_name, 
                    (case when (cip.index_name=cip.index_partition) then null else cip.index_partition end),
                    cp.policy_name, cip.extr_params,
                    decode(bitand(flags,1), 1, ''YES'',''NO''), 
                    decode(bitand(flags,6), 2, ''INPROGRESS'',
                                            4, ''FAILED'', ''VALID''), 
                    ''RDFCTX_MOD_''||abs(link_part_id)
             from mdsys.rdfctx_index_policies$ cip, SYS.all_indexes ai, 
                  mdsys.rdfctx_policies$ cp 
             where ai.owner = cip.index_owner and ai.index_name = cip.index_name
                   and cip.policy_rid = cp.rowid)';

          exception
            when others then
              SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
                'SEM_INSTALL: semu112: create mdsys.rdfctx_index_policies view: '||SQLERRM);
        end;

      end if;

    end;

  end if;
     
end;
/

prompt ... finished creating new tables and views

------- 12.1 stuff --------

-- rdb2rdf flags column in rdf_model$
declare
  flags_exists number;

  -- constants for model types: ids and names
  MODEL_TYPE_ID_REGULAR   CONSTANT SIMPLE_INTEGER :=     1;
  MODEL_TYPE_ID_VIRTUAL   CONSTANT SIMPLE_INTEGER :=     2;
  MODEL_TYPE_ID_RDFCTX    CONSTANT SIMPLE_INTEGER :=     4;
  MODEL_TYPE_ID_RDB2RDF   CONSTANT SIMPLE_INTEGER :=     8;
  MODEL_TYPE_ID_VERSIONED CONSTANT SIMPLE_INTEGER :=     16;

  MODEL_TYPE_REGULAR   CONSTANT CHAR(1) :=     'M';
  MODEL_TYPE_VIRTUAL   CONSTANT CHAR(1) :=     'V';
  MODEL_TYPE_RDFCTX    CONSTANT CHAR(1) :=     'X';
  MODEL_TYPE_RDB2RDF   CONSTANT CHAR(1) :=     'D';
  MODEL_TYPE_VERSIONED CONSTANT CHAR(1) :=     'W';
begin

  -- see if flags column already exists
  SELECT count(*) INTO flags_exists FROM sys.all_tab_columns 
  WHERE table_name='RDF_MODEL_INTERNAL$' AND column_name='FLAGS' AND owner='MDSYS';

  if (flags_exists = 0) then
    -- add flags column --
    begin
      execute immediate
        'ALTER TABLE mdsys.rdf_model_internal$ add (flags NUMBER default 0)';
      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: alter rdf_model_internal$: '||SQLERRM);
    end;
    -- populate flags column --
    -- virtual models (negative id) --
    begin
      execute immediate
        'UPDATE mdsys.rdf_model_internal$ 
         SET flags = :1
         WHERE model_id < 0'
      using MODEL_TYPE_ID_VIRTUAL;
      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: update virtual model flags: '||SQLERRM);
    end;
    -- rdfctx models (positive id and no application table) --
    begin
      execute immediate
        'UPDATE mdsys.rdf_model_internal$ 
         SET flags = :1
         WHERE table_id IS NULL AND flags = 0'
      using MODEL_TYPE_ID_RDFCTX;
      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: update rdfctx model flags: '||SQLERRM);
    end;
    -- version enabled models --
    begin
      execute immediate
        'UPDATE mdsys.rdf_model_internal$ 
         SET flags = :1
         WHERE model_id IN
          (select rm.model_id 
           from SYS.all_objects ao, mdsys.rdf_model_internal$ rm 
           where ao.owner = ''MDSYS'' and 
                 ao.object_type = ''TRIGGER'' and 
                 ao.object_name = ''WMLT$IT''||rm.model_name)'
      using MODEL_TYPE_ID_VERSIONED;
      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: update versioned model flags: '||SQLERRM);
    end;
    -- everything else is regular (RDB2RDF not possible in 11.2) --
    begin
      execute immediate
        'UPDATE mdsys.rdf_model_internal$ 
         SET flags = :1
         WHERE flags = 0'
      using MODEL_TYPE_ID_REGULAR;
      exception
        when others then
          SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
            'SEM_INSTALL: semu112: update regular model flags: '||SQLERRM);
    end;
    commit;

  end if;
end;
/

-- go ahead and replace view even if it's already updated, zero-time operation
declare
  flag_decode  varchar2(4000);
  dyn_sql_stmt varchar2(30000);

  -- constants for model types: ids and names
  MODEL_TYPE_ID_REGULAR   CONSTANT SIMPLE_INTEGER :=     1;
  MODEL_TYPE_ID_VIRTUAL   CONSTANT SIMPLE_INTEGER :=     2;
  MODEL_TYPE_ID_RDFCTX    CONSTANT SIMPLE_INTEGER :=     4;
  MODEL_TYPE_ID_RDB2RDF   CONSTANT SIMPLE_INTEGER :=     8;
  MODEL_TYPE_ID_VERSIONED CONSTANT SIMPLE_INTEGER :=     16;

  MODEL_TYPE_REGULAR   CONSTANT CHAR(1) :=     'M';
  MODEL_TYPE_VIRTUAL   CONSTANT CHAR(1) :=     'V';
  MODEL_TYPE_RDFCTX    CONSTANT CHAR(1) :=     'X';
  MODEL_TYPE_RDB2RDF   CONSTANT CHAR(1) :=     'D';
  MODEL_TYPE_VERSIONED CONSTANT CHAR(1) :=     'W';

  wm_view     boolean;
  wm_vc       varchar2(4000);
  wm_vc_len   integer;
  wm_cursor   integer;
  wm_stmt     varchar2(4000);
  junk        integer;
  apptabName  varchar2(128);
begin

  -- check view definition to see if we have a WM-enabled MODEL$ view
  wm_view := false;
  begin
    wm_cursor := dbms_sql.open_cursor();
    wm_stmt := 
      'select text
       from sys.all_views
       where view_name=''RDF_MODEL$'' and
             owner=''MDSYS''';
    dbms_sql.parse(wm_cursor, wm_stmt, dbms_sql.NATIVE);
    dbms_sql.define_column_long(wm_cursor, 1);
    junk := dbms_sql.execute(wm_cursor);

    if (dbms_sql.fetch_rows(wm_cursor) > 0) then
      dbms_sql.column_value_long(
        wm_cursor, 1, 4000, 0, wm_vc, wm_vc_len);

      if (INSTR(NLS_UPPER(wm_vc), 'GET_ORIG_APPTAB_NAME') > 0) then
        wm_view := true;
      end if;
    end if;

    dbms_sql.close_cursor(wm_cursor);

    exception
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: check for wm rdf_model$ view: '||SQLERRM);
        dbms_sql.close_cursor(wm_cursor);
  end;

  -- set correct application table name based on WM 
  apptabName := 'ao.object_name';
  if (wm_view) then
    apptabName := 'mdsys.sem_rdfwm_dr.get_orig_apptab_name(au.username, ao.object_name)';
  end if;

  flag_decode := 
    ', case rmi.flags
        when ' || MODEL_TYPE_ID_REGULAR || ' then ' 
               || sys.dbms_assert.enquote_literal(sys.dbms_assert.simple_sql_name(MODEL_TYPE_REGULAR)) || ' 
        when ' || MODEL_TYPE_ID_VIRTUAL || ' then ' 
               || sys.dbms_assert.enquote_literal(sys.dbms_assert.simple_sql_name(MODEL_TYPE_VIRTUAL)) || ' 
        when ' || MODEL_TYPE_ID_RDFCTX || ' then ' 
               || sys.dbms_assert.enquote_literal(sys.dbms_assert.simple_sql_name(MODEL_TYPE_RDFCTX)) || ' 
        when ' || MODEL_TYPE_ID_RDB2RDF || ' then ' 
               || sys.dbms_assert.enquote_literal(sys.dbms_assert.simple_sql_name(MODEL_TYPE_RDB2RDF)) || ' 
        when ' || MODEL_TYPE_ID_VERSIONED || ' then ' 
               || sys.dbms_assert.enquote_literal(sys.dbms_assert.simple_sql_name(MODEL_TYPE_VERSIONED)) || ' 
        else                                     to_char(rmi.flags)
      end flags';

  dyn_sql_stmt := '
    CREATE OR REPLACE VIEW mdsys.rdf_model$ (
      owner
    , model_id
    , model_name
    , table_name
    , column_name
    , model_tablespace_name
    , model_type
    ) AS
    SELECT 
      au.username
    , rmi.model_id
    , rmi.model_name
    , ' || apptabName || '
    , rmi.column_name
    , rmi.model_tablespace_name
    ' || flag_decode || '
    FROM mdsys.rdf_model_internal$ rmi,sys.all_users au,sys.all_objects ao
    WHERE rmi.owner_id=au.user_id AND ao.owner=au.username AND
          rmi.table_id=ao.object_id AND rmi.model_id > 0
    UNION ALL
    SELECT
      au.username
    , rmi.model_id
    , rmi.model_name
    , NULL
    , NULL
    , NULL
    ' || flag_decode || '
    FROM mdsys.rdf_model_internal$ rmi, sys.all_users au
    WHERE rmi.owner_id=au.user_id AND 
          rmi.model_id < 0
    UNION ALL
    SELECT
      au.username
    , rmi.model_id
    , rmi.model_name
    , NULL
    , NULL
    , NULL
    ' || flag_decode || '
    FROM mdsys.rdf_model_internal$ rmi, sys.all_users au
    WHERE rmi.owner_id=au.user_id AND 
          rmi.flags=' || MODEL_TYPE_ID_RDB2RDF || ' AND
          rmi.model_id > 0
    UNION ALL
    SELECT
      au.username
    , rmi.model_id
    , rmi.model_name
    , NULL
    , NULL
    , rmi.model_tablespace_name
    ' || flag_decode || '
    FROM mdsys.rdf_model_internal$ rmi, sys.all_users au
    WHERE rmi.owner_id=au.user_id AND 
          rmi.model_name = ''RDFCTX_MOD_''||rmi.model_id
    ';

  begin
    execute immediate dyn_sql_stmt;

    exception
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: re-create rdf_model$ view: '||SQLERRM);
  end;

  begin
    execute immediate
     'CREATE OR REPLACE VIEW MDSYS.SEM_MODEL$ AS SELECT * FROM MDSYS.RDF_MODEL$';

    exception
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: re-create sem_model$ view: '||SQLERRM);
  end;

end;
/

----- Correct metadata for virtual models from rdfctx -------
-- dependent entailments were set to 'V', should be 'X'
-- go ahead and run this even if metadata has already been corrected
-- won't do any harm
begin
  execute immediate
   'update mdsys.rdf_precomp_dep$
    set deptyp=''X''
    where deptyp=''V'' and 
          depid in (select model_id from mdsys.rdf_precomp$)';
  exception
    when others then
      SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
        'SEM_INSTALL: semu112: update rdf_precomp_dep$: '||SQLERRM);
end;
/

-------------- Create tables for full database export -------------
declare
  tblspc_name VARCHAR2(128);
  tblspc_clause VARCHAR2(300);
  table_not_found exception;
  pragma exception_init(table_not_found, -00942);
begin

  -- get the tablespace 
  tblspc_clause := '';
  begin
    execute immediate '
      select tablespace_name
      from   SYS.all_tables
      where  owner=''MDSYS'' and table_name=''RDF_VALUE$'''
    into tblspc_name;

    if (tblspc_name is not null) then
      tblspc_clause := ' tablespace ' || 
        sys.dbms_assert.enquote_name(tblspc_name, false);
    end if;

    exception
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: tablespace for imp/exp tables: '||SQLERRM);
  end;

  -- this dummy table is needed for export of RDF_MODEL$ view from 11.2.0.3
  begin 
    execute immediate '
      DROP TABLE MDSYS.RDF_MODEL$_TBL';
    exception
      when table_not_found then NULL;
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: drop rdf_model$_tbl: '||SQLERRM);
  end;
  begin 
    execute immediate '
      CREATE TABLE MDSYS.RDF_MODEL$_TBL ' ||
      tblspc_clause || '
        as select * from MDSYS.RDF_MODEL$ where 0=1';
    exception
      when table_not_found then NULL;
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: create rdf_model$_tbl: '||SQLERRM);
  end;

  -- this dummy table is needed for export of SEM_DTYPE_INDEX_INFO view from 11.2.0.3
  begin 
    execute immediate '
      DROP TABLE MDSYS.RDF_DTYPE_INDEX_INFO_TBL';
    exception
      when table_not_found then NULL;
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: drop rdf_dtype_index_info_tbl: '||SQLERRM);
  end;
  begin 
    execute immediate '
      CREATE TABLE MDSYS.RDF_DTYPE_INDEX_INFO_TBL ' ||
      tblspc_clause || '
        as select * from MDSYS.RDF_DTYPE_INDEX_INFO where 0=1';
    exception
      when table_not_found then NULL;
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: create rdf_dtype_index_info_tbl: '||SQLERRM);
  end;

  -- for export of grant info from 11.2.0.3: populated at export system_callout
  begin
    execute immediate '
      DROP TABLE MDSYS.RDF_GRANT_INFO$';
    exception
      when table_not_found then NULL;
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: drop rdf_grant_info$: '||SQLERRM);
  end;
  begin
    execute immediate '
      CREATE TABLE MDSYS.RDF_GRANT_INFO$ (
        grantor VARCHAR2(128), grantee VARCHAR2(128), grantopt NUMBER
       ,privilege VARCHAR2(40), objowner VARCHAR2(128), objname VARCHAR2(128)
      ) ' ||
      tblspc_clause;
    exception
      when others then
        SYS.DBMS_SYSTEM.KSDWRT(SYS.DBMS_SYSTEM.TRACE_FILE, 
          'SEM_INSTALL: semu112: create rdf_grant_info$: '||SQLERRM);
  end;
end;
/

--//////// ALTER existing types to add/change attributes and methods for the new release \\\\\\\\--

--/////////// GRANT any additional privileges required by MDSYS for the new release \\\\\\\\\\\\\--

prompt ... finished preparing semantic network for named graph support

-- stack 12.2 upgrade --
@@semu121.sql

alter session set current_schema=SYS;

Rem ********************************************************************
Rem #16473696: Indicate Oracle-Supplied object
@@?/rdbms/admin/sqlsessend.sql
Rem ********************************************************************


OHA YOOOO