| 1 |
/* contrib/lo/lo_test.sql */ |
| 2 |
|
| 3 |
-- Adjust this setting to control where the objects get created. |
| 4 |
SET search_path = public; |
| 5 |
|
| 6 |
-- |
| 7 |
-- This runs some common tests against the type |
| 8 |
-- |
| 9 |
-- It's used just for development |
| 10 |
-- |
| 11 |
-- XXX would be nice to turn this into a proper regression test |
| 12 |
-- |
| 13 |
|
| 14 |
-- Check what is in pg_largeobject |
| 15 |
SELECT count(oid) FROM pg_largeobject_metadata; |
| 16 |
|
| 17 |
-- ignore any errors here - simply drop the table if it already exists |
| 18 |
DROP TABLE a; |
| 19 |
|
| 20 |
-- create the test table |
| 21 |
CREATE TABLE a (fname name,image lo); |
| 22 |
|
| 23 |
-- insert a null object |
| 24 |
INSERT INTO a VALUES ('empty'); |
| 25 |
|
| 26 |
-- insert a large object based on a file |
| 27 |
INSERT INTO a VALUES ('/etc/group', lo_import('/etc/group')::lo); |
| 28 |
|
| 29 |
-- now select the table |
| 30 |
SELECT * FROM a; |
| 31 |
|
| 32 |
-- check that coercion to plain oid works |
| 33 |
SELECT *,image::oid from a; |
| 34 |
|
| 35 |
-- now test the trigger |
| 36 |
CREATE TRIGGER t_a |
| 37 |
BEFORE UPDATE OR DELETE ON a |
| 38 |
FOR EACH ROW |
| 39 |
EXECUTE PROCEDURE lo_manage(image); |
| 40 |
|
| 41 |
-- insert |
| 42 |
INSERT INTO a VALUES ('aa', lo_import('/etc/hosts')); |
| 43 |
SELECT * FROM a |
| 44 |
WHERE fname LIKE 'aa%'; |
| 45 |
|
| 46 |
-- update |
| 47 |
UPDATE a SET image=lo_import('/etc/group')::lo |
| 48 |
WHERE fname='aa'; |
| 49 |
SELECT * FROM a |
| 50 |
WHERE fname LIKE 'aa%'; |
| 51 |
|
| 52 |
-- update the 'empty' row which should be null |
| 53 |
UPDATE a SET image=lo_import('/etc/hosts') |
| 54 |
WHERE fname='empty'; |
| 55 |
SELECT * FROM a |
| 56 |
WHERE fname LIKE 'empty%'; |
| 57 |
UPDATE a SET image=null |
| 58 |
WHERE fname='empty'; |
| 59 |
SELECT * FROM a |
| 60 |
WHERE fname LIKE 'empty%'; |
| 61 |
|
| 62 |
-- delete the entry |
| 63 |
DELETE FROM a |
| 64 |
WHERE fname='aa'; |
| 65 |
SELECT * FROM a |
| 66 |
WHERE fname LIKE 'aa%'; |
| 67 |
|
| 68 |
-- This deletes the table contents. Note, if you comment this out, and |
| 69 |
-- expect the drop table to remove the objects, think again. The trigger |
| 70 |
-- doesn't get fired by drop table. |
| 71 |
DELETE FROM a; |
| 72 |
|
| 73 |
-- finally drop the table |
| 74 |
DROP TABLE a; |
| 75 |
|
| 76 |
-- Check what is in pg_largeobject ... if different from original, trouble |
| 77 |
SELECT count(oid) FROM pg_largeobject_metadata; |
| 78 |
|
| 79 |
-- end of tests |