-
Notifications
You must be signed in to change notification settings - Fork 247
/
interface.py
288 lines (220 loc) · 8.8 KB
/
interface.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright 2014 Cloudera Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import datetime
import re
import six
from six import reraise
from impala.util import _escape
from impala.error import ( # pylint: disable=unused-import
Error, Warning, InterfaceError, DatabaseError, InternalError,
OperationalError, ProgrammingError, IntegrityError, DataError,
NotSupportedError)
class Connection(object):
# PEP 249
# Connection objects are associated with a TCLIService.Client thrift
# service
# it's instantiated with an alive TCLIService.Client
def close(self):
# PEP 249
raise NotImplementedError
def commit(self):
# PEP 249
raise NotImplementedError
def rollback(self):
# PEP 249
raise NotImplementedError
def cursor(self, user=None, configuration=None, convert_types=True):
# PEP 249
raise NotImplementedError
def reconnect(self):
raise NotImplementedError
def kerberized(self):
# pylint: disable=protected-access
# returns bool whether underlying service is kerberized or not
from thrift_sasl import TSaslClientTransport
if isinstance(self.service._iprot.trans, TSaslClientTransport):
if self.service._iprot.trans.mechanism == 'GSSAPI':
return True
return False
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
if exc_type is not None:
reraise(exc_type, exc_val, exc_tb)
# optional DB API addition to make the errors attributes of Connection
Error = Error
Warning = Warning
InterfaceError = InterfaceError
DatabaseError = DatabaseError
InternalError = InternalError
OperationalError = OperationalError
ProgrammingError = ProgrammingError
IntegrityError = IntegrityError
DataError = DataError
NotSupportedError = NotSupportedError
class Cursor(object):
"""Abstract representation of Cursor"""
def description(self):
raise NotImplementedError
def rowcount(self):
raise NotImplementedError
def lastrowid(self):
raise NotImplementedError
def query_string(self):
raise NotImplementedError
def get_arraysize(self):
raise NotImplementedError
def set_arraysize(self, arraysize):
raise NotImplementedError
def buffersize(self):
raise NotImplementedError
def has_result_set(self):
raise NotImplementedError
def close(self):
raise NotImplementedError
def close_operation(self):
raise NotImplementedError
def execute(self, operation, parameters=None, configuration=None):
raise NotImplementedError
def _execute_sync(self, operation_fn):
raise NotImplementedError
def _reset_state(self):
raise NotImplementedError
def _wait_to_finish(self):
raise NotImplementedError
def executemany(self, operation, seq_of_parameters):
raise NotImplementedError
def fetchone(self):
raise NotImplementedError
def fetchmany(self, size=None):
raise NotImplementedError
def fetchall(self):
raise NotImplementedError
def setinputsizes(self, sizes):
raise NotImplementedError
def setoutputsize(self, size, column=None):
raise NotImplementedError
def __iter__(self):
raise NotImplementedError
def __next__(self):
raise NotImplementedError
def next(self):
# for py2 compat
return self.__next__()
def ping(self):
raise NotImplementedError
def get_log(self):
raise NotImplementedError
def get_profile(self):
raise NotImplementedError
def get_summary(self):
raise NotImplementedError
def build_summary_table(self, summary, idx, is_fragment_root, indent_level,
output):
raise NotImplementedError
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
if exc_type is not None:
reraise(exc_type, exc_val, exc_tb)
def _replace_numeric_markers(operation, string_parameters, paramstyle):
"""
Replaces qmark, format, and numeric markers in the given operation, from
the string_parameters list.
Raises ProgrammingError on wrong number of parameters or markers.
For numeric markers there has to be enough parameters to satisfy
every marker and there has to bo no unused parameter.
"""
RE_QMARK = r'(?P<qmark>\?)'
RE_FORMAT = r'(?P<format>%s)'
RE_NUMERIC = r'(?P<numeric>:(?P<index>\d+))'
RE_ALL = '|'.join([RE_QMARK, RE_FORMAT, RE_NUMERIC])
if paramstyle is not None:
if paramstyle in ['named', 'pyformat']:
raise ProgrammingError(
"Paramstyle '%s' is not compatible with parameters passed as "
"list. Please use a dict for your parameters instead "
"or specify a different paramstyle" % paramstyle)
if paramstyle not in ['qmark', 'format', 'numeric']:
raise ProgrammingError(
"Paramstyle '%s' is not supported. Please use a different one")
param_count = len(string_parameters)
used_positional_indexes = set()
used_numeric_indexes = set()
def replace_marker(match):
if paramstyle is not None and match.group(paramstyle) is None:
return match.group(0)
if match.group('index') is not None:
param_index = int(match.group('index')) - 1
used_numeric_indexes.add(param_index)
else:
param_index = len(used_positional_indexes)
used_positional_indexes.add(param_index)
if param_index >= param_count:
raise ProgrammingError(
"Incorrect number of bindings supplied. The current statement "
"uses %d or more, and there are %d supplied." % (
param_index, param_count))
return string_parameters[param_index]
operation = re.sub(RE_ALL, replace_marker, operation)
marker_count = len(used_numeric_indexes | used_positional_indexes)
if marker_count < param_count:
raise ProgrammingError(
"Incorrect number of bindings supplied. The current statement "
"uses %d, and there are %d supplied." % (
marker_count, param_count))
return operation
def _bind_parameters_list(operation, parameters, paramstyle):
string_parameters = []
for value in parameters:
if value is None:
string_parameters.append('NULL')
elif isinstance(value, six.string_types):
string_parameters.append("'" + _escape(value) + "'")
elif isinstance(value, datetime.datetime):
string_parameters.append("'" + str(value) + "'")
elif isinstance(value, datetime.date):
string_parameters.append("'" + str(value) + "'")
else:
string_parameters.append(str(value))
# replace qmark and numeric parameters
return _replace_numeric_markers(operation, string_parameters, paramstyle)
def _bind_parameters_dict(operation, parameters):
string_parameters = {}
for (name, value) in six.iteritems(parameters):
if value is None:
string_parameters[name] = 'NULL'
elif isinstance(value, six.string_types):
string_parameters[name] = "'" + _escape(value) + "'"
elif isinstance(value, datetime.date):
string_parameters[name] = "'{0}'".format(value)
else:
string_parameters[name] = str(value)
# replace named parameters by their pyformat equivalents
operation = re.sub(r":([^\d\W]\w*)", r"%(\g<1>)s", operation)
# replace pyformat parameters
return operation % string_parameters
def _bind_parameters(operation, parameters, paramstyle=None):
# If parameters is a list, assume either qmark, format, or numeric
# format. If not, assume either named or pyformat parameters
if isinstance(parameters, (list, tuple)):
return _bind_parameters_list(operation, parameters, paramstyle)
elif isinstance(parameters, dict):
return _bind_parameters_dict(operation, parameters)
else:
raise ProgrammingError("Query parameters argument should be a "
"list, tuple, or dict object")