PHP 8.1.28 Released!

PDO::pgsqlLOBCreate

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL pdo_pgsql >= 1.0.2)

PDO::pgsqlLOBCreateCrear un nuevo objeto grande

Descripción

public PDO::pgsqlLOBCreate(): string

PDO::pgsqlLOBCreate() crea un objeto grande y devuelve el OID del mismo. Se podrá entonces abrir un flujo sobre el objeto usando PDO::pgsqlLOBOpen() para leer o escribir datos en él. El OID se puede almacenar en columnas de tipo OID y se puede usar para hacer referencia al objeto grande, sin causar que la fila se vuelva arbitrariamente grande. El objeto grande continuará estando en la base de datos hasta que sea eliminado mediante una llamada a PDO::pgsqlLOBUnlink().

Los objetos grandes pueden tener un tamaño de hasta 2GB, pero son incómodos de usar; es necesario asegurarse de que se invoca a PDO::pgsqlLOBUnlink() antes de borrar la última fila que hacía referencia a su OID desde la base de datos. Además, los objetos grandes no tienen controles de acceso. Como alternativa, se ha de intentar con el tipo de columna BYTEA; las versiones recientes de PostgreSQL permiten un tamaño para las columnas BYTEA de hasta 1GB y administran de forma transparente el almacenamiento para un tamaño de fila óptimo.

Nota: Esta función debe ser llamada dentro de una transacción.

Parámetros

PDO::pgsqlLOBCreate() no tiene parámetros.

Valores devueltos

Devuelve el OID del objeto grande recién creado en caso de éxito, o false en caso de error.

Ejemplos

Ejemplo #1 Un ejemplo de PDO::pgsqlLOBCreate()

Este ejemplo crea un nuevo objeto grande y copia el contenido de un fichero en él. El OID es entonces almacenado en una tabla.

<?php
$bd
= new PDO('pgsql:dbname=test host=localhost', $usuario, $contraseña);
$bd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$bd->beginTransaction();
$oid = $bd->pgsqlLOBCreate();
$flujo = $bd->pgsqlLOBOpen($oid, 'w');
$local = fopen($filename, 'rb');
stream_copy_to_stream($local, $flujo);
$local = null;
$flujo = null;
$sentencia = $bd->prepare("INSERT INTO BLOBS (ident, oid) VALUES (?, ?)");
$sentencia->execute(array($algún_id, $oid));
$bd->commit();
?>

Ver también

add a note

User Contributed Notes 2 notes

up
0
Hayley Watson
5 years ago
If you're not plausibly going to be storing more than 1GB of binary data in a single field, you might as well use the normal bytea type instead of LOBbing it.

They won't bloat the table as PostgreSQL would store the larger byte streams outside the table anyway (as Large Object storage does, only transparently) - including compressing them if it helps - while retaining all the binary string functions and operators.
up
0
mauroi at digbang dot com
18 years ago
IMHO, there's a better way to handle the deletion of lob objects than the suggested here. The programmer can easily forget to unlink the lob. With the following trigger, no programmer actions are required.
By the way, one problem with bytea fields is that when you query the database, if you ask for that field, the data is actually retrieved. When you query for and oid, only the oid is retrieved and then you can open the lob whenever you want (if it's required).

CREATE OR REPLACE FUNCTION oidtable_after_update_delete()
RETURNS "trigger" AS
$BODY$
BEGIN
IF (TG_OP = 'UPDATE') THEN
IF (OLD.oidfield = NEW.oidfield) OR (OLD.oidfield IS NULL) THEN
RETURN NEW;
END IF;
END IF;
IF (EXISTS (SELECT 1 FROM pg_largeobject WHERE loid = OLD.oidfield)) THEN
PERFORM LO_UNLINK (OLD.oidfield);
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

CREATE TRIGGER oidtable_after_update_delete
AFTER UPDATE OR DELETE
ON oidtable
FOR EACH ROW
EXECUTE PROCEDURE oidtable_after_update_delete();
To Top