-- ===================================================================
-- MARS-1049 MAIN INSTALL SCRIPT: CSV Encoding Support
-- ===================================================================
-- Purpose: Complete installation of CSV encoding support in FILE_MANAGER system
-- Author: Grzegorz Michalski
-- Date: 2025-11-24
-- Version: 1.1.0

-- Dynamic spool file generation
var filename VARCHAR2(100)
BEGIN
    SELECT 'INSTALL_MARS_1049_' || SUBSTR(PDB_NAME, (INSTR(PDB_NAME,'_',1)+1), (LENGTH(PDB_NAME)-INSTR(PDB_NAME,'_',1))) ||  '_' ||TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS')||'.log' INTO :filename from DBA_PDBS;
END;
/
column filename new_value _filename
select :filename filename from dual;
spool &_filename
-- 
-- IMPLEMENTATION STATUS: COMPLETED and DEPLOYED
-- - A_SOURCE_FILE_CONFIG: ENCODING VARCHAR2(50) column added
-- - CT_MRDS.FILE_MANAGER: Updated to version 3.2.1 with pEncoding parameter
-- - ODS.FILE_MANAGER_ODS: Updated to version 2.1.0 with pEncoding wrapper support  
-- - JSON Implementation: Uses JSON_OBJECT due to Oracle 23c compatibility
--
-- IMPORTANT: Tests are NOT part of this installation.
--            Testing is separate and optional (see test/ directory).
-- 
-- DEPLOYMENT ORDER (CRITICAL):
-- 0. Grant SELECT ANY DICTIONARY to CT_MRDS (for cross-schema hash tracking)
-- 1. Database structure changes (A_SOURCE_FILE_CONFIG.ENCODING column)
-- 2. Update existing configurations with default encoding values
-- 3. CT_MRDS.FILE_MANAGER package specification 
-- 4. CT_MRDS.FILE_MANAGER package body
-- 5. ODS.FILE_MANAGER_ODS package specification
-- 6. ODS.FILE_MANAGER_ODS package body
-- 7. Track package versions
-- 8. Verify all tracked packages
-- 
-- NOTE: Testing is separate from installation.
--       For verification and testing, run separately:
--       test/05_MARS_1049_verify_encoding_functionality.sql
-- ===================================================================

PROMPT ===============================================
PROMPT MARS-1049: CSV Encoding Support - Full Installation
PROMPT ===============================================

-- Check prerequisites
PROMPT Checking prerequisites...
SELECT USER AS CURRENT_USER FROM DUAL;
SELECT COUNT(*) AS TABLE_COUNT FROM ALL_TABLES WHERE OWNER = 'CT_MRDS' AND TABLE_NAME = 'A_SOURCE_FILE_CONFIG';

PROMPT
PROMPT Step 0: Granting permissions for cross-schema hash tracking...
PROMPT ===============================================
@@00_MARS_1049_grant_permissions.sql

PROMPT
PROMPT Step 1: Installing database structure changes...
PROMPT ===============================================
@@01_MARS_1049_install_CT_MRDS_ADD_ENCODING_COLUMN.sql

PROMPT
PROMPT Step 2: Updating existing configurations with encoding values...
PROMPT ===============================================
@@02_MARS_1049_update_CT_MRDS_ENCODING_COLUMN.sql

PROMPT
PROMPT Step 3: Installing CT_MRDS.FILE_MANAGER specification...
PROMPT ===============================================
@@new_version/FILE_MANAGER.pkg

PROMPT
PROMPT Step 4: Installing CT_MRDS.FILE_MANAGER package body...
PROMPT ===============================================
@@new_version/FILE_MANAGER.pkb

PROMPT
PROMPT Step 5: Installing ODS.FILE_MANAGER_ODS specification...
PROMPT ===============================================
@@new_version/FILE_MANAGER_ODS.pkg

PROMPT
PROMPT Step 6: Installing ODS.FILE_MANAGER_ODS package body...
PROMPT ===============================================
@@new_version/FILE_MANAGER_ODS.pkb

PROMPT
PROMPT Step 7: Tracking package versions...
PROMPT ===============================================
@@track_package_versions.sql

PROMPT
PROMPT Step 8: Verifying all tracked packages...
PROMPT ===============================================
@@verify_packages_version.sql

PROMPT
PROMPT ===============================================
PROMPT MARS-1049: Installation Complete!
PROMPT ===============================================
PROMPT
PROMPT Summary of Changes:
PROMPT - A_SOURCE_FILE_CONFIG: Added ENCODING VARCHAR2(50) column for CSV character set specification
PROMPT - A_SOURCE_FILE_CONFIG: Updated existing configurations with default encoding values (UTF8 default, WE8MSWIN1252 for CSDB)
PROMPT - CT_MRDS.FILE_MANAGER v3.2.1: Added pEncoding parameter to ADD_SOURCE_FILE_CONFIG and CREATE_EXTERNAL_TABLE
PROMPT - ODS.FILE_MANAGER_ODS v2.1.0: Updated wrapper package with pEncoding parameter delegation
PROMPT
PROMPT Installation completed successfully!
PROMPT

spool off

exit