-- ===================================================================
-- MARS-1005-PREHOOK INSTALL SCRIPT: Fix DATA_EXPORTER RFC 4180 Compliance
-- ===================================================================
-- Purpose: Pre-hook for MARS-1005 - Deploy updated DATA_EXPORTER (v2.17.0)
--          that fixes RFC 4180 CSV compliance and Parquet format corruption.
-- 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 ("").
--             v2.16.0 fixed CSV by applying REPLACE(col, '"', '""') in SELECT,
--             but this corrupted Parquet exports containing double-quote values.
--             v2.17.0 applies REPLACE only when pFormat = 'CSV', leaving
--             Parquet exports unchanged.
-- 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 which produces \"-escaped
PROMPT          quotes instead of RFC 4180 doubling "". v2.16.0 fixed CSV but
PROMPT          applied REPLACE to Parquet exports too, corrupting quote values.
PROMPT
PROMPT This script will:
PROMPT - Deploy CT_MRDS.DATA_EXPORTER v2.17.0 (RFC 4180 CSV + Parquet fix)
PROMPT - CSV exports use RFC 4180 doubling ("") for embedded quotes
PROMPT - Parquet exports pass column values unchanged (no REPLACE applied)
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.17.0 (Parquet Fix + RFC 4180)
PROMPT =========================================================================
@@00_MARS_1005_PREHOOK_install_DATA_EXPORTER.sql

PROMPT
PROMPT =========================================================================
PROMPT Step 2: Track Package Versions
PROMPT =========================================================================
@@track_package_versions.sql

PROMPT
PROMPT =========================================================================
PROMPT Step 3: Verify Package Versions
PROMPT =========================================================================
@@verify_packages_version.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;
