/
connections.py
689 lines (554 loc) · 22.3 KB
/
connections.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#
# Copyright (C) 2020-2021 Georg Richter and MariaDB Corporation AB
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
# You should have received a copy of the GNU Library General Public
# License along with this library; if not see <http://www.gnu.org/licenses>
# or write to the Free Software Foundation, Inc.,
# 51 Franklin St., Fifth Floor, Boston, MA 02110, USA
#
import mariadb
import socket
import mariadb.cursors
from mariadb.constants import STATUS, TPC_STATE, INFO
from packaging import version
_DEFAULT_CHARSET = "utf8mb4"
_DEFAULT_COLLATION = "utf8mb4_general_ci"
_MAX_TPC_XID_SIZE = 64
class Connection(mariadb._mariadb.connection):
"""
MariaDB Connector/Python Connection Object
Handles the connection to a MariaDB or MySQL database server.
It encapsulates a database session.
Connections are created using the method mariadb.connect()
"""
def _check_closed(self):
if self._closed:
raise mariadb.ProgrammingError("Invalid connection or "
"not connected")
def __init__(self, *args, **kwargs):
"""
Establishes a connection to a database server and returns a connection
object.
"""
self._socket = None
self._used = 0
self._last_executed_statement = None
self._socket = None
self.__pool = None
self.__last_used = 0
self.tpc_state = TPC_STATE.NONE
self._xid = None
autocommit = kwargs.pop("autocommit", False)
reconnect = kwargs.pop("reconnect", False)
self._converter = kwargs.pop("converter", None)
# if host contains a connection string or multiple hosts,
# we need to check if it's supported by Connector/C
if "host" in kwargs:
host = kwargs.get("host")
if version.Version(mariadb.mariadbapi_version) <\
version.Version('3.3.0') and ',' in host:
raise mariadb.ProgrammingError("Host failover list requires "
"MariaDB Connector/C 3.3.0 "
"or newer")
# compatibility feature: if SSL is provided as a dictionary,
# we will map it's content
if "ssl" in kwargs and not isinstance(kwargs["ssl"], bool):
ssl = kwargs.pop("ssl", None)
for key in ["ca", "cert", "capath", "key", "cipher"]:
if key in ssl:
kwargs["ssl_%s" % key] = ssl[key]
kwargs["ssl"] = True
super().__init__(*args, **kwargs)
self.autocommit = autocommit
self.auto_reconnect = reconnect
def cursor(self, cursorclass=mariadb.cursors.Cursor, **kwargs):
"""
Returns a new cursor object for the current connection.
If no cursorclass was specified, a cursor with default mariadb.Cursor
class will be created.
Optional keyword parameters:
- buffered = True
If set to False the result will be unbuffered, which means before
executing another statement with the same connection the entire
result set must be fetched.
Please note that the default was False for MariaDB Connector/Python
versions < 1.1.0.
- dictionary = False
Return fetch values as dictionary.
- named_tuple = False
Return fetch values as named tuple. This feature exists for
compatibility reasons and should be avoided due to possible
inconsistency.
- cursor_type = CURSOR.NONE
If cursor_type is set to CURSOR.READ_ONLY, a cursor is opened
for the statement invoked with cursors execute() method.
- prepared = False
When set to True cursor will remain in prepared state after the first
execute() method was called. Further calls to execute() method will
ignore the sql statement.
- binary = False
Always execute statement in MariaDB client/server binary protocol.
In versions prior to 1.1.0 results were unbuffered by default,
which means before executing another statement with the same
connection the entire result set must be fetched.
fetch* methods of the cursor class by default return result set values
as a tuple, unless named_tuple or dictionary was specified.
The latter one exists for compatibility reasons and should be avoided
due to possible inconsistency in case two or more fields in a result
set have the same name.
If cursor_type is set to CURSOR.READ_ONLY, a cursor is opened for
the statement invoked with cursors execute() method.
"""
self._check_closed()
cursor = cursorclass(self, **kwargs)
if not isinstance(cursor, mariadb._mariadb.cursor):
raise mariadb.ProgrammingError("%s is not an instance of "
"mariadb.cursor" % cursor)
return cursor
def close(self):
self._check_closed()
if self._Connection__pool:
self._Connection__pool._close_connection(self)
else:
super().close()
def __enter__(self):
self._check_closed()
"Returns a copy of the connection."
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self._check_closed()
"Closes connection."
self.close()
def commit(self):
"""
Commit any pending transaction to the database.
"""
self._check_closed()
if self.tpc_state > TPC_STATE.NONE:
raise mariadb.ProgrammingError("commit() is not allowed if "
"a TPC transaction is active")
self._execute_command("COMMIT")
self._read_response()
def rollback(self):
"""
Causes the database to roll back to the start of any pending
transaction
Closing a connection without committing the changes first will
cause an implicit rollback to be performed.
Note that rollback() will not work as expected if autocommit mode
was set to True or the storage engine does not support transactions."
"""
self._check_closed()
if self.tpc_state > TPC_STATE.NONE:
raise mariadb.ProgrammingError("rollback() is not allowed if a "
"TPC transaction is active")
self._execute_command("ROLLBACK")
self._read_response()
def kill(self, id: int):
"""
This function is used to ask the server to kill a database connection
specified by the processid parameter.
The connection id can be be retrieved by SHOW PROCESSLIST sql command.
"""
self._check_closed()
if not isinstance(id, int):
raise mariadb.ProgrammingError("id must be of type int.")
stmt = "KILL %s" % id
self._execute_command(stmt)
self._read_response()
def begin(self):
"""
Start a new transaction which can be committed by .commit() method,
or cancelled by .rollback() method.
"""
self._check_closed()
self._execute_command("BEGIN")
self._read_response()
def select_db(self, new_db: str):
"""
Gets the default database for the current connection.
The default database can also be obtained or changed by database
attribute.
"""
self._check_closed()
self.database = new_db
def get_server_version(self):
"""
Returns a tuple representing the version of the connected server in
the following format: (MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
"""
return self.server_version_info
def show_warnings(self):
"""
Shows error, warning and note messages from last executed command.
"""
self._check_closed()
if (not self.warnings):
return None
cursor = self.cursor()
cursor.execute("SHOW WARNINGS")
ret = cursor.fetchall()
del cursor
return ret
class xid(tuple):
"""
xid(format_id: int, global_transaction_id: str, branch_qualifier: str)
Creates a transaction ID object suitable for passing to the .tpc_*()
methods of this connection.
Parameters:
- format_id: Format id. If not set default value `0` will be used.
- global_transaction_id: Global transaction qualifier, which must be
unique. The maximum length of the global transaction id is
limited to 64 characters.
- branch_qualifier: Branch qualifier which represents a local
transaction identifier. The maximum length of the branch qualifier
is limited to 64 characters.
"""
def __new__(self, format_id, transaction_id, branch_qualifier):
if not isinstance(format_id, int):
raise mariadb.ProgrammingError("argument 1 must be int, "
"not %s",
type(format_id).__name__)
if not isinstance(transaction_id, str):
raise mariadb.ProgrammingError("argument 2 must be str, "
"not %s",
type(transaction_id).__mane__)
if not isinstance(branch_qualifier, str):
raise mariadb.ProgrammingError("argument 3 must be str, "
"not %s",
type(transaction_id).__name__)
if len(transaction_id) > _MAX_TPC_XID_SIZE:
raise mariadb.ProgrammingError("Maximum length of "
"transaction_id exceeded.")
if len(branch_qualifier) > _MAX_TPC_XID_SIZE:
raise mariadb.ProgrammingError("Maximum length of "
"branch_qualifier exceeded.")
if format_id == 0:
format_id = 1
return super().__new__(self, (format_id,
transaction_id,
branch_qualifier))
def tpc_begin(self, xid):
"""
Parameter:
xid: xid object which was created by .xid() method of connection
class
Begins a TPC transaction with the given transaction ID xid.
This method should be called outside of a transaction
(i.e. nothing may have executed since the last .commit()
or .rollback()).
Furthermore, it is an error to call .commit() or .rollback() within
the TPC transaction. A ProgrammingError is raised, if the application
calls .commit() or .rollback() during an active TPC transaction.
"""
self._check_closed()
if type(xid).__name__ != "xid":
raise mariadb.ProgrammingError("argument 1 must be xid "
"not %s", type(xid).__name__)
stmt = "XA BEGIN '%s','%s',%s" % (xid[1], xid[2], xid[0])
try:
self._execute_command(stmt)
self._read_response()
except mariadb.Error:
raise
self.tpc_state = TPC_STATE.XID
self._xid = xid
def tpc_commit(self, xid=None):
"""
Optional parameter:"
xid: xid object which was created by .xid() method of connection class.
When called with no arguments, .tpc_commit() commits a TPC transaction
previously prepared with .tpc_prepare().
If .tpc_commit() is called prior to .tpc_prepare(), a single phase
commit is performed. A transaction manager may choose to do this if
only a single resource is participating in the global transaction.
When called with a transaction ID xid, the database commits the given
transaction. If an invalid transaction ID is provided,
a ProgrammingError will be raised.
This form should be called outside of a transaction, and
is intended for use in recovery."
"""
self._check_closed()
if not xid:
xid = self._xid
if self.tpc_state == TPC_STATE.NONE:
raise mariadb.ProgrammingError("Transaction not started.")
if xid is None and self.tpc_state != TPC_STATE.PREPARE:
raise mariadb.ProgrammingError("Transaction is not prepared.")
if xid and type(xid).__name__ != "xid":
raise mariadb.ProgrammingError("argument 1 must be xid "
"not %s" % type(xid).__name__)
if self.tpc_state < TPC_STATE.PREPARE:
stmt = "XA END '%s','%s',%s" % (xid[1], xid[2], xid[0])
self._execute_command(stmt)
try:
self._read_response()
except mariadb.Error:
self._xid = None
self.tpc_state = TPC_STATE.NONE
raise
stmt = "XA COMMIT '%s','%s',%s" % (xid[1], xid[2], xid[0])
if self.tpc_state < TPC_STATE.PREPARE:
stmt = stmt + " ONE PHASE"
try:
self._execute_command(stmt)
self._read_response()
except mariadb.Error:
self._xid = None
self.tpc_state = TPC_STATE.NONE
raise
# cleanup
self._xid = None
self.tpc_state = TPC_STATE.NONE
def tpc_prepare(self):
"""
Performs the first phase of a transaction started with .tpc_begin().
A ProgrammingError will be raised if this method was called outside of
a TPC transaction.
After calling .tpc_prepare(), no statements can be executed until
.tpc_commit() or .tpc_rollback() have been called.
"""
self._check_closed()
if self.tpc_state == TPC_STATE.NONE:
raise mariadb.ProgrammingError("Transaction not started.")
if self.tpc_state == TPC_STATE.PREPARE:
raise mariadb.ProgrammingError("Transaction is already in "
"prepared state.")
xid = self._xid
stmt = "XA END '%s','%s',%s" % (xid[1], xid[2], xid[0])
try:
self._execute_command(stmt)
self._read_response()
except mariadb.Error:
self._xid = None
self.tpc_state = TPC_STATE.NONE
raise
stmt = "XA PREPARE '%s','%s',%s" % (xid[1], xid[2], xid[0])
try:
self._execute_command(stmt)
self._read_response()
except mariadb.Error:
self._xid = None
self.tpc_state = TPC_STATE.NONE
raise
self.tpc_state = TPC_STATE.PREPARE
def tpc_rollback(self, xid=None):
"""
Parameter:
xid: xid object which was created by .xid() method of connection
class
Performs the first phase of a transaction started with .tpc_begin().
A ProgrammingError will be raised if this method outside of a TPC
transaction.
After calling .tpc_prepare(), no statements can be executed until
.tpc_commit() or .tpc_rollback() have been called.
"""
self._check_closed()
if self.tpc_state == TPC_STATE.NONE:
raise mariadb.ProgrammingError("Transaction not started.")
if xid and type(xid).__name__ != "xid":
raise mariadb.ProgrammingError("argument 1 must be xid "
"not %s" % type(xid).__name__)
if not xid:
xid = self._xid
if self.tpc_state < TPC_STATE.PREPARE:
stmt = "XA END '%s','%s',%s" % (xid[1], xid[2], xid[0])
self._execute_command(stmt)
try:
self._read_response()
except mariadb.Error:
self._xid = None
self.tpc_state = TPC_STATE.NONE
raise
stmt = "XA ROLLBACK '%s','%s',%s" % (xid[1], xid[2], xid[0])
try:
self._execute_command(stmt)
self._read_response()
except mariadb.Error:
self._xid = None
self.tpc_state = TPC_STATE.NONE
raise
self.tpc_state = TPC_STATE.PREPARE
def tpc_recover(self):
"""
Returns a list of pending transaction IDs suitable for use with
tpc_commit(xid) or .tpc_rollback(xid).
"""
self._check_closed()
cursor = self.cursor()
cursor.execute("XA RECOVER")
result = cursor.fetchall()
del cursor
return result
@property
def database(self):
"""Get default database for connection."""
self._check_closed()
return self._mariadb_get_info(INFO.SCHEMA)
@database.setter
def database(self, schema):
"""Set default database."""
self._check_closed()
try:
self._execute_command("USE %s" % str(schema))
self._read_response()
except mariadb.Error:
raise
@property
def user(self):
"""
Returns the user name for the current connection or empty
string if it can't be determined, e.g. when using socket
authentication.
"""
self._check_closed()
return self._mariadb_get_info(INFO.USER)
@property
def character_set(self):
"""
Client character set.
For MariaDB Connector/Python it is always utf8mb4.
"""
return _DEFAULT_CHARSET
@property
def client_capabilities(self):
"""Client capability flags."""
self._check_closed()
return self._mariadb_get_info(INFO.CLIENT_CAPABILITIES)
@property
def server_capabilities(self):
"""Server capability flags."""
self._check_closed()
return self._mariadb_get_info(INFO.SERVER_CAPABILITIES)
@property
def extended_server_capabilities(self):
"""
Extended server capability flags (only for MariaDB
database servers).
"""
self._check_closed()
return self._mariadb_get_info(INFO.EXTENDED_SERVER_CAPABILITIES)
@property
def server_port(self):
"""
Database server TCP/IP port. This value will be 0 in case of a unix
socket connection.
"""
self._check_closed()
return self._mariadb_get_info(INFO.PORT)
@property
def unix_socket(self):
"""Unix socket name."""
self._check_closed()
return self._mariadb_get_info(INFO.UNIX_SOCKET)
@property
def server_name(self):
"""Name or IP address of database server."""
self._check_closed()
return self._mariadb_get_info(INFO.HOST)
@property
def collation(self):
"""Client character set collation"""
return _DEFAULT_COLLATION
@property
def server_info(self):
"""Server version in alphanumerical format (str)"""
self._check_closed()
return self._mariadb_get_info(INFO.SERVER_VERSION)
@property
def tls_cipher(self):
"""TLS cipher suite if a secure connection is used."""
self._check_closed()
return self._mariadb_get_info(INFO.SSL_CIPHER)
@property
def tls_version(self):
"""TLS protocol version if a secure connection is used."""
self._check_closed()
return self._mariadb_get_info(INFO.TLS_VERSION)
@property
def server_status(self):
"""
Return server status flags
"""
self._check_closed()
return self._mariadb_get_info(INFO.SERVER_STATUS)
@property
def server_version(self):
"""
Server version in numerical format.
The form of the version number is
VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH
"""
self._check_closed()
return self._mariadb_get_info(INFO.SERVER_VERSION_ID)
@property
def server_version_info(self):
"""
Returns numeric version of connected database server in tuple format.
"""
self._check_closed()
version = self.server_version
return (int(version / 10000),
int((version % 10000) / 100),
version % 100)
@property
def autocommit(self):
"""
Toggles autocommit mode on or off for the current database connection.
Autocommit mode only affects operations on transactional table types.
Be aware that rollback() will not work, if autocommit mode was switched
on.
By default autocommit mode is set to False."
"""
self._check_closed()
return bool(self.server_status & STATUS.AUTOCOMMIT)
@autocommit.setter
def autocommit(self, mode):
self._check_closed()
if bool(mode) == self.autocommit:
return
try:
self._execute_command("SET AUTOCOMMIT=%s" % int(mode))
self._read_response()
except mariadb.Error:
raise
@property
def socket(self):
"""Returns the socket used for database connection"""
fno = self._get_socket()
if not self._socket:
self._socket = socket.socket(fileno=fno)
# in case of a possible reconnect, file descriptor has changed
elif fno != self._socket.fileno():
self._socket = socket.socket(fileno=fno)
return self._socket
@property
def open(self):
"""
Returns true if the connection is alive.
A ping command will be send to the server for this purpose,
which means this function might fail if there are still
non processed pending result sets.
"""
self._check_closed()
try:
self.ping()
except mariadb.Error:
return False
return True
# Aliases
character_set_name = character_set
@property
def thread_id(self):
"""
Alias for connection_id
"""
self._check_closed()
return self.connection_id