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

Commit551e0aa

Browse files
committed
Fix parameter handling.
Fix a bug where cs.execute('select %d + %d', (1, 2)) would get interpretedas cs.executemany('select %d + %d', (1, 2))
1 parent37d67eb commit551e0aa

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

‎src/interfaces/python/pgdb.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
importtypes
6161
importDateTime
6262
importtime
63+
importtypes
6364

6465
### module constants
6566

@@ -175,9 +176,14 @@ def close(self):
175176
self.rowcount=-1
176177

177178
defexecute(self,operation,params=None):
178-
iftype(params)==types.TupleTypeortype(params)==types.ListType:
179+
# "The parameters may also be specified as list of
180+
# tuples to e.g. insert multiple rows in a single
181+
# operation, but this kind of usage is depreciated:
182+
ifparamsandtype(params)==types.ListTypeand \
183+
type(params[0])==types.TupleType:
179184
self.executemany(operation,params)
180185
else:
186+
# not a list of tuples
181187
self.executemany(operation, (params,))
182188

183189
defexecutemany(self,operation,param_seq):
@@ -190,7 +196,7 @@ def executemany(self, operation, param_seq):
190196
try:
191197
forparamsinparam_seq:
192198
ifparams!=None:
193-
sql=operation%params
199+
sql=_quoteparams(operation,params)
194200
else:
195201
sql=operation
196202
rows=self.__source.execute(sql)
@@ -251,6 +257,34 @@ def setinputsizes(self, sizes):
251257
defsetoutputsize(self,size,col=0):
252258
pass
253259

260+
261+
def_quote(x):
262+
iftype(x)==types.StringType:
263+
x="'"+string.replace(
264+
string.replace(str(x),'\\','\\\\'),"'","''")+"'"
265+
266+
elif type(x)in (types.IntType,types.LongType,types.FloatType):
267+
pass
268+
elifxisNone:
269+
x='NULL'
270+
elifhasattr(x,'__pg_repr__'):
271+
x=x.__pg_repr__()
272+
else:
273+
raiseInterfaceError,'do not know how to handle type %s'%type(x)
274+
275+
returnx
276+
277+
def_quoteparams(s,params):
278+
ifhasattr(params,'has_key'):
279+
x= {}
280+
fork,vinparams.items():
281+
x[k]=_quote(v)
282+
params=x
283+
else:
284+
params=tuple(map(_quote,params))
285+
286+
returns%params
287+
254288
### connection object
255289

256290
classpgdbCnx:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp