-- ===================================================================
-- MARS-1005-PREHOOK INSTALL SCRIPT: Fix DATA_EXPORTER RFC 4180 Compliance
-- ===================================================================
-- Purpose: Pre-hook for MARS-1005 - Deploy updated DATA_EXPORTER (v2.15.0)
--          that exports CSV files in RFC 4180 compliant format.
-- Background: DATA_EXPORTER v2.14.0 uses escape=true in DBMS_CLOUD.EXPORT_DATA,
--             which produces backslash-escaped embedded quotes (\")
--             instead of RFC 4180 doubling ("").
--             ODS external tables (FIELDS CSV WITHOUT EMBEDDED) expect RFC 4180.
--             Fix: remove escape=true so Oracle uses RFC 4180 doubling.
-- Author: Grzegorz Michalski
-- Date: 2026-03-10

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
-- Log files are automatically created in log/ subdirectory
-- IMPORTANT: Ensure log/ directory exists before SPOOL (use host mkdir)
host mkdir log 2>nul

var filename VARCHAR2(100)
BEGIN
    :filename := 'log/INSTALL_MARS_1005_PREHOOK_' || SYS_CONTEXT('USERENV', 'CON_NAME') || '_' || TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS') || '.log';
END;
/
column filename new_value _filename
select :filename filename from dual;
spool &_filename

SET ECHO OFF
SET TIMING ON
SET SERVEROUTPUT ON SIZE UNLIMITED
SET PAUSE OFF

PROMPT =========================================================================
PROMPT MARS-1005-PREHOOK: Fix DATA_EXPORTER RFC 4180 Compliance
PROMPT =========================================================================
PROMPT
PROMPT Problem: DATA_EXPORTER v2.14.0 uses escape=true in DBMS_CLOUD.EXPORT_DATA
PROMPT          which causes backslash-escaped embedded quotes (\")
PROMPT          instead of RFC 4180 doubling (""). ODS external tables with
PROMPT          FIELDS CSV WITHOUT EMBEDDED reject rows with backslash-escape.
PROMPT
PROMPT This script will:
PROMPT - Deploy CT_MRDS.DATA_EXPORTER v2.15.0 (removes escape=true parameter)
PROMPT - Exported CSV files will use RFC 4180 doubling ("") for embedded quotes
PROMPT - ODS external tables (FIELDS CSV WITHOUT EMBEDDED) are NOT modified
PROMPT
PROMPT Expected Duration: 1-2 minutes
PROMPT =========================================================================

-- Confirm installation with user
ACCEPT continue CHAR PROMPT 'Type YES to continue with installation, or Ctrl+C to abort: '
WHENEVER SQLERROR EXIT SQL.SQLCODE
BEGIN
    IF '&continue' IS NULL OR TRIM('&continue') IS NULL OR UPPER(TRIM('&continue')) != 'YES' THEN
        RAISE_APPLICATION_ERROR(-20999, 'Installation aborted by user.');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

PROMPT
PROMPT =========================================================================
PROMPT Step 1: Deploy DATA_EXPORTER v2.15.0 (RFC 4180 Fix)
PROMPT =========================================================================
@@00_MARS_1005_PREHOOK_install_DATA_EXPORTER.sql

PROMPT
PROMPT =========================================================================
PROMPT MARS-1005-PREHOOK Installation - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete installation details.
PROMPT For rollback, use: rollback_mars1005_prehook.sql
PROMPT =========================================================================

spool off

quit;
