singlestoredb 0.3.3__py3-none-any.whl → 1.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of singlestoredb might be problematic. Click here for more details.

Files changed (121) hide show
  1. singlestoredb/__init__.py +33 -2
  2. singlestoredb/alchemy/__init__.py +90 -0
  3. singlestoredb/auth.py +6 -4
  4. singlestoredb/config.py +116 -16
  5. singlestoredb/connection.py +489 -523
  6. singlestoredb/converters.py +275 -26
  7. singlestoredb/exceptions.py +30 -4
  8. singlestoredb/functions/__init__.py +1 -0
  9. singlestoredb/functions/decorator.py +142 -0
  10. singlestoredb/functions/dtypes.py +1639 -0
  11. singlestoredb/functions/ext/__init__.py +2 -0
  12. singlestoredb/functions/ext/arrow.py +375 -0
  13. singlestoredb/functions/ext/asgi.py +661 -0
  14. singlestoredb/functions/ext/json.py +427 -0
  15. singlestoredb/functions/ext/mmap.py +306 -0
  16. singlestoredb/functions/ext/rowdat_1.py +744 -0
  17. singlestoredb/functions/signature.py +673 -0
  18. singlestoredb/fusion/__init__.py +11 -0
  19. singlestoredb/fusion/graphql.py +213 -0
  20. singlestoredb/fusion/handler.py +621 -0
  21. singlestoredb/fusion/handlers/__init__.py +0 -0
  22. singlestoredb/fusion/handlers/stage.py +257 -0
  23. singlestoredb/fusion/handlers/utils.py +162 -0
  24. singlestoredb/fusion/handlers/workspace.py +412 -0
  25. singlestoredb/fusion/registry.py +164 -0
  26. singlestoredb/fusion/result.py +399 -0
  27. singlestoredb/http/__init__.py +27 -0
  28. singlestoredb/http/connection.py +1192 -0
  29. singlestoredb/management/__init__.py +3 -2
  30. singlestoredb/management/billing_usage.py +148 -0
  31. singlestoredb/management/cluster.py +19 -14
  32. singlestoredb/management/manager.py +100 -40
  33. singlestoredb/management/organization.py +188 -0
  34. singlestoredb/management/region.py +6 -8
  35. singlestoredb/management/utils.py +253 -4
  36. singlestoredb/management/workspace.py +1153 -35
  37. singlestoredb/mysql/__init__.py +177 -0
  38. singlestoredb/mysql/_auth.py +298 -0
  39. singlestoredb/mysql/charset.py +214 -0
  40. singlestoredb/mysql/connection.py +1814 -0
  41. singlestoredb/mysql/constants/CLIENT.py +38 -0
  42. singlestoredb/mysql/constants/COMMAND.py +32 -0
  43. singlestoredb/mysql/constants/CR.py +78 -0
  44. singlestoredb/mysql/constants/ER.py +474 -0
  45. singlestoredb/mysql/constants/FIELD_TYPE.py +32 -0
  46. singlestoredb/mysql/constants/FLAG.py +15 -0
  47. singlestoredb/mysql/constants/SERVER_STATUS.py +10 -0
  48. singlestoredb/mysql/constants/__init__.py +0 -0
  49. singlestoredb/mysql/converters.py +271 -0
  50. singlestoredb/mysql/cursors.py +713 -0
  51. singlestoredb/mysql/err.py +92 -0
  52. singlestoredb/mysql/optionfile.py +20 -0
  53. singlestoredb/mysql/protocol.py +388 -0
  54. singlestoredb/mysql/tests/__init__.py +19 -0
  55. singlestoredb/mysql/tests/base.py +126 -0
  56. singlestoredb/mysql/tests/conftest.py +37 -0
  57. singlestoredb/mysql/tests/test_DictCursor.py +132 -0
  58. singlestoredb/mysql/tests/test_SSCursor.py +141 -0
  59. singlestoredb/mysql/tests/test_basic.py +452 -0
  60. singlestoredb/mysql/tests/test_connection.py +851 -0
  61. singlestoredb/mysql/tests/test_converters.py +58 -0
  62. singlestoredb/mysql/tests/test_cursor.py +141 -0
  63. singlestoredb/mysql/tests/test_err.py +16 -0
  64. singlestoredb/mysql/tests/test_issues.py +514 -0
  65. singlestoredb/mysql/tests/test_load_local.py +75 -0
  66. singlestoredb/mysql/tests/test_nextset.py +88 -0
  67. singlestoredb/mysql/tests/test_optionfile.py +27 -0
  68. singlestoredb/mysql/tests/thirdparty/__init__.py +6 -0
  69. singlestoredb/mysql/tests/thirdparty/test_MySQLdb/__init__.py +9 -0
  70. singlestoredb/mysql/tests/thirdparty/test_MySQLdb/capabilities.py +323 -0
  71. singlestoredb/mysql/tests/thirdparty/test_MySQLdb/dbapi20.py +865 -0
  72. singlestoredb/mysql/tests/thirdparty/test_MySQLdb/test_MySQLdb_capabilities.py +110 -0
  73. singlestoredb/mysql/tests/thirdparty/test_MySQLdb/test_MySQLdb_dbapi20.py +224 -0
  74. singlestoredb/mysql/tests/thirdparty/test_MySQLdb/test_MySQLdb_nonstandard.py +101 -0
  75. singlestoredb/mysql/times.py +23 -0
  76. singlestoredb/pytest.py +283 -0
  77. singlestoredb/tests/empty.sql +0 -0
  78. singlestoredb/tests/ext_funcs/__init__.py +385 -0
  79. singlestoredb/tests/test.sql +210 -0
  80. singlestoredb/tests/test2.sql +1 -0
  81. singlestoredb/tests/test_basics.py +482 -117
  82. singlestoredb/tests/test_config.py +13 -15
  83. singlestoredb/tests/test_connection.py +241 -289
  84. singlestoredb/tests/test_dbapi.py +27 -0
  85. singlestoredb/tests/test_exceptions.py +0 -2
  86. singlestoredb/tests/test_ext_func.py +1193 -0
  87. singlestoredb/tests/test_ext_func_data.py +1101 -0
  88. singlestoredb/tests/test_fusion.py +465 -0
  89. singlestoredb/tests/test_http.py +32 -28
  90. singlestoredb/tests/test_management.py +588 -10
  91. singlestoredb/tests/test_plugin.py +33 -0
  92. singlestoredb/tests/test_results.py +11 -14
  93. singlestoredb/tests/test_types.py +0 -2
  94. singlestoredb/tests/test_udf.py +687 -0
  95. singlestoredb/tests/test_xdict.py +0 -2
  96. singlestoredb/tests/utils.py +3 -4
  97. singlestoredb/types.py +4 -5
  98. singlestoredb/utils/config.py +71 -12
  99. singlestoredb/utils/convert_rows.py +0 -2
  100. singlestoredb/utils/debug.py +13 -0
  101. singlestoredb/utils/mogrify.py +151 -0
  102. singlestoredb/utils/results.py +4 -3
  103. singlestoredb/utils/xdict.py +12 -12
  104. singlestoredb-1.0.3.dist-info/METADATA +139 -0
  105. singlestoredb-1.0.3.dist-info/RECORD +112 -0
  106. {singlestoredb-0.3.3.dist-info → singlestoredb-1.0.3.dist-info}/WHEEL +1 -1
  107. singlestoredb-1.0.3.dist-info/entry_points.txt +2 -0
  108. singlestoredb/drivers/__init__.py +0 -46
  109. singlestoredb/drivers/base.py +0 -200
  110. singlestoredb/drivers/cymysql.py +0 -40
  111. singlestoredb/drivers/http.py +0 -49
  112. singlestoredb/drivers/mariadb.py +0 -42
  113. singlestoredb/drivers/mysqlconnector.py +0 -51
  114. singlestoredb/drivers/mysqldb.py +0 -62
  115. singlestoredb/drivers/pymysql.py +0 -39
  116. singlestoredb/drivers/pyodbc.py +0 -67
  117. singlestoredb/http.py +0 -794
  118. singlestoredb-0.3.3.dist-info/METADATA +0 -105
  119. singlestoredb-0.3.3.dist-info/RECORD +0 -46
  120. {singlestoredb-0.3.3.dist-info → singlestoredb-1.0.3.dist-info}/LICENSE +0 -0
  121. {singlestoredb-0.3.3.dist-info → singlestoredb-1.0.3.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,92 @@
1
+ import struct
2
+ from typing import Any
3
+
4
+ from ..exceptions import DatabaseError # noqa: F401
5
+ from ..exceptions import DataError # noqa: F401
6
+ from ..exceptions import Error # noqa: F401
7
+ from ..exceptions import IntegrityError # noqa: F401
8
+ from ..exceptions import InterfaceError # noqa: F401
9
+ from ..exceptions import InternalError # noqa: F401
10
+ from ..exceptions import MySQLError # noqa: F401
11
+ from ..exceptions import NotSupportedError # noqa: F401
12
+ from ..exceptions import OperationalError # noqa: F401
13
+ from ..exceptions import ProgrammingError # noqa: F401
14
+ from ..exceptions import Warning # noqa: F401
15
+ from .constants import ER
16
+
17
+
18
+ error_map = {}
19
+
20
+
21
+ def _map_error(exc: Any, *errors: int) -> None:
22
+ for error in errors:
23
+ error_map[error] = exc
24
+
25
+
26
+ _map_error(
27
+ ProgrammingError,
28
+ ER.DB_CREATE_EXISTS,
29
+ ER.SYNTAX_ERROR,
30
+ ER.PARSE_ERROR,
31
+ ER.NO_SUCH_TABLE,
32
+ ER.WRONG_DB_NAME,
33
+ ER.WRONG_TABLE_NAME,
34
+ ER.FIELD_SPECIFIED_TWICE,
35
+ ER.INVALID_GROUP_FUNC_USE,
36
+ ER.UNSUPPORTED_EXTENSION,
37
+ ER.TABLE_MUST_HAVE_COLUMNS,
38
+ ER.CANT_DO_THIS_DURING_AN_TRANSACTION,
39
+ ER.WRONG_DB_NAME,
40
+ ER.WRONG_COLUMN_NAME,
41
+ )
42
+ _map_error(
43
+ DataError,
44
+ ER.WARN_DATA_TRUNCATED,
45
+ ER.WARN_NULL_TO_NOTNULL,
46
+ ER.WARN_DATA_OUT_OF_RANGE,
47
+ ER.NO_DEFAULT,
48
+ ER.PRIMARY_CANT_HAVE_NULL,
49
+ ER.DATA_TOO_LONG,
50
+ ER.DATETIME_FUNCTION_OVERFLOW,
51
+ ER.TRUNCATED_WRONG_VALUE_FOR_FIELD,
52
+ ER.ILLEGAL_VALUE_FOR_TYPE,
53
+ )
54
+ _map_error(
55
+ IntegrityError,
56
+ ER.DUP_ENTRY,
57
+ ER.NO_REFERENCED_ROW,
58
+ ER.NO_REFERENCED_ROW_2,
59
+ ER.ROW_IS_REFERENCED,
60
+ ER.ROW_IS_REFERENCED_2,
61
+ ER.CANNOT_ADD_FOREIGN,
62
+ ER.BAD_NULL_ERROR,
63
+ )
64
+ _map_error(
65
+ NotSupportedError,
66
+ ER.WARNING_NOT_COMPLETE_ROLLBACK,
67
+ ER.NOT_SUPPORTED_YET,
68
+ ER.FEATURE_DISABLED,
69
+ ER.UNKNOWN_STORAGE_ENGINE,
70
+ )
71
+ _map_error(
72
+ OperationalError,
73
+ ER.DBACCESS_DENIED_ERROR,
74
+ ER.ACCESS_DENIED_ERROR,
75
+ ER.CON_COUNT_ERROR,
76
+ ER.TABLEACCESS_DENIED_ERROR,
77
+ ER.COLUMNACCESS_DENIED_ERROR,
78
+ ER.CONSTRAINT_FAILED,
79
+ ER.LOCK_DEADLOCK,
80
+ )
81
+
82
+
83
+ del _map_error, ER
84
+
85
+
86
+ def raise_mysql_exception(data: bytes) -> Exception:
87
+ errno = struct.unpack('<h', data[1:3])[0]
88
+ errval = data[9:].decode('utf-8', 'replace')
89
+ errorclass = error_map.get(errno)
90
+ if errorclass is None:
91
+ errorclass = InternalError if errno < 1000 else OperationalError
92
+ raise errorclass(errno, errval)
@@ -0,0 +1,20 @@
1
+ import configparser
2
+ from typing import Any
3
+
4
+
5
+ class Parser(configparser.RawConfigParser):
6
+
7
+ def __init__(self, **kwargs: Any) -> None:
8
+ kwargs['allow_no_value'] = True
9
+ configparser.RawConfigParser.__init__(self, **kwargs)
10
+
11
+ def __remove_quotes(self, value: str) -> str:
12
+ quotes = ["'", '"']
13
+ for quote in quotes:
14
+ if len(value) >= 2 and value[0] == value[-1] == quote:
15
+ return value[1:-1]
16
+ return value
17
+
18
+ def get(self, section: str, option: str) -> str: # type: ignore
19
+ value = configparser.RawConfigParser.get(self, section, option)
20
+ return self.__remove_quotes(value)
@@ -0,0 +1,388 @@
1
+ # type: ignore
2
+ # Python implementation of low level MySQL client-server protocol
3
+ # http://dev.mysql.com/doc/internals/en/client-server-protocol.html
4
+ import struct
5
+ import sys
6
+
7
+ from . import err
8
+ from ..config import get_option
9
+ from ..utils.results import Description
10
+ from .charset import MBLENGTH
11
+ from .constants import FIELD_TYPE
12
+ from .constants import SERVER_STATUS
13
+
14
+
15
+ DEBUG = get_option('debug.connection')
16
+
17
+ NULL_COLUMN = 251
18
+ UNSIGNED_CHAR_COLUMN = 251
19
+ UNSIGNED_SHORT_COLUMN = 252
20
+ UNSIGNED_INT24_COLUMN = 253
21
+ UNSIGNED_INT64_COLUMN = 254
22
+
23
+
24
+ def dump_packet(data): # pragma: no cover
25
+
26
+ def printable(data):
27
+ if 32 <= data < 127:
28
+ return chr(data)
29
+ return '.'
30
+
31
+ try:
32
+ print('packet length:', len(data))
33
+ for i in range(1, 7):
34
+ f = sys._getframe(i)
35
+ print('call[%d]: %s (line %d)' % (i, f.f_code.co_name, f.f_lineno))
36
+ print('-' * 66)
37
+ except ValueError:
38
+ pass
39
+ dump_data = [data[i: i + 16] for i in range(0, min(len(data), 256), 16)]
40
+ for d in dump_data:
41
+ print(
42
+ ' '.join('{:02X}'.format(x) for x in d)
43
+ + ' ' * (16 - len(d))
44
+ + ' ' * 2
45
+ + ''.join(printable(x) for x in d),
46
+ )
47
+ print('-' * 66)
48
+ print()
49
+
50
+
51
+ class MysqlPacket:
52
+ """
53
+ Representation of a MySQL response packet.
54
+
55
+ Provides an interface for reading/parsing the packet results.
56
+
57
+ """
58
+
59
+ __slots__ = ('_position', '_data')
60
+
61
+ def __init__(self, data, encoding):
62
+ self._position = 0
63
+ self._data = data
64
+
65
+ def get_all_data(self):
66
+ return self._data
67
+
68
+ def read(self, size):
69
+ """Read the first 'size' bytes in packet and advance cursor past them."""
70
+ result = self._data[self._position: (self._position + size)]
71
+ if len(result) != size:
72
+ error = (
73
+ 'Result length not requested length:\n'
74
+ 'Expected=%s. Actual=%s. Position: %s. Data Length: %s'
75
+ % (size, len(result), self._position, len(self._data))
76
+ )
77
+ if DEBUG:
78
+ print(error)
79
+ self.dump()
80
+ raise AssertionError(error)
81
+ self._position += size
82
+ return result
83
+
84
+ def read_all(self):
85
+ """
86
+ Read all remaining data in the packet.
87
+
88
+ (Subsequent read() will return errors.)
89
+
90
+ """
91
+ result = self._data[self._position:]
92
+ self._position = None # ensure no subsequent read()
93
+ return result
94
+
95
+ def advance(self, length):
96
+ """Advance the cursor in data buffer ``length`` bytes."""
97
+ new_position = self._position + length
98
+ if new_position < 0 or new_position > len(self._data):
99
+ raise Exception(
100
+ 'Invalid advance amount (%s) for cursor. '
101
+ 'Position=%s' % (length, new_position),
102
+ )
103
+ self._position = new_position
104
+
105
+ def rewind(self, position=0):
106
+ """Set the position of the data buffer cursor to 'position'."""
107
+ if position < 0 or position > len(self._data):
108
+ raise Exception('Invalid position to rewind cursor to: %s.' % position)
109
+ self._position = position
110
+
111
+ def get_bytes(self, position, length=1):
112
+ """
113
+ Get 'length' bytes starting at 'position'.
114
+
115
+ Position is start of payload (first four packet header bytes are not
116
+ included) starting at index '0'.
117
+
118
+ No error checking is done. If requesting outside end of buffer
119
+ an empty string (or string shorter than 'length') may be returned!
120
+
121
+ """
122
+ return self._data[position: (position + length)]
123
+
124
+ def read_uint8(self):
125
+ result = self._data[self._position]
126
+ self._position += 1
127
+ return result
128
+
129
+ def read_uint16(self):
130
+ result = struct.unpack_from('<H', self._data, self._position)[0]
131
+ self._position += 2
132
+ return result
133
+
134
+ def read_uint24(self):
135
+ low, high = struct.unpack_from('<HB', self._data, self._position)
136
+ self._position += 3
137
+ return low + (high << 16)
138
+
139
+ def read_uint32(self):
140
+ result = struct.unpack_from('<I', self._data, self._position)[0]
141
+ self._position += 4
142
+ return result
143
+
144
+ def read_uint64(self):
145
+ result = struct.unpack_from('<Q', self._data, self._position)[0]
146
+ self._position += 8
147
+ return result
148
+
149
+ def read_string(self):
150
+ end_pos = self._data.find(b'\0', self._position)
151
+ if end_pos < 0:
152
+ return None
153
+ result = self._data[self._position: end_pos]
154
+ self._position = end_pos + 1
155
+ return result
156
+
157
+ def read_length_encoded_integer(self):
158
+ """
159
+ Read a 'Length Coded Binary' number from the data buffer.
160
+
161
+ Length coded numbers can be anywhere from 1 to 9 bytes depending
162
+ on the value of the first byte.
163
+
164
+ """
165
+ c = self.read_uint8()
166
+ if c == NULL_COLUMN:
167
+ return None
168
+ if c < UNSIGNED_CHAR_COLUMN:
169
+ return c
170
+ elif c == UNSIGNED_SHORT_COLUMN:
171
+ return self.read_uint16()
172
+ elif c == UNSIGNED_INT24_COLUMN:
173
+ return self.read_uint24()
174
+ elif c == UNSIGNED_INT64_COLUMN:
175
+ return self.read_uint64()
176
+
177
+ def read_length_coded_string(self):
178
+ """
179
+ Read a 'Length Coded String' from the data buffer.
180
+
181
+ A 'Length Coded String' consists first of a length coded
182
+ (unsigned, positive) integer represented in 1-9 bytes followed by
183
+ that many bytes of binary data. (For example "cat" would be "3cat".)
184
+
185
+ """
186
+ length = self.read_length_encoded_integer()
187
+ if length is None:
188
+ return None
189
+ return self.read(length)
190
+
191
+ def read_struct(self, fmt):
192
+ s = struct.Struct(fmt)
193
+ result = s.unpack_from(self._data, self._position)
194
+ self._position += s.size
195
+ return result
196
+
197
+ def is_ok_packet(self):
198
+ # https://dev.mysql.com/doc/internals/en/packet-OK_Packet.html
199
+ return self._data[0] == 0 and len(self._data) >= 7
200
+
201
+ def is_eof_packet(self):
202
+ # http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-EOF_Packet
203
+ # Caution: \xFE may be LengthEncodedInteger.
204
+ # If \xFE is LengthEncodedInteger header, 8bytes followed.
205
+ return self._data[0] == 0xFE and len(self._data) < 9
206
+
207
+ def is_auth_switch_request(self):
208
+ # http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest
209
+ return self._data[0] == 0xFE
210
+
211
+ def is_extra_auth_data(self):
212
+ # https://dev.mysql.com/doc/internals/en/successful-authentication.html
213
+ return self._data[0] == 1
214
+
215
+ def is_resultset_packet(self):
216
+ field_count = self._data[0]
217
+ return 1 <= field_count <= 250
218
+
219
+ def is_load_local_packet(self):
220
+ return self._data[0] == 0xFB
221
+
222
+ def is_error_packet(self):
223
+ return self._data[0] == 0xFF
224
+
225
+ def check_error(self):
226
+ if self.is_error_packet():
227
+ self.raise_for_error()
228
+
229
+ def raise_for_error(self):
230
+ self.rewind()
231
+ self.advance(1) # field_count == error (we already know that)
232
+ errno = self.read_uint16()
233
+ if DEBUG:
234
+ print('errno =', errno)
235
+ err.raise_mysql_exception(self._data)
236
+
237
+ def dump(self):
238
+ dump_packet(self._data)
239
+
240
+
241
+ class FieldDescriptorPacket(MysqlPacket):
242
+ """
243
+ A MysqlPacket that represents a specific column's metadata in the result.
244
+
245
+ Parsing is automatically done and the results are exported via public
246
+ attributes on the class such as: db, table_name, name, length, type_code.
247
+
248
+ """
249
+
250
+ def __init__(self, data, encoding):
251
+ MysqlPacket.__init__(self, data, encoding)
252
+ self._parse_field_descriptor(encoding)
253
+
254
+ def _parse_field_descriptor(self, encoding):
255
+ """
256
+ Parse the 'Field Descriptor' (Metadata) packet.
257
+
258
+ This is compatible with MySQL 4.1+ (not compatible with MySQL 4.0).
259
+
260
+ """
261
+ self.catalog = self.read_length_coded_string()
262
+ self.db = self.read_length_coded_string()
263
+ self.table_name = self.read_length_coded_string().decode(encoding)
264
+ self.org_table = self.read_length_coded_string().decode(encoding)
265
+ self.name = self.read_length_coded_string().decode(encoding)
266
+ self.org_name = self.read_length_coded_string().decode(encoding)
267
+ (
268
+ self.charsetnr,
269
+ self.length,
270
+ self.type_code,
271
+ self.flags,
272
+ self.scale,
273
+ ) = self.read_struct('<xHIBHBxx')
274
+ # 'default' is a length coded binary and is still in the buffer?
275
+ # not used for normal result sets...
276
+
277
+ def description(self):
278
+ """Provides a 7-item tuple compatible with the Python PEP249 DB Spec."""
279
+ return Description(
280
+ self.name,
281
+ self.type_code,
282
+ None, # TODO: display_length; should this be self.length?
283
+ self.get_column_length(), # 'internal_size'
284
+ self.get_column_length(), # 'precision' # TODO: why!?!?
285
+ self.scale,
286
+ self.flags % 2 == 0,
287
+ self.flags,
288
+ self.charsetnr,
289
+ )
290
+
291
+ def get_column_length(self):
292
+ if self.type_code == FIELD_TYPE.VAR_STRING:
293
+ mblen = MBLENGTH.get(self.charsetnr, 1)
294
+ return self.length // mblen
295
+ return self.length
296
+
297
+ def __str__(self):
298
+ return '%s %r.%r.%r, type=%s, flags=%x, charsetnr=%s' % (
299
+ self.__class__,
300
+ self.db,
301
+ self.table_name,
302
+ self.name,
303
+ self.type_code,
304
+ self.flags,
305
+ self.charsetnr,
306
+ )
307
+
308
+
309
+ class OKPacketWrapper:
310
+ """
311
+ OK Packet Wrapper.
312
+
313
+ It uses an existing packet object, and wraps around it, exposing
314
+ useful variables while still providing access to the original packet
315
+ objects variables and methods.
316
+
317
+ """
318
+
319
+ def __init__(self, from_packet):
320
+ if not from_packet.is_ok_packet():
321
+ raise ValueError(
322
+ 'Cannot create '
323
+ + str(self.__class__.__name__)
324
+ + ' object from invalid packet type',
325
+ )
326
+
327
+ self.packet = from_packet
328
+ self.packet.advance(1)
329
+
330
+ self.affected_rows = self.packet.read_length_encoded_integer()
331
+ self.insert_id = self.packet.read_length_encoded_integer()
332
+ self.server_status, self.warning_count = self.read_struct('<HH')
333
+ self.message = self.packet.read_all()
334
+ self.has_next = self.server_status & SERVER_STATUS.SERVER_MORE_RESULTS_EXISTS
335
+
336
+ def __getattr__(self, key):
337
+ return getattr(self.packet, key)
338
+
339
+
340
+ class EOFPacketWrapper:
341
+ """
342
+ EOF Packet Wrapper.
343
+
344
+ It uses an existing packet object, and wraps around it, exposing
345
+ useful variables while still providing access to the original packet
346
+ objects variables and methods.
347
+
348
+ """
349
+
350
+ def __init__(self, from_packet):
351
+ if not from_packet.is_eof_packet():
352
+ raise ValueError(
353
+ f"Cannot create '{self.__class__}' object from invalid packet type",
354
+ )
355
+
356
+ self.packet = from_packet
357
+ self.warning_count, self.server_status = self.packet.read_struct('<xhh')
358
+ if DEBUG:
359
+ print('server_status=', self.server_status)
360
+ self.has_next = self.server_status & SERVER_STATUS.SERVER_MORE_RESULTS_EXISTS
361
+
362
+ def __getattr__(self, key):
363
+ return getattr(self.packet, key)
364
+
365
+
366
+ class LoadLocalPacketWrapper:
367
+ """
368
+ Load Local Packet Wrapper.
369
+
370
+ It uses an existing packet object, and wraps around it, exposing useful
371
+ variables while still providing access to the original packet
372
+ objects variables and methods.
373
+
374
+ """
375
+
376
+ def __init__(self, from_packet):
377
+ if not from_packet.is_load_local_packet():
378
+ raise ValueError(
379
+ f"Cannot create '{self.__class__}' object from invalid packet type",
380
+ )
381
+
382
+ self.packet = from_packet
383
+ self.filename = self.packet.get_all_data()[1:]
384
+ if DEBUG:
385
+ print('filename=', self.filename)
386
+
387
+ def __getattr__(self, key):
388
+ return getattr(self.packet, key)
@@ -0,0 +1,19 @@
1
+ # type: ignore
2
+ # Sorted by alphabetical order
3
+ from singlestoredb.mysql.tests.test_basic import * # noqa: F403, F401
4
+ from singlestoredb.mysql.tests.test_connection import * # noqa: F403, F401
5
+ from singlestoredb.mysql.tests.test_converters import * # noqa: F403, F401
6
+ from singlestoredb.mysql.tests.test_cursor import * # noqa: F403, F401
7
+ from singlestoredb.mysql.tests.test_DictCursor import * # noqa: F403, F401
8
+ from singlestoredb.mysql.tests.test_err import * # noqa: F403, F401
9
+ from singlestoredb.mysql.tests.test_issues import * # noqa: F403, F401
10
+ from singlestoredb.mysql.tests.test_load_local import * # noqa: F403, F401
11
+ from singlestoredb.mysql.tests.test_nextset import * # noqa: F403, F401
12
+ from singlestoredb.mysql.tests.test_optionfile import * # noqa: F403, F401
13
+ from singlestoredb.mysql.tests.test_SSCursor import * # noqa: F403, F401
14
+ from singlestoredb.mysql.tests.thirdparty import * # noqa: F403, F401
15
+
16
+ if __name__ == '__main__':
17
+ import unittest
18
+
19
+ unittest.main()
@@ -0,0 +1,126 @@
1
+ # type: ignore
2
+ import json
3
+ import os
4
+ import platform
5
+ import re
6
+ import unittest
7
+ import warnings
8
+
9
+ import singlestoredb.mysql as sv
10
+ from singlestoredb.connection import build_params
11
+
12
+ DBNAME_BASE = 'singlestoredb__test_%s_%s_%s_%s_' % \
13
+ (
14
+ *platform.python_version_tuple()[:2],
15
+ platform.system(), platform.machine(),
16
+ )
17
+
18
+
19
+ class PyMySQLTestCase(unittest.TestCase):
20
+ # You can specify your test environment creating a file named
21
+ # "databases.json" or editing the `databases` variable below.
22
+ fname = os.path.join(os.path.dirname(__file__), 'databases.json')
23
+ if os.path.exists(fname):
24
+ with open(fname) as f:
25
+ databases = json.load(f)
26
+ else:
27
+ params = build_params()
28
+ databases = [
29
+ {
30
+ 'host': params['host'],
31
+ 'port': params['port'],
32
+ 'user': params['user'],
33
+ 'passwd': params['password'],
34
+ 'database': DBNAME_BASE + '1',
35
+ 'use_unicode': True,
36
+ 'local_infile': True,
37
+ 'buffered': params['buffered'],
38
+ },
39
+ {
40
+ 'host': params['host'], 'user': params['user'],
41
+ 'port': params['port'], 'passwd': params['password'],
42
+ 'database': DBNAME_BASE + '2',
43
+ 'buffered': params['buffered'],
44
+ },
45
+ ]
46
+
47
+ def mysql_server_is(self, conn, version_tuple):
48
+ """
49
+ Return True if the given connection is on the version given or greater.
50
+
51
+ This only checks the server version string provided when the
52
+ connection is established, therefore any check for a version tuple
53
+ greater than (5, 5, 5) will always fail on MariaDB, as it always
54
+ starts with 5.5.5, e.g. 5.5.5-10.7.1-MariaDB-1:10.7.1+maria~focal.
55
+
56
+ Examples
57
+ --------
58
+
59
+ if self.mysql_server_is(conn, (5, 6, 4)):
60
+ # do something for MySQL 5.6.4 and above
61
+
62
+ """
63
+ server_version = conn.get_server_info()
64
+ server_version_tuple = tuple(
65
+ (int(dig) if dig is not None else 0)
66
+ for dig in re.match(r'(\d+)\.(\d+)\.(\d+)', server_version).group(1, 2, 3)
67
+ )
68
+ return server_version_tuple >= version_tuple
69
+
70
+ _connections = None
71
+
72
+ @property
73
+ def connections(self):
74
+ if self._connections is None:
75
+ self._connections = []
76
+ for params in self.databases:
77
+ conn = sv.connect(**params)
78
+ self._connections.append(conn)
79
+ self.addCleanup(self._teardown_connections)
80
+ return self._connections
81
+
82
+ def connect(self, **params):
83
+ p = self.databases[0].copy()
84
+ p.update(params)
85
+ conn = sv.connect(**p)
86
+
87
+ @self.addCleanup
88
+ def teardown():
89
+ if conn.open:
90
+ conn.close()
91
+
92
+ return conn
93
+
94
+ def _teardown_connections(self):
95
+ if self._connections:
96
+ for connection in self._connections:
97
+ if connection.open:
98
+ connection.close()
99
+ self._connections = None
100
+
101
+ def safe_create_table(self, connection, tablename, ddl, cleanup=True):
102
+ """
103
+ Create a table.
104
+
105
+ Ensures any existing version of that table is first dropped.
106
+
107
+ Also adds a cleanup rule to drop the table after the test
108
+ completes.
109
+
110
+ """
111
+ cursor = connection.cursor()
112
+
113
+ with warnings.catch_warnings():
114
+ warnings.simplefilter('ignore')
115
+ cursor.execute('drop table if exists `%s`' % (tablename,))
116
+ cursor.execute(ddl)
117
+ cursor.close()
118
+ if cleanup:
119
+ self.addCleanup(self.drop_table, connection, tablename)
120
+
121
+ def drop_table(self, connection, tablename):
122
+ cursor = connection.cursor()
123
+ with warnings.catch_warnings():
124
+ warnings.simplefilter('ignore')
125
+ cursor.execute('drop table if exists `%s`' % (tablename,))
126
+ cursor.close()
@@ -0,0 +1,37 @@
1
+ import platform
2
+
3
+ import singlestoredb.mysql as sv
4
+ from singlestoredb.connection import build_params
5
+
6
+
7
+ DBNAME_BASE = 'singlestoredb__test_%s_%s_%s_%s_' % \
8
+ (
9
+ *platform.python_version_tuple()[:2],
10
+ platform.system(), platform.machine(),
11
+ )
12
+
13
+
14
+ def pytest_sessionstart() -> None:
15
+ params = build_params()
16
+ conn = sv.connect( # type: ignore
17
+ host=params['host'], user=params['user'],
18
+ passwd=params['password'], port=params['port'],
19
+ buffered=params['buffered'],
20
+ )
21
+ cur = conn.cursor()
22
+ cur.execute(f'CREATE DATABASE IF NOT EXISTS {DBNAME_BASE}1')
23
+ cur.execute(f'CREATE DATABASE IF NOT EXISTS {DBNAME_BASE}2')
24
+ conn.close()
25
+
26
+
27
+ def pytest_sessionfinish() -> None:
28
+ params = build_params()
29
+ conn = sv.connect( # type: ignore
30
+ host=params['host'], user=params['user'],
31
+ passwd=params['password'], port=params['port'],
32
+ buffered=params['buffered'],
33
+ )
34
+ cur = conn.cursor()
35
+ cur.execute(f'DROP DATABASE {DBNAME_BASE}1')
36
+ cur.execute(f'DROP DATABASE {DBNAME_BASE}2')
37
+ conn.close()