/
api.py
42 lines (32 loc) · 1.02 KB
/
api.py
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
import collections
class NestedCursorType(object):
def __eq__(self, other):
return other == 5001 or isinstance(other, NestedCursorType)
def __hash__(self):
return hash(5001)
NESTED_CURSOR = NestedCursorType()
class NestedCursor(object):
def __init__(self, ctx, arraysize, fields, description_factory):
self.ctx = ctx
self._fields = fields
self._description_factory = description_factory
self._rows = collections.deque()
self.arraysize = arraysize
@property
def description(self):
return self._description_factory(self._fields)
def fetchone(self):
if self._rows:
return self._rows.popleft()
else:
return None
def fetchall(self):
r = list(self._rows)
self._rows.clear()
return r
def fetchmany(self, size=None):
if size is None:
size = self.arraysize
l = list(self._rows)
r, self._rows = l[0:size], collections.deque(l[size:])
return r