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

Commit 551e0aa

Browse files
committed
Fix parameter handling.
Fix a bug where cs.execute('select %d + %d', (1, 2)) would get interpreted as cs.executemany('select %d + %d', (1, 2))
1 parent 37d67eb commit 551e0aa

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/interfaces/python/pgdb.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import types
6161
import DateTime
6262
import time
63+
import types
6364

6465
### module constants
6566

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

177178
def execute(self, operation, params = None):
178-
if type(params) == types.TupleType or type(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+
if params and type(params) == types.ListType and \
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
def executemany(self, operation, param_seq):
@@ -190,7 +196,7 @@ def executemany(self, operation, param_seq):
190196
try:
191197
for params in param_seq:
192198
if params != 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
def setoutputsize(self, size, col = 0):
252258
pass
253259

260+
261+
def _quote(x):
262+
if type(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+
elif x is None:
269+
x = 'NULL'
270+
elif hasattr(x, '__pg_repr__'):
271+
x = x.__pg_repr__()
272+
else:
273+
raise InterfaceError, 'do not know how to handle type %s' % type(x)
274+
275+
return x
276+
277+
def _quoteparams(s, params):
278+
if hasattr(params, 'has_key'):
279+
x = {}
280+
for k, v in params.items():
281+
x[k] = _quote(v)
282+
params = x
283+
else:
284+
params = tuple(map(_quote, params))
285+
286+
return s % params
287+
254288
### connection object
255289

256290
class pgdbCnx:

0 commit comments

Comments
 (0)