60
60
import types
61
61
import DateTime
62
62
import time
63
+ import types
63
64
64
65
### module constants
65
66
@@ -175,9 +176,14 @@ def close(self):
175
176
self .rowcount = - 1
176
177
177
178
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 :
179
184
self .executemany (operation , params )
180
185
else :
186
+ # not a list of tuples
181
187
self .executemany (operation , (params ,))
182
188
183
189
def executemany (self , operation , param_seq ):
@@ -190,7 +196,7 @@ def executemany(self, operation, param_seq):
190
196
try :
191
197
for params in param_seq :
192
198
if params != None :
193
- sql = operation % params
199
+ sql = _quoteparams ( operation , params )
194
200
else :
195
201
sql = operation
196
202
rows = self .__source .execute (sql )
@@ -251,6 +257,34 @@ def setinputsizes(self, sizes):
251
257
def setoutputsize (self , size , col = 0 ):
252
258
pass
253
259
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
+
254
288
### connection object
255
289
256
290
class pgdbCnx :
0 commit comments