Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf393ee0

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 methodof passing a formatting string to have a replacement done and output willbe sent to the standard output exactly as it did before. In addition theycan set it to a file object, sys.stderr for example, and the query stringwill be printed to it. Thay can also set it to a method (function) and thequery string will be passed to that method giving them the maximum flexibilityto do whatever they want with the query string.I will be working with the PyGreSQL documentation shortly and at that timewill properly document this feature.
1 parent04c8785 commitf393ee0

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_pgimport*
9+
fromtypesimport*
910
importstring,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+
ifnotself.debug:return
79+
iftype(self.debug)==StringType:printself.debug%s
80+
iftype(self.debug)==FunctionType:self.debug(s)
81+
iftype(self.debug)==FileType:print>>self.debug,s
82+
7683
# wrap query for debugging
7784
defquery(self,qstr):
78-
ifself.debug!=None:
79-
printself.debug%qstr
85+
self._do_debug(qstr)
8086
returnself.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-
iftype(arg)==type({}):
167+
iftype(arg)==DictType:
162168
# To allow users to work with multiple tables we munge the
163169
# name when the key is "oid"
164170
ifkeyname=='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-
ifself.debug!=None:printself.debug%q
187+
self._do_debug(q)
182188
res=self.db.query(q).dictresult()
183189
ifres== []:
184190
raiseerror, \
@@ -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-
ifself.debug!=None:printself.debug%q
214+
self._do_debug(q)
209215
a['oid_%s'%cl]=self.db.query(q)
210216
except:
211217
raiseerror,"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-
ifself.debug!=None:printself.debug%q
250+
self._do_debug(q)
245251
self.db.query(q)
246252
except:
247253
raiseerror,"Can't update %s: %s"% (cl,sys.exc_value)
@@ -270,7 +276,7 @@ def clear(self, cl, a = {}):
270276
defdelete(self,cl,a):
271277
try:
272278
q="DELETE FROM %s WHERE oid = %s"% (cl,a['oid_%s'%cl])
273-
ifself.debug!=None:printself.debug%q
279+
self._do_debug(q)
274280
self.db.query(q)
275281
except:
276282
raiseerror,"Can't delete %s: %s"% (cl,sys.exc_value)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp