<?php
/**
 * Demo Oracle — DemoOracleDashExportsRepository.
 *
 * Portage de modules/oracle/lib/OracleDashExportsRepository.php : requetes de LECTURE
 * uniquement.
 *
 * Convention conservee du module complet : classe sans etat, $conn (ressource
 * OCI8) en premier argument, retour systematique en array — vide en cas
 * d'echec OCI, jamais false, jamais d'exception. La page appelante affiche
 * l'erreur globale.
 */

if (class_exists('DemoOracleDashExportsRepository')) return;

class DemoOracleDashExportsRepository
{
    public static function fetchDataPumpJobs($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT owner_name, job_name, operation, job_mode, state,
                    degree, attached_sessions, datapump_sessions
             FROM DBA_DATAPUMP_JOBS
             ORDER BY state, job_name");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
            oci_free_statement($stm);
        }
        return $rows;
    }

    public static function fetchDataPumpLongOps($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT inst_id, sid, serial# AS serial_num, username,
                    opname, target, sofar, totalwork, units,
                    TO_CHAR(start_time,       'YYYY-MM-DD HH24:MI:SS') AS start_time,
                    TO_CHAR(last_update_time, 'YYYY-MM-DD HH24:MI:SS') AS last_update_time,
                    elapsed_seconds, time_remaining
             FROM GV\$SESSION_LONGOPS
             WHERE UPPER(opname) LIKE '%DUMP%'
                OR UPPER(opname) LIKE 'EXPORT%'
                OR UPPER(opname) LIKE 'IMPORT%'
             ORDER BY start_time DESC
             FETCH FIRST 30 ROWS ONLY");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
            oci_free_statement($stm);
        }
        return $rows;
    }

    public static function fetchDirectories($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT owner, directory_name, directory_path
             FROM DBA_DIRECTORIES
             ORDER BY directory_name");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
            oci_free_statement($stm);
        }
        return $rows;
    }

    public static function fetchExternalTables($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT owner, table_name, type_owner, type_name,
                    default_directory_owner, default_directory_name,
                    access_type, reject_limit
             FROM DBA_EXTERNAL_TABLES
             ORDER BY owner, table_name");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
            oci_free_statement($stm);
        }
        return $rows;
    }

    public static function fetchExternalLocations($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT owner, table_name, location, directory_owner, directory_name
             FROM DBA_EXTERNAL_LOCATIONS
             ORDER BY owner, table_name, location");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
            oci_free_statement($stm);
        }
        return $rows;
    }
}
