@@ -227,21 +227,37 @@ def __repr__(self):
227
227
228
228
sqlliteral = SQLLiteral
229
229
230
+ def _sqllist (values ):
231
+ """
232
+ >>> _sqllist([1, 2, 3])
233
+ <sql: '(1, 2, 3)'>
234
+ """
235
+ items = []
236
+ items .append ('(' )
237
+ for i , v in enumerate (values ):
238
+ if i != 0 :
239
+ items .append (', ' )
240
+ items .append (sqlparam (v ))
241
+ items .append (')' )
242
+ return SQLQuery (items )
243
+
230
244
def reparam (string_ , dictionary ):
231
245
"""
232
246
Takes a string and a dictionary and interpolates the string
233
247
using values from the dictionary. Returns an `SQLQuery` for the result.
234
248
235
249
>>> reparam("s = $s", dict(s=True))
236
250
<sql: "s = 't'">
251
+ >>> reparam("s IN $s", dict(s=[1, 2]))
252
+ <sql: 's IN (1, 2)'>
237
253
"""
238
254
dictionary = dictionary .copy () # eval mucks with it
239
255
vals = []
240
256
result = []
241
257
for live , chunk in _interpolate (string_ ):
242
258
if live :
243
259
v = eval (chunk , dictionary )
244
- result .append (sqlparam (v ))
260
+ result .append (sqlquote (v ))
245
261
else :
246
262
result .append (chunk )
247
263
return SQLQuery .join (result , '' )
@@ -338,8 +354,13 @@ def sqlquote(a):
338
354
339
355
>>> 'WHERE x = ' + sqlquote(True) + ' AND y = ' + sqlquote(3)
340
356
<sql: "WHERE x = 't' AND y = 3">
357
+ >>> 'WHERE x = ' + sqlquote(True) + ' AND y IN ' + sqlquote([2, 3])
358
+ <sql: "WHERE x = 't' AND y IN (2, 3)">
341
359
"""
342
- return sqlparam (a ).sqlquery ()
360
+ if isinstance (a , list ):
361
+ return _sqllist (a )
362
+ else :
363
+ return sqlparam (a ).sqlquery ()
343
364
344
365
class Transaction :
345
366
"""Database transaction."""
0 commit comments