1
|
1 |
|
from configparser import ConfigParser |
2
|
1 |
|
from typing import Dict, Optional, Union |
3
|
|
|
|
4
|
1 |
|
from pystratum_backend.StratumIO import StratumIO |
5
|
|
|
|
6
|
1 |
|
from pystratum_mysql.MySqlDefaultConnector import MySqlDefaultConnector |
7
|
1 |
|
from pystratum_mysql.MySqlMetadataDataLayer import MySqlMetadataDataLayer |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
class MySqlWorker: |
11
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
12
|
1 |
|
def __init__(self, io: StratumIO, config: ConfigParser): |
13
|
|
|
""" |
14
|
|
|
Object constructor. |
15
|
|
|
|
16
|
|
|
:param io: The output decorator. |
17
|
|
|
""" |
18
|
|
|
|
19
|
1 |
|
self._io: StratumIO = io |
20
|
|
|
""" |
21
|
|
|
The output decorator. |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
self._config: ConfigParser = config |
25
|
|
|
""" |
26
|
|
|
The configuration object. |
27
|
|
|
""" |
28
|
|
|
|
29
|
1 |
|
self._dl = MySqlMetadataDataLayer(io, MySqlDefaultConnector(self.__read_configuration_file())) |
30
|
1 |
|
""" |
31
|
|
|
The metadata layer. |
32
|
|
|
""" |
33
|
|
|
|
34
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
35
|
1 |
|
def connect(self) -> None: |
36
|
|
|
""" |
37
|
|
|
Connects to the database. |
38
|
|
|
""" |
39
|
1 |
|
self._dl.connect() |
40
|
|
|
|
41
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
42
|
1 |
|
def disconnect(self) -> None: |
43
|
|
|
""" |
44
|
|
|
Disconnects from the database. |
45
|
|
|
""" |
46
|
|
|
self._dl.disconnect() |
47
|
|
|
|
48
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
49
|
1 |
|
def __read_configuration_file(self) -> Dict[str, Union[str, int]]: |
50
|
|
|
""" |
51
|
|
|
Reads connections parameters from the configuration file. |
52
|
|
|
""" |
53
|
1 |
|
params = {'host': self.__get_option(self._config, 'database', 'host_name', fallback='localhost'), |
54
|
|
|
'user': self.__get_option(self._config, 'database', 'user'), |
55
|
|
|
'password': self.__get_option(self._config, 'database', 'password'), |
56
|
|
|
'database': self.__get_option(self._config, 'database', 'database'), |
57
|
|
|
'port': int(self.__get_option(self._config, 'database', 'port', fallback='3306')), |
58
|
|
|
'charset': self.__get_option(self._config, 'database', 'character_set_client', fallback='utf-8'), |
59
|
|
|
'collation': self.__get_option(self._config, 'database', 'collation_connection', |
60
|
|
|
fallback='utf8_general_ci'), |
61
|
|
|
'sql_mode': self.__get_option(self._config, 'database', 'sql_mode') |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return params |
65
|
|
|
|
66
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
67
|
1 |
|
@staticmethod |
68
|
1 |
|
def __get_option(config: ConfigParser, |
69
|
|
|
section: str, |
70
|
|
|
option: str, |
71
|
|
|
fallback: Optional[str] = None) -> str: |
72
|
|
|
""" |
73
|
|
|
Reads an option for a configuration file. |
74
|
|
|
|
75
|
|
|
:param config: The main config file. |
76
|
|
|
:param section: The name of the section op the option. |
77
|
|
|
:param option: The name of the option. |
78
|
|
|
:param fallback: The fallback value of the option if it is not set in either configuration files. |
79
|
|
|
""" |
80
|
|
|
return_value = config.get(section, option, fallback=fallback) |
81
|
|
|
|
82
|
|
|
if fallback is None and return_value is None: |
83
|
|
|
raise KeyError("Option '{0!s}' is not found in section '{1!s}'.".format(option, section)) |
84
|
1 |
|
|
85
|
|
|
return return_value |
86
|
1 |
|
|
87
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
88
|
|
|
|