Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f393ee0

Browse files
committed
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.
1 parent 04c8785 commit f393ee0

File tree

1 file changed

+13
-7
lines changed
  • src/interfaces/python

1 file changed

+13
-7
lines changed

src/interfaces/python/pg.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# "Classic" interface. For DB-API compliance use the pgdb module.
77

88
from _pg import *
9+
from types import *
910
import string, re, sys
1011

1112
# utility function
@@ -73,10 +74,15 @@ def __init__(self, *args, **kw):
7374
pg_attribute.attisdropped = 'f'""").getresult():
7475
self.__pkeys__[rel] = att
7576

77+
def _do_debug(self, s):
78+
if not self.debug: return
79+
if type(self.debug) == StringType: print self.debug % s
80+
if type(self.debug) == FunctionType: self.debug(s)
81+
if type(self.debug) == FileType: print >> self.debug, s
82+
7683
# wrap query for debugging
7784
def query(self, qstr):
78-
if self.debug != None:
79-
print self.debug % qstr
85+
self._do_debug(qstr)
8086
return self.db.query(qstr)
8187

8288
# If third arg supplied set primary key to it
@@ -158,7 +164,7 @@ def get(self, cl, arg, keyname = None, view = 0):
158164

159165
fnames = self.get_attnames(xcl)
160166

161-
if type(arg) == type({}):
167+
if type(arg) == DictType:
162168
# To allow users to work with multiple tables we munge the
163169
# name when the key is "oid"
164170
if keyname == 'oid': k = arg['oid_%s' % xcl]
@@ -178,7 +184,7 @@ def get(self, cl, arg, keyname = None, view = 0):
178184
(xcl, string.join(fnames.keys(), ','),\
179185
cl, keyname, _quote(k, fnames[keyname]))
180186

181-
if self.debug != None: print self.debug % q
187+
self._do_debug(q)
182188
res = self.db.query(q).dictresult()
183189
if res == []:
184190
raise error, \
@@ -205,7 +211,7 @@ def insert(self, cl, a):
205211
try:
206212
q = "INSERT INTO %s (%s) VALUES (%s)" % \
207213
(cl, string.join(n, ','), string.join(l, ','))
208-
if self.debug != None: print self.debug % q
214+
self._do_debug(q)
209215
a['oid_%s' % cl] = self.db.query(q)
210216
except:
211217
raise error, "Error inserting into %s: %s" % (cl, sys.exc_value)
@@ -241,7 +247,7 @@ def update(self, cl, a):
241247
try:
242248
q = "UPDATE %s SET %s WHERE %s" % \
243249
(cl, string.join(v, ','), where)
244-
if self.debug != None: print self.debug % q
250+
self._do_debug(q)
245251
self.db.query(q)
246252
except:
247253
raise error, "Can't update %s: %s" % (cl, sys.exc_value)
@@ -270,7 +276,7 @@ def clear(self, cl, a = {}):
270276
def delete(self, cl, a):
271277
try:
272278
q = "DELETE FROM %s WHERE oid = %s" % (cl, a['oid_%s' % cl])
273-
if self.debug != None: print self.debug % q
279+
self._do_debug(q)
274280
self.db.query(q)
275281
except:
276282
raise error, "Can't delete %s: %s" % (cl, sys.exc_value)

0 commit comments

Comments
 (0)