PDO::pgsqlLOBCreate

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

PDO::pgsqlLOBCreate새 라지 오브젝트를 생성합니다

설명

string PDO::pgsqlLOBCreate ( void )

PDO::pgsqlLOBCreate()는 라지 오브젝트를 만들고 해당 오브젝트의 OID를 반환합니다. PDO::pgsqlLOBOpen()으로 해당 오브젝트에 스트림을 열어서 읽고 쓰기를 할 수 있습니다. OID는 OID 형식의 컬럼에 저장하여, 열이 마음대로 늘어나는 일이 없도록 참조하여 사용할 수 있습니다. 라지 오브젝트는 PDO::pgsqlLOBUnlink()를 호출하여 제거할 때 까지 계속해서 데이터베이스에 존재합니다.

라지 오브젝트는 2GB 크기까지 사용할 수 있지만, 사용하기 편하지 않습니다; PDO::pgsqlLOBUnlink()를 제대로 호출한 후 데이터베이스에서 해당 OID를 참조하는 줄을 삭제해야 합니다. 추가로, 라지 오브젝트는 접근 제어가 없습니다. bytea 컬럼형으로 대체할 수 있습니다; PostgreSQL 최근 버전에선 bytea 컬럼을 1GB까지 사용할 수 있고, 스토리지를 적절한 행 크기로 관리합니다.

Note: 이 함수는 트랙젝션 안에서 호출해야 합니다.

인수

PDO::pgsqlLOBCreate()는 인수가 없습니다.

반환값

성공시엔 새로 만들어진 라지 오브젝트의 OID, 실패시엔 FALSE를 반환합니다.

예제

Example #1 PDO::pgsqlLOBCreate() 예제

이 예제는 새 라지 오브젝트를 만들고 파일 내용을 복사해 넣습니다. 그리고 OID는 테이블에 저장합니다.

<?php
$db 
= new PDO('pgsql:dbname=test host=localhost'$user$pass);
$db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$oid $db->pgsqlLOBCreate();
$stream $db->pgsqlLOBOpen($oid'w');
$local fopen($filename'rb');
stream_copy_to_stream($local$stream);
$local null;
$stream null;
$stmt $db->prepare("INSERT INTO BLOBS (ident, oid) VALUES (?, ?)");
$stmt->execute(array($some_id$oid));
$db->commit();
?>

참고

add a note 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