<?php
class PostgresLOB extends MLOB
{
    function handle($object, $attribute, $value, $operation)
    {
        global $MIOLO;
        if ($operation == 'select')
        {
            $md5 = md5($value);
            $file = $MIOLO->getConf('home.reports') . '/' . $md5 . '.lob';
            $transaction = $this->conn->db->getTransaction();
            $transaction->begin();
            pg_lo_export($this->conn->id,$value,$file);
            $error = pg_last_error() == '' ? $transaction->commit() : $transaction->rollback();
            $object->$attribute = file_get_contents($file);
            unlink($file);
        }
        return "";
    }

    function lobload($value)
    {
        return $value;
    }

    function lobsave($value)
    {
        global $MIOLO;

        $md5 = md5($value);
        $file = $MIOLO->getConf('home.reports') . '/' . $md5 . '.lob';
        file_put_contents($file,$value);
        $transaction = $this->conn->db->getTransaction();
        $transaction->begin();
        $oid = pg_lo_import($this->conn->id,$file);
        $error = pg_last_error() == '' ? $transaction->commit() : $transaction->rollback();
        unlink($file);
        return $oid;
    }

}
?>