summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2006-01-12 17:41:00 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2006-01-12 17:41:00 +0000
commit99ce95b27e3a65c50f6b27d18b851fb27df11d29 (patch)
treefdcdf838091ed5297d7c64171a570d8f4b5e738d
parent791a728c01cdaca1438b07a91fea4324ae163e23 (diff)
downloadpsycopg2-99ce95b27e3a65c50f6b27d18b851fb27df11d29.tar.gz
Added a cursor subclass example.
-rw-r--r--doc/extensions.rst23
1 files changed, 22 insertions, 1 deletions
diff --git a/doc/extensions.rst b/doc/extensions.rst
index 2708ef6..3bdc680 100644
--- a/doc/extensions.rst
+++ b/doc/extensions.rst
@@ -25,7 +25,28 @@ customized cursors but other uses are possible. `cursor` is much more
interesting, because it is the class where query building, execution and result
type-casting into Python variables happens.
-`connection` instances
+An example of cursor subclass performing logging is::
+
+ import psycopg2
+ import psycopg2.extensions
+ import logging
+
+ class LoggingCursor(psycopg2.extensions.cursor):
+ def execute(self, sql, args=None):
+ logger = logging.getLogger('sql_debug')
+ logger.info(self.mogrify(sql, args))
+
+ try:
+ psycopg2.extensions.cursor.execute(self, sql, args)
+ except Exception, exc:
+ logger.error("%s: %s" % (exc.__class__.__name__, exc))
+ raise
+
+ conn = psycopg2.connect(DSN)
+ curs = conn.cursor(cursor_factory=LoggingCursor)
+ curs.execute("INSERT INTO mytable VALUES (%s, %s, %s);",
+ (10, 20, 30))
+
Row factories
-------------