From 9d9848668fd868a4e51f3a3f22c2807ff0e46582 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Wed, 12 Aug 2009 16:37:26 +0000
Subject: Split the plpython regression test into test cases arranged by topic,
 instead of the previous monolithic setup-create-run sequence, that was
 apparently inherited from a previous test infrastructure, but makes working
 with the tests and adding new ones weird.

---
 src/pl/plpython/sql/plpython_trigger.sql | 95 ++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
 create mode 100644 src/pl/plpython/sql/plpython_trigger.sql

(limited to 'src/pl/plpython/sql/plpython_trigger.sql')

diff --git a/src/pl/plpython/sql/plpython_trigger.sql b/src/pl/plpython/sql/plpython_trigger.sql
new file mode 100644
index 0000000000..b042ae926e
--- /dev/null
+++ b/src/pl/plpython/sql/plpython_trigger.sql
@@ -0,0 +1,95 @@
+-- these triggers are dedicated to HPHC of RI who
+-- decided that my kid's name was william not willem, and
+-- vigorously resisted all efforts at correction.  they have
+-- since gone bankrupt...
+
+CREATE FUNCTION users_insert() returns trigger
+	AS
+'if TD["new"]["fname"] == None or TD["new"]["lname"] == None:
+	return "SKIP"
+if TD["new"]["username"] == None:
+	TD["new"]["username"] = TD["new"]["fname"][:1] + "_" + TD["new"]["lname"]
+	rv = "MODIFY"
+else:
+	rv = None
+if TD["new"]["fname"] == "william":
+	TD["new"]["fname"] = TD["args"][0]
+	rv = "MODIFY"
+return rv'
+	LANGUAGE plpythonu;
+
+
+CREATE FUNCTION users_update() returns trigger
+	AS
+'if TD["event"] == "UPDATE":
+	if TD["old"]["fname"] != TD["new"]["fname"] and TD["old"]["fname"] == TD["args"][0]:
+		return "SKIP"
+return None'
+	LANGUAGE plpythonu;
+
+
+CREATE FUNCTION users_delete() RETURNS trigger
+	AS
+'if TD["old"]["fname"] == TD["args"][0]:
+	return "SKIP"
+return None'
+	LANGUAGE plpythonu;
+
+
+CREATE TRIGGER users_insert_trig BEFORE INSERT ON users FOR EACH ROW
+	EXECUTE PROCEDURE users_insert ('willem');
+
+CREATE TRIGGER users_update_trig BEFORE UPDATE ON users FOR EACH ROW
+	EXECUTE PROCEDURE users_update ('willem');
+
+CREATE TRIGGER users_delete_trig BEFORE DELETE ON users FOR EACH ROW
+	EXECUTE PROCEDURE users_delete ('willem');
+
+
+-- quick peek at the table
+--
+SELECT * FROM users;
+
+-- should fail
+--
+UPDATE users SET fname = 'william' WHERE fname = 'willem';
+
+-- should modify william to willem and create username
+--
+INSERT INTO users (fname, lname) VALUES ('william', 'smith');
+INSERT INTO users (fname, lname, username) VALUES ('charles', 'darwin', 'beagle');
+
+SELECT * FROM users;
+
+
+-- dump trigger data
+
+CREATE TABLE trigger_test
+	(i int, v text );
+
+CREATE FUNCTION trigger_data() returns trigger language plpythonu as $$
+
+if TD.has_key('relid'):
+	TD['relid'] = "bogus:12345"
+
+skeys = TD.keys()
+skeys.sort()
+for key in skeys:
+	val = TD[key]
+	plpy.notice("TD[" + key + "] => " + str(val))
+
+return None  
+
+$$;
+
+CREATE TRIGGER show_trigger_data_trig 
+BEFORE INSERT OR UPDATE OR DELETE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+
+insert into trigger_test values(1,'insert');
+update trigger_test set v = 'update' where i = 1;
+delete from trigger_test;
+      
+DROP TRIGGER show_trigger_data_trig on trigger_test;
+      
+DROP FUNCTION trigger_data();
-- 
cgit v1.2.1

