-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathconnector.py
More file actions
184 lines (144 loc) · 5.13 KB
/
Copy pathconnector.py
File metadata and controls
184 lines (144 loc) · 5.13 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
import asyncio
import asynch
import asynch.errors
from sqlalchemy.engine.interfaces import AdaptedConnection
from sqlalchemy.util.concurrency import await_only
class AsyncAdapt_asynch_cursor:
__slots__ = (
'_adapt_connection',
'_connection',
'await_',
'_cursor',
'_rows'
)
def __init__(self, adapt_connection):
self._adapt_connection = adapt_connection
self._connection = adapt_connection._connection # noqa
self.await_ = adapt_connection.await_
cursor = self._connection.cursor()
self._cursor = self.await_(cursor.__aenter__())
self._rows = []
@property
def _execute_mutex(self):
return self._adapt_connection._execute_mutex # noqa
@property
def description(self):
return self._cursor.description
@property
def rowcount(self):
return self._cursor.rowcount
@property
def arraysize(self):
return self._cursor.arraysize
@arraysize.setter
def arraysize(self, value):
self._cursor.arraysize = value
@property
def lastrowid(self):
return self._cursor.lastrowid
def close(self):
# note we aren't actually closing the cursor here,
# we are just letting GC do it. to allow this to be async
# we would need the Result to change how it does "Safe close cursor".
self._rows[:] = [] # noqa
def execute(self, operation, params=None, context=None):
return self.await_(self._execute_async(operation, params, context))
async def _execute_async(self, operation, params, context):
async with self._execute_mutex:
result = await self._cursor.execute(
operation,
args=params,
context=context
)
self._rows = list(await self._cursor.fetchall())
return result
def executemany(self, operation, params=None, context=None):
return self.await_(self._executemany_async(operation, params, context))
async def _executemany_async(self, operation, params, context):
async with self._execute_mutex:
return await self._cursor.executemany(
operation,
args=params,
context=context
)
def setinputsizes(self, *args):
pass
def setoutputsizes(self, *args):
pass
def __iter__(self):
while self._rows:
yield self._rows.pop(0)
def fetchone(self):
if self._rows:
return self._rows.pop(0)
else:
return None
def fetchmany(self, size=None):
if size is None:
size = self.arraysize
retval = self._rows[0:size]
self._rows[:] = self._rows[size:]
return retval
def fetchall(self):
retval = self._rows[:]
self._rows[:] = []
return retval
class AsyncAdapt_asynch_dbapi:
def __init__(self):
self.paramstyle = 'pyformat'
self._init_dbapi_attributes()
def _init_dbapi_attributes(self):
self.Error = asynch.errors.ClickHouseException
for name in (
'ServerException',
'UnexpectedPacketFromServerError',
'LogicalError',
'UnknownTypeError',
'ChecksumDoesntMatchError',
'TypeMismatchError',
'UnknownCompressionMethod',
'TooLargeStringSize',
'NetworkError',
'SocketTimeoutError',
'UnknownPacketFromServerError',
'CannotParseUuidError',
'CannotParseDomainError',
'PartiallyConsumedQueryError',
'ColumnException',
'ColumnTypeMismatchException',
'StructPackException',
'InterfaceError',
'DatabaseError',
'ProgrammingError',
'NotSupportedError',
):
setattr(self, name, getattr(asynch.errors, name))
def connect(self, *args, **kwargs) -> 'AsyncAdapt_asynch_connection':
return AsyncAdapt_asynch_connection(
self,
await_only(asynch.connect(*args, **kwargs))
)
class AsyncAdapt_asynch_connection(AdaptedConnection):
await_ = staticmethod(await_only)
__slots__ = ('dbapi', '_execute_mutex')
def __init__(self, dbapi, connection):
self.dbapi = dbapi
self._connection = connection
self._execute_mutex = asyncio.Lock()
def ping(self, reconnect):
return self.await_(self._ping_async())
async def _ping_async(self):
async with self._execute_mutex:
return await self._connection.ping()
def character_set_name(self):
return self._connection.character_set_name()
def autocommit(self, value):
self.await_(self._connection.autocommit(value))
def cursor(self, server_side=False):
return AsyncAdapt_asynch_cursor(self)
def rollback(self):
self.await_(self._connection.rollback())
def commit(self):
self.await_(self._connection.commit())
def close(self):
self.await_(self._connection.close())