-<!-- $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>
<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
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
/**********************************************************************
* 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 $
*
*********************************************************************
*/
*pltevent,
*pltwhen,
*pltlevel,
- *pltrelid;
+ *pltrelid,
+ *plttablename,
+ *plttableschema;
PyObject *pltargs,
*pytnew,
*pytold;
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))
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
--