<?php

class DbBuffer
{

    var $name;

    var $data;

    var $entete;

    var $dettbl;

    var $connection;

    var $connectype;

    var $connecteur;

    var $base;

    function __construct(& $name, $connection, $connectype, & $result, $base)
    {
        global $connecteur;
        $this->connection = & $connection;
        $this->connectype = & $connectype;
        $this->name = & $name;
        $this->connecteur = & $connecteur;
        $this->base = & $base;
        
        if (! is_null($result))
            $this->LoadRecord($result);
    }

    function LoadRecord(& $result)
    {
        switch ($this->connecteur->getType($this->base)) {
            case DB_MSSQL:
                if (@ sqlsrv_num_fields($result)) {
                    $compte = sqlsrv_num_fields($result);
                    if ($compte != 0) {
                        $fieldMetadata = sqlsrv_field_metadata($result);
                        foreach ($fieldMetadata as & $fieldMetadata)
                            $zone[$fieldMetadata['Name']] = $fieldMetadata;
                        
                        foreach ($fieldMetadata as $name => & $value)
                            $this->dettbl[$name] = new FielInfo($name, $value);
                        
                        while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
                       
                            foreach ($row as $key => & $element)
                                // Traitement des données en fonction des differentes bases
                                if (($zone[$key]['Size'] == 1 && $zone[$key]['Type'] == - 2))
                                    if ($element == 'T')
                                        $element = '1';
                                    else
                                        $element = '0';
                            
                            $this->data[] =  $row;
                        }
                    }
                    sqlsrv_cancel($result);
                }
                break;
            case DB_ORA:
                if (@ oci_num_fields($result)) {
                    for ($i = 1; $i <= oci_num_fields($result); $i ++)
                        $this->dettbl[oci_field_name($result, $i)] = new FielInfo(oci_field_name($result, $i), oci_field_type($result, $i));
                    
                    WHILE ($row = oci_fetch_assoc($result))
                        // Ne pas mettre d'esperluette : le driver Oracle perd les références
                        $this->data[] = $row;
                    
                    oci_free_statement($result);
                }
                break;
        }
    }
}