Files
mariadb-connector-python/mariadb/connections.py
Georg Richter 2bd40ca1de Test fixes
2021-07-18 18:16:38 +02:00

95 lines
2.8 KiB
Python

#
# 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 time
from mariadb.constants import STATUS
_DEFAULT_CHARSET = "utf8mb4"
_DEFAULT_COLLATION = "utf8mb4_general_ci"
class Connection(mariadb._mariadb.connection):
"""MariaDB connection class"""
def __init__(self, *args, **kwargs):
self._socket= None
self.__in_use= 0
self.__pool = None
self.__last_used = 0
# self._autocommit= kwargs.pop("autocommit", True)
self._converter= kwargs.pop("converter", None)
super().__init__(*args, **kwargs)
def cursor(self, **kwargs):
cursor= mariadb.Cursor(self, **kwargs)
return cursor
def close(self):
if self._Connection__pool:
self._Connection__pool._close_connection(self)
else:
super().close()
def __enter__(self):
"Returns a copy of the connection."
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"Closes connection."
self.close()
def get_server_version(self):
return self.server_version_info
@property
def character_set(self):
"""Client character set."""
return _DEFAULT_CHARSET
@property
def collation(self):
"""Client character set collation"""
return _DEFAULT_COLLATION
@property
def server_status(self):
"""Returns server status flags."""
return super()._server_status
@property
def server_version_info(self):
version= self.server_version
return (int(version / 10000), int((version % 10000) / 100), version % 100)
@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