-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcursor.py
More file actions
203 lines (179 loc) · 6.18 KB
/
cursor.py
File metadata and controls
203 lines (179 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from crate.client.exceptions import ProgrammingError
from distutils.version import StrictVersion
import warnings
BULK_INSERT_MIN_VERSION = StrictVersion("0.42.0")
class Cursor(object):
"""
not thread-safe by intention
should not be shared between different threads
"""
lastrowid = None # currently not supported
def __init__(self, connection, cursor):
self.arraysize = 1
self.connection = connection
self._closed = False
self._result = None
self.rows = None
self._cursor = cursor
def execute(self, sql, parameters=None, bulk_parameters=None):
"""
Prepare and execute a database operation (query or command).
"""
if self.connection._closed:
raise ProgrammingError("Connection closed")
if self._closed:
raise ProgrammingError("Cursor closed")
self._result = self.connection.client.sql(sql, parameters,
bulk_parameters)
if "rows" in self._result:
self.rows = iter(self._result["rows"])
def executemany(self, sql, seq_of_parameters):
"""
Prepare a database operation (query or command) and then execute it
against all parameter sequences or mappings found in the sequence
``seq_of_parameters``.
"""
row_counts = []
durations = []
if self.connection.lowest_server_version >= BULK_INSERT_MIN_VERSION:
self.execute(sql, bulk_parameters=seq_of_parameters)
for result in self._result.get('results', []):
if result.get('rowcount') > -1:
row_counts.append(result.get('rowcount'))
if self.duration > -1:
durations.append(self.duration)
else:
for params in seq_of_parameters:
self.execute(sql, parameters=params)
if self.rowcount > -1:
row_counts.append(self.rowcount)
if self.duration > -1:
durations.append(self.duration)
self._result = {
"rowcount": sum(row_counts) if row_counts else -1,
"duration": sum(durations) if durations else -1,
"rows": [],
"cols": self._result.get("cols", []),
"results": self._result.get("results")
}
self.rows = iter(self._result["rows"])
return self._result["results"]
def fetchone(self):
"""
Fetch the next row of a query result set, returning a single sequence,
or None when no more data is available.
Alias for ``next()``.
"""
try:
return self.next()
except StopIteration:
return None
def __iter__(self):
"""
support iterator interface:
http://legacy.python.org/dev/peps/pep-0249/#iter
This iterator is shared. Advancing this iterator will advance other
iterators created from this cursor.
"""
warnings.warn("DB-API extension cursor.__iter__() used")
return self
def fetchmany(self, count=None):
"""
Fetch the next set of rows of a query result, returning a sequence of
sequences (e.g. a list of tuples). An empty sequence is returned when
no more rows are available.
"""
if count is None:
count = self.arraysize
if count == 0:
return self.fetchall()
result = []
for i in range(count):
try:
result.append(self.next())
except StopIteration:
pass
return result
def fetchall(self):
"""
Fetch all (remaining) rows of a query result, returning them as a
sequence of sequences (e.g. a list of tuples). Note that the cursor's
arraysize attribute can affect the performance of this operation.
"""
result = []
iterate = True
while iterate:
try:
result.append(self.next())
except StopIteration:
iterate = False
return result
def close(self):
"""
Close the cursor now
"""
self._closed = True
self._result = None
def setinputsizes(self, sizes):
"""
Not supported method.
"""
pass
def setoutputsize(self, size, column=None):
"""
Not supported method.
"""
pass
@property
def rowcount(self):
"""
This read-only attribute specifies the number of rows that the last
.execute*() produced (for DQL statements like ``SELECT``) or affected
(for DML statements like ``UPDATE`` or ``INSERT``).
"""
if (self._closed or not self._result or "rows" not in self._result):
return -1
return self._result.get("rowcount", -1)
def next(self):
"""
Return the next row of a query result set, respecting if cursor was
closed.
"""
if self.rows is None:
raise ProgrammingError(
"No result available. " +
"execute() or executemany() must be called first."
)
elif not self._closed:
return next(self.rows)
else:
raise ProgrammingError("Cursor closed")
__next__ = next
@property
def description(self):
"""
This read-only attribute is a sequence of 7-item sequences.
"""
if self._closed:
return
description = []
for col in self._result["cols"]:
description.append((col,
None,
None,
None,
None,
None,
None))
return tuple(description)
@property
def duration(self):
"""
This read-only attribute specifies the server-side duration of a query
in milliseconds.
"""
if self._closed or \
not self._result or \
"duration" not in self._result:
return -1
return self._result.get("duration", 0)