Commit 0a269db9 authored by Andrew Dunstan's avatar Andrew Dunstan

parent 777f72cd
<!-- $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"> <chapter id="plpython">
<title>PL/Python - Python Procedural Language</title> <title>PL/Python - Python Procedural Language</title>
...@@ -123,6 +123,9 @@ def __plpython_procedure_myfunc_23456(): ...@@ -123,6 +123,9 @@ def __plpython_procedure_myfunc_23456():
<literal>AFTER</>, and <literal>UNKNOWN</>. <literal>AFTER</>, and <literal>UNKNOWN</>.
<literal>TD["level"]</> contains one of <literal>ROW</>, <literal>TD["level"]</> contains one of <literal>ROW</>,
<literal>STATEMENT</>, and <literal>UNKNOWN</>. <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["name"]</> contains the trigger name, and
<literal>TD["relid"]</> contains the OID of the table on <literal>TD["relid"]</> contains the OID of the table on
which the trigger occurred. If the <command>CREATE TRIGGER</> command which the trigger occurred. If the <command>CREATE TRIGGER</> command
......
...@@ -115,6 +115,61 @@ CREATE TRIGGER users_update_trig BEFORE UPDATE ON users FOR EACH ROW ...@@ -115,6 +115,61 @@ CREATE TRIGGER users_update_trig BEFORE UPDATE ON users FOR EACH ROW
EXECUTE PROCEDURE users_update ('willem'); EXECUTE PROCEDURE users_update ('willem');
CREATE TRIGGER users_delete_trig BEFORE DELETE ON users FOR EACH ROW CREATE TRIGGER users_delete_trig BEFORE DELETE ON users FOR EACH ROW
EXECUTE PROCEDURE users_delete ('willem'); 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 -- nested calls
-- --
CREATE FUNCTION nested_call_one(text) RETURNS text CREATE FUNCTION nested_call_one(text) RETURNS text
......
/********************************************************************** /**********************************************************************
* plpython.c - python as a procedural language for PostgreSQL * 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 * ...@@ -560,7 +560,9 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *
*pltevent, *pltevent,
*pltwhen, *pltwhen,
*pltlevel, *pltlevel,
*pltrelid; *pltrelid,
*plttablename,
*plttableschema;
PyObject *pltargs, PyObject *pltargs,
*pytnew, *pytnew,
*pytold; *pytold;
...@@ -584,6 +586,19 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple * ...@@ -584,6 +586,19 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *
Py_DECREF(pltrelid); Py_DECREF(pltrelid);
pfree(stroid); 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)) if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
pltwhen = PyString_FromString("BEFORE"); pltwhen = PyString_FromString("BEFORE");
else if (TRIGGER_FIRED_AFTER(tdata->tg_event)) else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
......
...@@ -140,6 +140,37 @@ CREATE TRIGGER users_delete_trig BEFORE DELETE ON users FOR EACH ROW ...@@ -140,6 +140,37 @@ CREATE TRIGGER users_delete_trig BEFORE DELETE ON users FOR EACH ROW
EXECUTE PROCEDURE users_delete ('willem'); 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 -- nested calls
-- --
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment