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