Commit f393ee06 authored by D'Arcy J.M. Cain's avatar D'Arcy J.M. Cain

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.
parent 04c8785c
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
# "Classic" interface. For DB-API compliance use the pgdb module. # "Classic" interface. For DB-API compliance use the pgdb module.
from _pg import * from _pg import *
from types import *
import string, re, sys import string, re, sys
# utility function # utility function
...@@ -73,10 +74,15 @@ class DB: ...@@ -73,10 +74,15 @@ class DB:
pg_attribute.attisdropped = 'f'""").getresult(): pg_attribute.attisdropped = 'f'""").getresult():
self.__pkeys__[rel] = att 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 # wrap query for debugging
def query(self, qstr): def query(self, qstr):
if self.debug != None: self._do_debug(qstr)
print self.debug % qstr
return self.db.query(qstr) return self.db.query(qstr)
# If third arg supplied set primary key to it # If third arg supplied set primary key to it
...@@ -158,7 +164,7 @@ class DB: ...@@ -158,7 +164,7 @@ class DB:
fnames = self.get_attnames(xcl) fnames = self.get_attnames(xcl)
if type(arg) == type({}): if type(arg) == DictType:
# To allow users to work with multiple tables we munge the # To allow users to work with multiple tables we munge the
# name when the key is "oid" # name when the key is "oid"
if keyname == 'oid': k = arg['oid_%s' % xcl] if keyname == 'oid': k = arg['oid_%s' % xcl]
...@@ -178,7 +184,7 @@ class DB: ...@@ -178,7 +184,7 @@ class DB:
(xcl, string.join(fnames.keys(), ','),\ (xcl, string.join(fnames.keys(), ','),\
cl, keyname, _quote(k, fnames[keyname])) cl, keyname, _quote(k, fnames[keyname]))
if self.debug != None: print self.debug % q self._do_debug(q)
res = self.db.query(q).dictresult() res = self.db.query(q).dictresult()
if res == []: if res == []:
raise error, \ raise error, \
...@@ -205,7 +211,7 @@ class DB: ...@@ -205,7 +211,7 @@ class DB:
try: try:
q = "INSERT INTO %s (%s) VALUES (%s)" % \ q = "INSERT INTO %s (%s) VALUES (%s)" % \
(cl, string.join(n, ','), string.join(l, ',')) (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) a['oid_%s' % cl] = self.db.query(q)
except: except:
raise error, "Error inserting into %s: %s" % (cl, sys.exc_value) raise error, "Error inserting into %s: %s" % (cl, sys.exc_value)
...@@ -241,7 +247,7 @@ class DB: ...@@ -241,7 +247,7 @@ class DB:
try: try:
q = "UPDATE %s SET %s WHERE %s" % \ q = "UPDATE %s SET %s WHERE %s" % \
(cl, string.join(v, ','), where) (cl, string.join(v, ','), where)
if self.debug != None: print self.debug % q self._do_debug(q)
self.db.query(q) self.db.query(q)
except: except:
raise error, "Can't update %s: %s" % (cl, sys.exc_value) raise error, "Can't update %s: %s" % (cl, sys.exc_value)
...@@ -270,7 +276,7 @@ class DB: ...@@ -270,7 +276,7 @@ class DB:
def delete(self, cl, a): def delete(self, cl, a):
try: try:
q = "DELETE FROM %s WHERE oid = %s" % (cl, a['oid_%s' % cl]) 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) self.db.query(q)
except: except:
raise error, "Can't delete %s: %s" % (cl, sys.exc_value) raise error, "Can't delete %s: %s" % (cl, sys.exc_value)
......
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