39
|
1 # -*- coding: utf-8 -*-
|
|
2 #
|
|
3 # Copyright (C) 2005-2006 Edgewall Software
|
|
4 # Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
|
|
5 # Copyright (C) 2006 Matthew Good <trac@matt-good.net>
|
|
6 # All rights reserved.
|
|
7 #
|
|
8 # This software is licensed as described in the file COPYING, which
|
|
9 # you should have received as part of this distribution. The terms
|
|
10 # are also available at http://trac.edgewall.com/license.html.
|
|
11 #
|
|
12 # This software consists of voluntary contributions made by many
|
|
13 # individuals. For the exact contribution history, see the revision
|
|
14 # history and logs, available at http://projects.edgewall.com/trac/.
|
|
15 #
|
|
16 # Author: Christopher Lenz <cmlenz@gmx.de>
|
|
17
|
|
18 def sql_escape_percent(sql):
|
|
19 import re
|
|
20 return re.sub("'((?:[^']|(?:''))*)'", lambda m: m.group(0).replace('%', '%%'), sql)
|
|
21
|
|
22
|
|
23 class IterableCursor(object):
|
|
24 """Wrapper for DB-API cursor objects that makes the cursor iterable
|
|
25 and escapes all "%"s used inside literal strings with parameterized
|
|
26 queries.
|
|
27
|
|
28 Iteration will generate the rows of a SELECT query one by one.
|
|
29 """
|
|
30 __slots__ = ['cursor']
|
|
31
|
|
32 def __init__(self, cursor):
|
|
33 self.cursor = cursor
|
|
34
|
|
35 def __getattr__(self, name):
|
|
36 return getattr(self.cursor, name)
|
|
37
|
|
38 def __iter__(self):
|
|
39 while True:
|
|
40 row = self.cursor.fetchone()
|
|
41 if not row:
|
|
42 return
|
|
43 yield row
|
|
44
|
|
45 def execute(self, sql, args=None):
|
|
46 if args:
|
|
47 return self.cursor.execute(sql_escape_percent(sql), args)
|
|
48 return self.cursor.execute(sql)
|
|
49
|
|
50 def executemany(self, sql, args=None):
|
|
51 if args:
|
|
52 return self.cursor.executemany(sql_escape_percent(sql), args)
|
|
53 return self.cursor.executemany(sql)
|
|
54
|
|
55
|
|
56 class ConnectionWrapper(object):
|
|
57 """Generic wrapper around connection objects.
|
|
58
|
|
59 This wrapper makes cursors produced by the connection iterable using
|
|
60 `IterableCursor`.
|
|
61 """
|
|
62 __slots__ = ['cnx']
|
|
63
|
|
64 def __init__(self, cnx):
|
|
65 self.cnx = cnx
|
|
66
|
|
67 def __getattr__(self, name):
|
|
68 if hasattr(self, 'cnx'):
|
|
69 return getattr(self.cnx, name)
|
|
70 return object.__getattr__(self, name)
|
|
71
|
|
72 def cursor(self):
|
|
73 return IterableCursor(self.cnx.cursor())
|