1
|
|
|
from abc import ABC |
2
|
|
|
from typing import Any, Dict, List |
3
|
|
|
|
4
|
|
|
from pystratum_common.wrapper.Wrapper import Wrapper |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class MsSqlWrapper(Wrapper, ABC): |
8
|
|
|
""" |
9
|
|
|
Parent class for wrapper method generators for stored procedures and functions. |
10
|
|
|
""" |
11
|
|
|
|
12
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
13
|
|
|
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_blob = False |
22
|
|
|
|
23
|
|
|
lookup = {'bigint': False, |
24
|
|
|
'binary': False, |
25
|
|
|
'bit': False, |
26
|
|
|
'char': False, |
27
|
|
|
'date': False, |
28
|
|
|
'datetime': False, |
29
|
|
|
'datetime2': False, |
30
|
|
|
'datetimeoffset': False, |
31
|
|
|
'decimal': False, |
32
|
|
|
'float': False, |
33
|
|
|
'geography': True, |
34
|
|
|
'geometry': True, |
35
|
|
|
'image': True, |
36
|
|
|
'int': False, |
37
|
|
|
'money': False, |
38
|
|
|
'nchar': False, |
39
|
|
|
'ntext': True, |
40
|
|
|
'numeric': False, |
41
|
|
|
'nvarchar': False, |
42
|
|
|
'real': False, |
43
|
|
|
'smalldatetime': False, |
44
|
|
|
'smallint': False, |
45
|
|
|
'smallmoney': False, |
46
|
|
|
'text': True, |
47
|
|
|
'time': False, |
48
|
|
|
'tinyint': False, |
49
|
|
|
'varbinary': False, |
50
|
|
|
'varchar': False, |
51
|
|
|
'xml': True} |
52
|
|
|
|
53
|
|
|
if parameters: |
54
|
|
|
for parameter_info in parameters: |
55
|
|
|
if parameter_info['data_type'] in lookup: |
56
|
|
|
has_blob = lookup[parameter_info['data_type']] |
57
|
|
|
else: |
58
|
|
|
raise Exception("Unexpected date type '{0}'".format(parameter_info['data_type'])) |
59
|
|
|
|
60
|
|
|
return has_blob |
61
|
|
|
|
62
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
63
|
|
|
def _write_routine_method_without_lob(self, routine: Dict[str, Any]) -> str: |
64
|
|
|
|
65
|
|
|
self._write_line() |
66
|
|
|
self._write_separator() |
67
|
|
|
self._write_line( |
68
|
|
|
'def {0!s}({1!s}):'.format(str(routine['routine_base_name']), str(self._get_wrapper_args(routine)))) |
69
|
|
|
self._write_result_handler(routine) |
70
|
|
|
|
71
|
|
|
return self._code |
72
|
|
|
|
73
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
74
|
|
|
def _generate_command(self, routine: Dict[str, Any]) -> str: |
75
|
|
|
""" |
76
|
|
|
Returns a SQL-statement for calling a stored routine. |
77
|
|
|
|
78
|
|
|
:param routine: Metadata of the stored routine. |
79
|
|
|
|
80
|
|
|
:rtype: str |
81
|
|
|
""" |
82
|
|
|
if routine['parameters']: |
83
|
|
|
if routine['designation'] == 'function': |
84
|
|
|
sql = "'select [%s].[%s](%s)', %s" |
85
|
|
|
else: |
86
|
|
|
sql = "'exec [%s].[%s] %s', %s" |
87
|
|
|
|
88
|
|
|
parameters = '' |
89
|
|
|
placeholders = '' |
90
|
|
|
for parameter in routine['parameters']: |
91
|
|
|
if parameters: |
92
|
|
|
parameters += ', ' |
93
|
|
|
placeholders += ', ' |
94
|
|
|
parameters += parameter['name'] |
95
|
|
|
placeholders += self._get_parameter_format_specifier(parameter['data_type']) |
96
|
|
|
|
97
|
|
|
ret = sql % (routine['schema_name'], |
98
|
|
|
routine['routine_base_name'], # routine_base_name |
99
|
|
|
placeholders, |
100
|
|
|
parameters) |
101
|
|
|
else: |
102
|
|
|
if routine['designation'] == 'function': |
103
|
|
|
sql = "'select [%s].[%s]()'" |
104
|
|
|
else: |
105
|
|
|
sql = "'exec [%s].[%s]'" |
106
|
|
|
|
107
|
|
|
ret = sql % (routine['schema_name'], |
108
|
|
|
routine['routine_base_name']) |
109
|
|
|
|
110
|
|
|
return ret |
111
|
|
|
|
112
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
113
|
|
|
@staticmethod |
114
|
|
|
def _get_parameter_format_specifier(data_type: str) -> str: |
115
|
|
|
""" |
116
|
|
|
Returns the appropriate format specifier for a parameter type. |
117
|
|
|
|
118
|
|
|
:param str data_type: The parameter type. |
119
|
|
|
|
120
|
|
|
:rtype: str |
121
|
|
|
""" |
122
|
|
|
lookup = {'bigint': '%s', |
123
|
|
|
'binary': '%s', |
124
|
|
|
'bit': '%s', |
125
|
|
|
'char': '%s', |
126
|
|
|
'date': '%s', |
127
|
|
|
'datetime': '%s', |
128
|
|
|
'datetime2': '%s', |
129
|
|
|
'datetimeoffset': '%s', |
130
|
|
|
'decimal': '%s', |
131
|
|
|
'float': '%s', |
132
|
|
|
'geography': '%s', |
133
|
|
|
'geometry': '%s', |
134
|
|
|
'image': '%s', |
135
|
|
|
'int': '%s', |
136
|
|
|
'money': '%s', |
137
|
|
|
'nchar': '%s', |
138
|
|
|
'ntext': '%s', |
139
|
|
|
'numeric': '%s', |
140
|
|
|
'nvarchar': '%s', |
141
|
|
|
'real': '%s', |
142
|
|
|
'smalldatetime': '%s', |
143
|
|
|
'smallint': '%s', |
144
|
|
|
'smallmoney': '%s', |
145
|
|
|
'text': '%s', |
146
|
|
|
'time': '%s', |
147
|
|
|
'timestamp': '%s', |
148
|
|
|
'tinyint': '%s', |
149
|
|
|
'varbinary': '%s', |
150
|
|
|
'varchar': '%s', |
151
|
|
|
'xml': '%s'} |
152
|
|
|
|
153
|
|
|
if data_type in lookup: |
154
|
|
|
return '%s' |
155
|
|
|
|
156
|
|
|
raise Exception('Unexpected data type {0}'.format(data_type)) |
157
|
|
|
|
158
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
159
|
|
|
@staticmethod |
160
|
|
|
def _get_re_type(data_type: str) -> str: |
161
|
|
|
|
162
|
|
|
lob = '%s' |
163
|
|
|
|
164
|
|
|
templates = {'bigint': '%s', |
165
|
|
|
'binary': '%s', |
166
|
|
|
'bit': '%s', |
167
|
|
|
'char': '%s', |
168
|
|
|
'date': '%s', |
169
|
|
|
'datetime': '%s', |
170
|
|
|
'datetime2': '%s', |
171
|
|
|
'datetimeoffset': '%s', |
172
|
|
|
'decimal': '%s', |
173
|
|
|
'float': '%s', |
174
|
|
|
'image': lob, |
175
|
|
|
'geography': lob, |
176
|
|
|
'geometry': lob, |
177
|
|
|
'int': '%s', |
178
|
|
|
'money': '%s', |
179
|
|
|
'nchar': '%s', |
180
|
|
|
'ntext': lob, |
181
|
|
|
'numeric': '%s', |
182
|
|
|
'nvarchar': '%s', |
183
|
|
|
'real': '%s', |
184
|
|
|
'smalldatetime': '%s', |
185
|
|
|
'smallint': '%s', |
186
|
|
|
'smallmoney': '%s', |
187
|
|
|
'text': lob, |
188
|
|
|
'time': '%s', |
189
|
|
|
'tinyint': '%s', |
190
|
|
|
'varbinary': '%s', |
191
|
|
|
'varchar': '%s', |
192
|
|
|
'xml': lob} |
193
|
|
|
|
194
|
|
|
if data_type in templates: |
195
|
|
|
return templates[data_type] |
196
|
|
|
|
197
|
|
|
raise Exception('Unexpected data type {0}'.format(data_type)) |
198
|
|
|
|
199
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
200
|
|
|
|