Author: cito
Date: Sun May 13 08:19:46 2012
New Revision: 443
Log:
Support the new payload parameter in getnotify().
Modified:
trunk/docs/changelog.txt
trunk/docs/pg.txt
trunk/module/pgmodule.c
trunk/module/test_pg.py
Modified: trunk/docs/changelog.txt
==============================================================================
--- trunk/docs/changelog.txt Sat May 12 18:18:55 2012 (r442)
+++ trunk/docs/changelog.txt Sun May 13 08:19:46 2012 (r443)
@@ -15,6 +15,7 @@
- Proper handling of floats with 'nan' or 'inf' values as input.
- Fixed the set_decimal() function.
- All DatabaseError instances now have a sqlstate attribute.
+- The getnotify() method can now also return payload strings.
- Open transactions are rolled back when pgdb connections are closed
(as suggested by Peter Harris).
- Connections and cursors can now be used with the "with" statement
Modified: trunk/docs/pg.txt
==============================================================================
--- trunk/docs/pg.txt Sat May 12 18:18:55 2012 (r442)
+++ trunk/docs/pg.txt Sun May 13 08:19:46 2012 (r443)
@@ -549,10 +549,11 @@
Description:
This methods try to get a notify from the server (from the SQL statement
NOTIFY). If the server returns no notify, the methods returns None.
- Otherwise, it returns a tuple (couple) `(relname, pid)`, where `relname`
- is the name of the notify and `pid` the process id of the connection that
- triggered the notify. Remember to do a listen query first otherwise
- getnotify() will always return `None`.
+ Otherwise, it returns a tuple (triplet) `(relname, pid, extra)`, where
+ `relname` is the name of the notify, `pid` is the process id of the
+ connection that triggered the notify, and `extra` is a payload string
+ that has been sent with the notification. Remember to do a listen query
+ first, otherwise getnotify() will always return `None`.
inserttable - insert a list into a table
----------------------------------------
Modified: trunk/module/pgmodule.c
==============================================================================
--- trunk/module/pgmodule.c Sat May 12 18:18:55 2012 (r442)
+++ trunk/module/pgmodule.c Sun May 13 08:19:46 2012 (r443)
@@ -2376,9 +2376,12 @@
PyObject *notify_result,
*temp;
- if (!(notify_result = PyTuple_New(2)) ||
- !(temp = PyString_FromString(notify->relname)))
+ if (!(notify_result = PyTuple_New(3)))
+ return NULL;
+
+ if (!(temp = PyString_FromString(notify->relname)))
{
+ Py_DECREF(notify_result);
return NULL;
}
@@ -2391,7 +2394,18 @@
}
PyTuple_SET_ITEM(notify_result, 1, temp);
+
+ /* extra exists even in old versions that did not support it */
+ if (!(temp = PyString_FromString(notify->extra)))
+ {
+ Py_DECREF(notify_result);
+ return NULL;
+ }
+
+ PyTuple_SET_ITEM(notify_result, 2, temp);
+
PQfreemem(notify);
+
return notify_result;
}
}
Modified: trunk/module/test_pg.py
==============================================================================
--- trunk/module/test_pg.py Sat May 12 18:18:55 2012 (r442)
+++ trunk/module/test_pg.py Sun May 13 08:19:46 2012 (r443)
@@ -615,13 +615,30 @@
self.c.query('listen test_notify')
try:
self.assert_(self.c.getnotify() is None)
- self.c.query('notify test_notify')
+ self.c.query("notify test_notify")
r = self.c.getnotify()
self.assert_(isinstance(r, tuple))
- self.assertEqual(len(r), 2)
+ self.assertEqual(len(r), 3)
self.assert_(isinstance(r[0], str))
self.assert_(isinstance(r[1], int))
+ self.assert_(isinstance(r[2], str))
self.assertEqual(r[0], 'test_notify')
+ self.assertEqual(r[2], '')
+ self.assert_(self.c.getnotify() is None)
+ try:
+ self.c.query("notify test_notify, 'test_payload'")
+ except pg.ProgrammingError: # PostgreSQL < 9.0
+ pass
+ else:
+ r = self.c.getnotify()
+ self.assert_(isinstance(r, tuple))
+ self.assertEqual(len(r), 3)
+ self.assert_(isinstance(r[0], str))
+ self.assert_(isinstance(r[1], int))
+ self.assert_(isinstance(r[2], str))
+ self.assertEqual(r[0], 'test_notify')
+ self.assertEqual(r[2], 'test_payload')
+ self.assert_(self.c.getnotify() is None)
finally:
self.c.query('unlisten test_notify')
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql