1
|
|
|
import pymssql |
2
|
|
|
from typing import Dict, Optional, Union, Any |
3
|
|
|
|
4
|
|
|
from pystratum_mssql.MsSqlConnector import MsSqlConnector |
5
|
|
|
from pystratum_mssql.MsSqlDataLayer import MsSqlDataLayer |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class MsSqlDefaultConnector(MsSqlConnector): |
9
|
|
|
""" |
10
|
|
|
Connects to a MySQL instance using user name and password. |
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
14
|
|
|
def __init__(self, params: Dict[str, Union[str, int]]): |
15
|
|
|
""" |
16
|
|
|
Object constructor. |
17
|
|
|
|
18
|
|
|
:param params: The connection parameters. |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
self._params: Dict[str, Union[str, int]] = params |
22
|
|
|
""" |
23
|
|
|
The connection parameters. |
24
|
|
|
""" |
25
|
|
|
|
26
|
|
|
self._connection: Optional[Any] = None |
27
|
|
|
""" |
28
|
|
|
The connection between Python and the MySQL instance. |
29
|
|
|
""" |
30
|
|
|
|
31
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
32
|
|
|
def connect(self) -> Any: |
33
|
|
|
""" |
34
|
|
|
Connects to the MySQL instance. |
35
|
|
|
""" |
36
|
|
|
self._connection = pymssql.connect(**self._params) |
37
|
|
|
|
38
|
|
|
# Install our own message handler. |
39
|
|
|
self._connection._conn.set_msghandler(MsSqlDataLayer.stratum_msg_handler) |
40
|
|
|
|
41
|
|
|
# Set the default settings. |
42
|
|
|
cursor = self._connection.cursor() |
43
|
|
|
cursor.execute('set nocount on') |
44
|
|
|
cursor.execute('set ansi_nulls on') |
45
|
|
|
cursor.close() |
46
|
|
|
|
47
|
|
|
return self._connection |
48
|
|
|
|
49
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
50
|
|
|
def disconnect(self) -> None: |
51
|
|
|
""" |
52
|
|
|
Disconnects from the MySQL instance. |
53
|
|
|
""" |
54
|
|
|
if self._connection: |
55
|
|
|
self._connection.close() |
56
|
|
|
self._connection = None |
57
|
|
|
|
58
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
59
|
|
|
|