1
|
1 |
|
from abc import ABC |
2
|
1 |
|
from typing import Any, Dict, List |
3
|
|
|
|
4
|
1 |
|
from pystratum_common.wrapper.Wrapper import Wrapper |
5
|
|
|
|
6
|
|
|
|
7
|
1 |
|
class MySqlWrapper(Wrapper, ABC): |
8
|
|
|
""" |
9
|
|
|
Parent class for wrapper method generators for stored procedures and functions. |
10
|
|
|
""" |
11
|
|
|
|
12
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
13
|
1 |
|
def is_lob_parameter(self, parameters: List[Dict[str, Any]]) -> bool: |
14
|
|
|
""" |
15
|
|
|
Returns True of one of the parameters is a BLOB or CLOB. Otherwise, returns False. |
16
|
|
|
|
17
|
|
|
:param parameters: The parameters of a stored routine. |
18
|
|
|
""" |
19
|
|
|
has_lob = False |
20
|
|
|
|
21
|
|
|
lookup = {'bigint': False, |
22
|
|
|
'binary': False, |
23
|
|
|
'bit': False, |
24
|
|
|
'char': False, |
25
|
|
|
'date': False, |
26
|
|
|
'datetime': False, |
27
|
|
|
'decimal': False, |
28
|
|
|
'double': False, |
29
|
|
|
'enum': False, |
30
|
|
|
'float': False, |
31
|
|
|
'int': False, |
32
|
|
|
'mediumint': False, |
33
|
|
|
'set': False, |
34
|
|
|
'smallint': False, |
35
|
|
|
'time': False, |
36
|
|
|
'timestamp': False, |
37
|
|
|
'tinyint': False, |
38
|
|
|
'varbinary': False, |
39
|
|
|
'varchar': False, |
40
|
|
|
'year': False, |
41
|
|
|
|
42
|
|
|
'blob': True, |
43
|
|
|
'longblob': True, |
44
|
|
|
'longtext': True, |
45
|
|
|
'mediumblob': True, |
46
|
|
|
'mediumtext': True, |
47
|
|
|
'text': True, |
48
|
|
|
'tinyblob': True, |
49
|
|
|
'tinytext': True} |
50
|
|
|
|
51
|
|
|
if parameters: |
52
|
|
|
for parameter_info in parameters: |
53
|
|
|
if parameter_info['data_type'] in lookup: |
54
|
|
|
has_lob = lookup[parameter_info['data_type']] |
55
|
|
|
else: |
56
|
|
|
raise Exception("Unexpected date type '{0!s}'.".format(parameter_info['data_type'])) |
57
|
|
|
|
58
|
|
|
return has_lob |
59
|
|
|
|
60
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
61
|
|
|
def _generate_command(self, routine: Dict[str, Any]) -> str: |
62
|
|
|
""" |
63
|
1 |
|
Generates SQL statement for calling a stored routine. |
64
|
|
|
|
65
|
|
|
:param routine: Metadata of the stored routine. |
66
|
|
|
""" |
67
|
|
|
parameters = '' |
68
|
|
|
placeholders = '' |
69
|
|
|
|
70
|
|
|
execute = 'call' |
71
|
|
|
if routine['designation'] == 'function': |
72
|
|
|
execute = 'select' |
73
|
|
|
|
74
|
|
|
i = 0 |
75
|
|
|
l = 0 |
76
|
|
|
for parameter in routine['parameters']: |
77
|
|
|
re_type = self._get_parameter_format_specifier(parameter['data_type']) |
78
|
|
|
if parameters: |
79
|
|
|
parameters += ', ' |
80
|
|
|
placeholders += ', ' |
81
|
|
|
parameters += parameter['name'] |
82
|
|
|
placeholders += re_type |
83
|
|
|
i += 1 |
84
|
|
|
if not re_type == '?': |
85
|
|
|
l += 1 |
86
|
|
|
|
87
|
|
|
if l == 0: |
88
|
|
|
line = '"{0!s} {1!s}()"'.format(execute, routine['routine_name']) |
89
|
|
|
elif l >= 1: |
90
|
|
|
line = '"{0!s} {1!s}({2!s})", {3!s}'.format(execute, routine['routine_name'], placeholders, parameters) |
91
|
|
|
else: |
92
|
|
|
raise Exception('Internal error.') |
93
|
|
|
|
94
|
|
|
return line |
95
|
|
|
|
96
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
97
|
|
|
@staticmethod |
98
|
|
|
def _get_parameter_format_specifier(data_type: str) -> str: |
99
|
|
|
""" |
100
|
1 |
|
Returns the appropriate format specifier for a parameter type. |
101
|
1 |
|
|
102
|
|
|
:param data_type: The parameter type. |
103
|
|
|
""" |
104
|
|
|
lookup = {'bigint': '%s', |
105
|
|
|
'binary': '%s', |
106
|
|
|
'bit': '%s', |
107
|
|
|
'blob': '%s', |
108
|
|
|
'char': '%s', |
109
|
|
|
'date': '%s', |
110
|
|
|
'datetime': '%s', |
111
|
|
|
'decimal': '%s', |
112
|
|
|
'double': '%s', |
113
|
|
|
'enum': '%s', |
114
|
|
|
'float': '%s', |
115
|
|
|
'int': '%s', |
116
|
|
|
'longblob': '%s', |
117
|
|
|
'longtext': '%s', |
118
|
|
|
'mediumblob': '%s', |
119
|
|
|
'mediumint': '%s', |
120
|
|
|
'mediumtext': '%s', |
121
|
|
|
'set': '%s', |
122
|
|
|
'smallint': '%s', |
123
|
|
|
'text': '%s', |
124
|
|
|
'time': '%s', |
125
|
|
|
'timestamp': '%s', |
126
|
|
|
'tinyblob': '%s', |
127
|
|
|
'tinyint': '%s', |
128
|
|
|
'tinytext': '%s', |
129
|
|
|
'varbinary': '%s', |
130
|
|
|
'varchar': '%s', |
131
|
|
|
'year': '%s'} |
132
|
|
|
|
133
|
|
|
if data_type in lookup: |
134
|
|
|
return lookup[data_type] |
135
|
|
|
|
136
|
|
|
raise Exception('Unexpected data type {0!s}.'.format(data_type)) |
137
|
|
|
|
138
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
139
|
|
|
|