-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgormless.py
29 lines (24 loc) · 898 Bytes
/
gormless.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
from dill import loads
from psycopg2.extensions import cursor as _cursor
from psycopg2.extensions import connection as _connection
class DillConnection(_connection):
def cursor(self, *args, **kwargs):
kwargs.setdefault('cursor_factory', DillCursor)
return super().cursor(*args, **kwargs)
class DillCursor(_cursor):
def fetchone(self):
t = super().fetchone()
if t is not None:
return loads(t[0])._bind(cur=self)
def fetchmany(self, size=None):
return (loads(t)._bind(cur=self) for t, in super().fetchmany(size))
def fetchall(self):
return (loads(t)._bind(cur=self) for t, in super().fetchall())
def __iter__(self):
try:
it = super().__iter__()
while True:
t, = next(it)
yield loads(t)._bind(cur=self)
except StopIteration:
return