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