From 0a269db9cf823de737049967a0c03ca82b9abce1 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Fri, 26 May 2006 19:23:09 +0000
Subject: [PATCH] Add table_name and table_schema to plpython trigger data,
 plus docs and regression test.

---
 doc/src/sgml/plpython.sgml                    |  5 +-
 .../plpython/expected/plpython_function.out   | 55 +++++++++++++++++++
 src/pl/plpython/plpython.c                    | 19 ++++++-
 src/pl/plpython/sql/plpython_function.sql     | 31 +++++++++++
 4 files changed, 107 insertions(+), 3 deletions(-)

diff --git a/doc/src/sgml/plpython.sgml b/doc/src/sgml/plpython.sgml
index c3f27e844bf..ccccf66155d 100644
--- a/doc/src/sgml/plpython.sgml
+++ b/doc/src/sgml/plpython.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.29 2005/05/20 01:52:25 neilc Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.30 2006/05/26 19:23:09 adunstan Exp $ -->
 
 <chapter id="plpython">
  <title>PL/Python - Python Procedural Language</title>
@@ -123,6 +123,9 @@ def __plpython_procedure_myfunc_23456():
    <literal>AFTER</>, and <literal>UNKNOWN</>.
    <literal>TD["level"]</> contains one of <literal>ROW</>,
    <literal>STATEMENT</>, and <literal>UNKNOWN</>.
+   <literal>TD["name"]</> contains the trigger name,
+   <literal>TD["table_name"]</> contains the name of the table on which the trigger occurred,
+   <literal>TD["table_schema"]</> contains the schema of the table on which the trigger occurred,
    <literal>TD["name"]</> contains the trigger name, and
    <literal>TD["relid"]</> contains the OID of the table on
    which the trigger occurred.  If the <command>CREATE TRIGGER</> command
diff --git a/src/pl/plpython/expected/plpython_function.out b/src/pl/plpython/expected/plpython_function.out
index a536ae7fe9a..d8646b675a2 100644
--- a/src/pl/plpython/expected/plpython_function.out
+++ b/src/pl/plpython/expected/plpython_function.out
@@ -115,6 +115,61 @@ 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');
+-- 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"
+
+for key in sorted(TD.keys()):
+	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');
+NOTICE:  ("TD[args] => ['23', 'skidoo']",)
+NOTICE:  ('TD[event] => INSERT',)
+NOTICE:  ('TD[level] => ROW',)
+NOTICE:  ('TD[name] => show_trigger_data_trig',)
+NOTICE:  ("TD[new] => {'i': 1, 'v': 'insert'}",)
+NOTICE:  ('TD[old] => None',)
+NOTICE:  ('TD[relid] => bogus:12345',)
+NOTICE:  ('TD[table_name] => trigger_test',)
+NOTICE:  ('TD[table_schema] => public',)
+NOTICE:  ('TD[when] => BEFORE',)
+update trigger_test set v = 'update' where i = 1;
+NOTICE:  ("TD[args] => ['23', 'skidoo']",)
+NOTICE:  ('TD[event] => UPDATE',)
+NOTICE:  ('TD[level] => ROW',)
+NOTICE:  ('TD[name] => show_trigger_data_trig',)
+NOTICE:  ("TD[new] => {'i': 1, 'v': 'update'}",)
+NOTICE:  ("TD[old] => {'i': 1, 'v': 'insert'}",)
+NOTICE:  ('TD[relid] => bogus:12345',)
+NOTICE:  ('TD[table_name] => trigger_test',)
+NOTICE:  ('TD[table_schema] => public',)
+NOTICE:  ('TD[when] => BEFORE',)
+delete from trigger_test;
+NOTICE:  ("TD[args] => ['23', 'skidoo']",)
+NOTICE:  ('TD[event] => DELETE',)
+NOTICE:  ('TD[level] => ROW',)
+NOTICE:  ('TD[name] => show_trigger_data_trig',)
+NOTICE:  ('TD[new] => None',)
+NOTICE:  ("TD[old] => {'i': 1, 'v': 'update'}",)
+NOTICE:  ('TD[relid] => bogus:12345',)
+NOTICE:  ('TD[table_name] => trigger_test',)
+NOTICE:  ('TD[table_schema] => public',)
+NOTICE:  ('TD[when] => BEFORE',)
+      
+DROP TRIGGER show_trigger_data_trig on trigger_test;
+      
+DROP FUNCTION trigger_data();
 -- nested calls
 --
 CREATE FUNCTION nested_call_one(text) RETURNS text
diff --git a/src/pl/plpython/plpython.c b/src/pl/plpython/plpython.c
index dc36c9f8879..7ac3f89c146 100644
--- a/src/pl/plpython/plpython.c
+++ b/src/pl/plpython/plpython.c
@@ -1,7 +1,7 @@
 /**********************************************************************
  * plpython.c - python as a procedural language for PostgreSQL
  *
- *	$PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.79 2006/04/27 14:18:07 momjian Exp $
+ *	$PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.80 2006/05/26 19:23:09 adunstan Exp $
  *
  *********************************************************************
  */
@@ -560,7 +560,9 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *
 			   *pltevent,
 			   *pltwhen,
 			   *pltlevel,
-			   *pltrelid;
+		       *pltrelid,
+		       *plttablename,
+		       *plttableschema;
 	PyObject   *pltargs,
 			   *pytnew,
 			   *pytold;
@@ -584,6 +586,19 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *
 		Py_DECREF(pltrelid);
 		pfree(stroid);
 
+		stroid = SPI_getrelname(tdata->tg_relation);
+		plttablename = PyString_FromString(stroid);
+		PyDict_SetItemString(pltdata, "table_name", plttablename);
+		Py_DECREF(plttablename);
+		pfree(stroid);
+		
+		stroid = SPI_getnspname(tdata->tg_relation);
+		plttableschema = PyString_FromString(stroid);
+		PyDict_SetItemString(pltdata, "table_schema", plttableschema);
+		Py_DECREF(plttableschema);
+		pfree(stroid);
+		
+
 		if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
 			pltwhen = PyString_FromString("BEFORE");
 		else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
diff --git a/src/pl/plpython/sql/plpython_function.sql b/src/pl/plpython/sql/plpython_function.sql
index c5920685d96..73b1a654055 100644
--- a/src/pl/plpython/sql/plpython_function.sql
+++ b/src/pl/plpython/sql/plpython_function.sql
@@ -140,6 +140,37 @@ CREATE TRIGGER users_delete_trig BEFORE DELETE ON users FOR EACH ROW
 	EXECUTE PROCEDURE users_delete ('willem');
 
 
+
+-- 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"
+
+for key in sorted(TD.keys()):
+	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();
+
 -- nested calls
 --
 
-- 
GitLab