From: D'Arcy J.M. Cain Date: Mon, 25 Nov 2002 01:28:32 +0000 (+0000) Subject: Change the debug variable to allow better control by the caller over how X-Git-Tag: REL7_4_BETA1~1491 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f393ee06802c247faa53cde2ad947f5e96fb92f2;p=postgresql 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. --- diff --git a/src/interfaces/python/pg.py b/src/interfaces/python/pg.py index 6a8d8ac78a..a5997341bc 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)