dbTalk Databases Forums  

Blob with OIDs and trigger

comp.databases.postgresql comp.databases.postgresql


Discuss Blob with OIDs and trigger in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Blob with OIDs and trigger - 11-19-2009 , 04:26 AM






Philipp Kraus wrote:
Quote:
I have create a table with this structur

CREATE TABLE import
(
id1 integer NOT NULL,
id2 integer NOT NULL,
dataset oid NOT NULL,
CONSTRAINT import_pk PRIMARY KEY (id1, id2),
CONSTRAINT import_fk FOREIGN KEY (id1, id1)
REFERENCES ... MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (OIDS=FALSE);

I insert my blobs with JDBC and I can read / write them without problems.

Must I delete explicite the blob, if I delete a row on my table "import" ?
If yes, how? I would like to do this with a trigger on my table.
Yes; here is an example:

test=> CREATE TABLE lo_test (id integer PRIMARY KEY, lob oid);

test=> CREATE OR REPLACE FUNCTION lo_trigger() RETURNS trigger
LANGUAGE plpgsql AS
$$BEGIN
IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
IF OLD.lob IS NOT NULL THEN
IF TG_OP = 'DELETE' THEN
PERFORM lo_unlink(OLD.lob);
ELSEIF (NEW.lob IS NULL OR OLD.lob != NEW.lob) THEN
PERFORM lo_unlink(OLD.lob);
END IF;
END IF;
END IF;
IF TG_OP = 'UPDATE' OR TG_OP = 'INSERT' THEN
RETURN NEW;
ELSE
RETURN OLD;
END IF;
END;$$;

test=> CREATE TRIGGER lo_trigger AFTER UPDATE OR DELETE ON lo_test
FOR EACH ROW EXECUTE PROCEDURE lo_trigger();

test=> \lo_import schiach.jpg
lo_import 20337

test=> INSERT INTO lo_test (id, lob) VALUES (1, 20337);
INSERT 0 1

test=> \lo_list
Large objects
ID | Description
-------+-------------
20337 |
(1 row)

test=> DELETE FROM lo_test WHERE id=1;
DELETE 1

test=> \lo_list
Large objects
ID | Description
----+-------------
(0 rows)

Yours,
Laurenz Albe

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.