2010-09-21 04:08:53 +08:00
|
|
|
/* contrib/lo/lo_test.sql */
|
2007-11-13 12:24:29 +08:00
|
|
|
|
2007-11-11 11:25:35 +08:00
|
|
|
-- Adjust this setting to control where the objects get created.
|
|
|
|
SET search_path = public;
|
|
|
|
|
2000-06-16 03:05:22 +08:00
|
|
|
--
|
|
|
|
-- This runs some common tests against the type
|
|
|
|
--
|
|
|
|
-- It's used just for development
|
|
|
|
--
|
2005-06-23 08:06:37 +08:00
|
|
|
-- XXX would be nice to turn this into a proper regression test
|
|
|
|
--
|
|
|
|
|
|
|
|
-- Check what is in pg_largeobject
|
2009-12-14 08:39:11 +08:00
|
|
|
SELECT count(oid) FROM pg_largeobject_metadata;
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- ignore any errors here - simply drop the table if it already exists
|
2002-10-19 02:41:22 +08:00
|
|
|
DROP TABLE a;
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- create the test table
|
2002-10-19 02:41:22 +08:00
|
|
|
CREATE TABLE a (fname name,image lo);
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- insert a null object
|
2005-06-23 08:06:37 +08:00
|
|
|
INSERT INTO a VALUES ('empty');
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- insert a large object based on a file
|
2005-06-23 08:06:37 +08:00
|
|
|
INSERT INTO a VALUES ('/etc/group', lo_import('/etc/group')::lo);
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- now select the table
|
2002-10-19 02:41:22 +08:00
|
|
|
SELECT * FROM a;
|
2000-06-16 03:05:22 +08:00
|
|
|
|
2005-06-23 08:06:37 +08:00
|
|
|
-- check that coercion to plain oid works
|
2002-10-19 02:41:22 +08:00
|
|
|
SELECT *,image::oid from a;
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- now test the trigger
|
2002-10-19 02:41:22 +08:00
|
|
|
CREATE TRIGGER t_a
|
|
|
|
BEFORE UPDATE OR DELETE ON a
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE lo_manage(image);
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- insert
|
2005-06-23 08:06:37 +08:00
|
|
|
INSERT INTO a VALUES ('aa', lo_import('/etc/hosts'));
|
2002-10-19 02:41:22 +08:00
|
|
|
SELECT * FROM a
|
|
|
|
WHERE fname LIKE 'aa%';
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- update
|
2002-10-19 02:41:22 +08:00
|
|
|
UPDATE a SET image=lo_import('/etc/group')::lo
|
|
|
|
WHERE fname='aa';
|
|
|
|
SELECT * FROM a
|
|
|
|
WHERE fname LIKE 'aa%';
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- update the 'empty' row which should be null
|
2005-06-23 08:06:37 +08:00
|
|
|
UPDATE a SET image=lo_import('/etc/hosts')
|
2002-10-19 02:41:22 +08:00
|
|
|
WHERE fname='empty';
|
|
|
|
SELECT * FROM a
|
|
|
|
WHERE fname LIKE 'empty%';
|
|
|
|
UPDATE a SET image=null
|
|
|
|
WHERE fname='empty';
|
|
|
|
SELECT * FROM a
|
|
|
|
WHERE fname LIKE 'empty%';
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- delete the entry
|
2002-10-19 02:41:22 +08:00
|
|
|
DELETE FROM a
|
|
|
|
WHERE fname='aa';
|
|
|
|
SELECT * FROM a
|
|
|
|
WHERE fname LIKE 'aa%';
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- This deletes the table contents. Note, if you comment this out, and
|
|
|
|
-- expect the drop table to remove the objects, think again. The trigger
|
2005-06-23 08:06:37 +08:00
|
|
|
-- doesn't get fired by drop table.
|
2002-10-19 02:41:22 +08:00
|
|
|
DELETE FROM a;
|
2000-06-16 03:05:22 +08:00
|
|
|
|
|
|
|
-- finally drop the table
|
2002-10-19 02:41:22 +08:00
|
|
|
DROP TABLE a;
|
2000-06-16 03:05:22 +08:00
|
|
|
|
2005-06-23 08:06:37 +08:00
|
|
|
-- Check what is in pg_largeobject ... if different from original, trouble
|
2009-12-14 08:39:11 +08:00
|
|
|
SELECT count(oid) FROM pg_largeobject_metadata;
|
2005-06-23 08:06:37 +08:00
|
|
|
|
2000-06-16 03:05:22 +08:00
|
|
|
-- end of tests
|