Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorD'Arcy J.M. Cain2002-11-25 01:28:32 +0000
committerD'Arcy J.M. Cain2002-11-25 01:28:32 +0000
commitf393ee06802c247faa53cde2ad947f5e96fb92f2 (patch)
tree35af40fc1c3b8cca010212b6a1e39be56c09901a /src/interfaces/python
parent04c8785c7b2b3dea038522cd96085c710c628c5b (diff)
Change the debug variable to allow better control by the caller over how
debug output is managed. The user can continue to use the current method of passing a formatting string to have a replacement done and output will be sent to the standard output exactly as it did before. In addition they can set it to a file object, sys.stderr for example, and the query string will be printed to it. Thay can also set it to a method (function) and the query string will be passed to that method giving them the maximum flexibility to do whatever they want with the query string. I will be working with the PyGreSQL documentation shortly and at that time will properly document this feature.
Diffstat (limited to 'src/interfaces/python')
-rw-r--r--src/interfaces/python/pg.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/interfaces/python/pg.py b/src/interfaces/python/pg.py
index 6a8d8ac78a2..a5997341bcf 100644
--- a/src/interfaces/python/pg.py
+++ b/src/interfaces/python/pg.py
@@ -6,6 +6,7 @@
# "Classic" interface. For DB-API compliance use the pgdb module.
from _pg import *
+from types import *
import string, re, sys
# utility function
@@ -73,10 +74,15 @@ class DB:
pg_attribute.attisdropped = 'f'""").getresult():
self.__pkeys__[rel] = att
+ def _do_debug(self, s):
+ if not self.debug: return
+ if type(self.debug) == StringType: print self.debug % s
+ if type(self.debug) == FunctionType: self.debug(s)
+ if type(self.debug) == FileType: print >> self.debug, s
+
# wrap query for debugging
def query(self, qstr):
- if self.debug != None:
- print self.debug % qstr
+ self._do_debug(qstr)
return self.db.query(qstr)
# If third arg supplied set primary key to it
@@ -158,7 +164,7 @@ class DB:
fnames = self.get_attnames(xcl)
- if type(arg) == type({}):
+ if type(arg) == DictType:
# To allow users to work with multiple tables we munge the
# name when the key is "oid"
if keyname == 'oid': k = arg['oid_%s' % xcl]
@@ -178,7 +184,7 @@ class DB:
(xcl, string.join(fnames.keys(), ','),\
cl, keyname, _quote(k, fnames[keyname]))
- if self.debug != None: print self.debug % q
+ self._do_debug(q)
res = self.db.query(q).dictresult()
if res == []:
raise error, \
@@ -205,7 +211,7 @@ class DB:
try:
q = "INSERT INTO %s (%s) VALUES (%s)" % \
(cl, string.join(n, ','), string.join(l, ','))
- if self.debug != None: print self.debug % q
+ self._do_debug(q)
a['oid_%s' % cl] = self.db.query(q)
except:
raise error, "Error inserting into %s: %s" % (cl, sys.exc_value)
@@ -241,7 +247,7 @@ class DB:
try:
q = "UPDATE %s SET %s WHERE %s" % \
(cl, string.join(v, ','), where)
- if self.debug != None: print self.debug % q
+ self._do_debug(q)
self.db.query(q)
except:
raise error, "Can't update %s: %s" % (cl, sys.exc_value)
@@ -270,7 +276,7 @@ class DB:
def delete(self, cl, a):
try:
q = "DELETE FROM %s WHERE oid = %s" % (cl, a['oid_%s' % cl])
- if self.debug != None: print self.debug % q
+ self._do_debug(q)
self.db.query(q)
except:
raise error, "Can't delete %s: %s" % (cl, sys.exc_value)