--
-- Recreate script van Facilitor
--
/*
 * C-style comment is removed from the result
 */
#include "modules.h"
#include "comsql.h"

SET TERMOUT ON
SET FEEDBACK OFF
SET ECHO OFF
SET PAGESIZE 0
SPOOL OFF
SET HEADING OFF
PROMPT ***********************************************************************
PROMPT
PROMPT RECREATE SCRIPT van Facilitor
PROMPT $Revision$
PROMPT $Id$
PROMPT
PROMPT Re-creëert alle algemene views, triggers etc van Faciltor schema _DBV
PROMPT
PROMPT Het wordt aangeraden om deze upgrade te draaien op een moment dat er
PROMPT geen gebruikers bezig zijn met de applicatie.
PROMPT
PROMPT De recreate wordt uitgevoerd. Even geduld a.u.b...
PROMPT

SET TERMOUT ON
SET HEADING OFF
SET FEEDBACK ON
SET ECHO ON
SET VERIFY OFF
SET LINESIZE 1000
SET TIME ON
SET SERVEROUTPUT ON;
SET DEFINE ON
column fcltlogfile new_value fcltlogfile NOPRINT;
select 'RECRE'||_DBV_STRING || '_' || UPPER(USER || '_' || sys_context('USERENV', 'SERVICE_NAME') || '_' || sys_context('USERENV', 'SERVER_HOST')) || '_' || to_char(sysdate,'YYYYMMDD_HH24MISS') || '.lst' as fcltlogfile from dual;
spool &fcltlogfile

column 1 new_value 1 noprint
select '' "1" from dual where rownum = 0;
define forceit = &1 "donot"

WHENEVER SQLERROR EXIT;
DECLARE
    tekst VARCHAR2 (1000);
    fac_schema fac_version.fac_version_schema%TYPE;
BEGIN
    SELECT fac_version_schema
      INTO fac_schema
      FROM fac_version;
    IF SUBSTR(fac_schema, 1, 2) <> SUBSTR(_DBV_STRING, 1, 2) AND '&forceit' != 'FORCE' THEN
        tekst := '=================================================================' || CHR(10)
              || 'This recreate can only be applied to a DB'|| SUBSTR(_DBV_STRING, 1, 2) || ' database' || CHR(10)
              || 'Current database is version DB'|| fac_schema || CHR(10)
              || 'Add parameter FORCE when calling this script to force the recreate' || CHR(10)
              || 'This recreate script is now cancelled!' || CHR(10)
              || '=================================================================' || CHR(10);
        RAISE_APPLICATION_ERROR(-20000, 'OR'||'A-' || CHR(10) || tekst);
    END IF;
EXCEPTION
  WHEN OTHERS THEN
    IF SQLCODE <> -20000 THEN
        RAISE_APPLICATION_ERROR(-20000, 'Could not find table FAC_VERSION. Not a Facilitor database?');
    ELSE
        RAISE;
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE;

SELECT 'The current version of Facilitor@' || USER || ' is: DB' || fac_version_schema
  FROM fac_version;

SET DEFINE OFF

SELECT adm.systeminfo FROM DUAL;

variable recreate_start number
exec :recreate_start := dbms_utility.get_time

/* first compile all views, triggers etc */
BEGIN DBMS_UTILITY.COMPILE_SCHEMA(USER, FALSE); END;
/
PROMPT List of all invalid objects *before* Facilitor upgrade.
SELECT 'OR'||'A-'||'DB'||_DBV_STRING||' Warning: ' || object_type || ' ' || object_name || ' was ' || status || ' before the upgrade.'
  FROM user_objects
 WHERE object_type IN ('VIEW', 'TRIGGER', 'PACKAGE', 'PROCEDURE', 'FUNCTION', 'PACKAGE BODY')
   AND status <> 'VALID'
 ORDER BY object_name;

#include "recreate.inc"

select 'Recreate has '||'completed in ' || ROUND(( dbms_utility.get_time - :recreate_start )/100) || ' seconds.' from dual;

/* Report invalid objects, if any */
SET ECHO OFF
SET HEADING OFF
SET LINESIZE 132
PROMPT List of all invalid objects after Facilitor upgrade:
SELECT 'OR'||'A-'||'DB'||_DBV_STRING||' Warning: ' || object_type || ' ' || object_name || ' is ' || status
  FROM user_objects
 WHERE object_type IN ('VIEW', 'TRIGGER', 'PACKAGE', 'PROCEDURE', 'FUNCTION', 'PACKAGE BODY')
   AND status <> 'VALID'
 ORDER BY object_name;

SET HEADING ON
column name format a32
column text format a60
column line format 9999
column pos format 9999
SET HEADING ON
SELECT TYPE, NAME, LINE, POSITION POS, TEXT
  FROM USER_ERRORS
 ORDER BY NAME, TYPE, SEQUENCE;

SET ECHO ON
REGISTERONCE('$Id$')

INSERT INTO adm_tracking
    (adm_tracking_name, adm_tracking_revision)
VALUES
    ('Database recreated', 'DB' || _DBV_STRING);
COMMIT;

SPOOL OFF
SET ECHO OFF
SET DEFINE ON
PROMPT Recreate is done. Check &fcltlogfile
