pystratum_mysql.MySqlDefaultConnector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 8
eloc 25
dl 0
loc 65
ccs 12
cts 24
cp 0.5
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A MySqlDefaultConnector.__init__() 0 14 1
A MySqlDefaultConnector.disconnect() 0 7 2
A MySqlDefaultConnector.connect() 0 7 1
A MySqlDefaultConnector.is_alive() 0 15 4
1 1
from typing import Dict, Optional, Union
2
3 1
from mysql.connector import MySQLConnection
4
5 1
from pystratum_mysql.MySqlConnector import MySqlConnector
6
7
8 1
class MySqlDefaultConnector(MySqlConnector):
9
    """
10
    Connects to a MySQL instance using username and password.
11
    """
12
13
    # ------------------------------------------------------------------------------------------------------------------
14 1
    def __init__(self, params: Dict[str, Union[str, int]]):
15
        """
16
        Object constructor.
17
        
18
        :param params: The connection parameters.
19
        """
20
21 1
        self._params: Dict[str, Union[str, int]] = params
22
        """
23
        The connection parameters.
24
        """
25
26 1
        self._connection: Optional[MySQLConnection] = None
27 1
        """
28
        The connection between Python and the MySQL instance.
29
        """
30
31
    # ------------------------------------------------------------------------------------------------------------------
32 1
    def connect(self) -> MySQLConnection:
33
        """
34
        Connects to the MySQL instance.
35
        """
36 1
        self._connection = MySQLConnection(**self._params)
37
38
        return self._connection
39
40
    # ------------------------------------------------------------------------------------------------------------------
41 1
    def disconnect(self) -> None:
42
        """
43
        Disconnects from the MySQL instance.
44
        """
45
        if self._connection:
46
            self._connection.close()
47
            self._connection = None
48
49
    # ------------------------------------------------------------------------------------------------------------------
50 1
    def is_alive(self) -> bool:
51
        """
52
        Returns whether Python is (still) connected to a MySQL or MariaDB instance.
53
        """
54
        is_alive = False
55
56
        if self._connection:
57
            try:
58
                result = self._connection.cmd_ping()
59
                if isinstance(result, dict):
60
                    is_alive = True
61
            except:
62
                pass
63
64
        return is_alive
65
66
# ----------------------------------------------------------------------------------------------------------------------
67