-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.py
260 lines (212 loc) 路 9.1 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Copyright (C) 2019
# Author: Cesar Roman
# Contact: thecesrom@gmail.com
"""Database module."""
__all__ = [
'check',
'execute_non_query',
'get_data',
'get_output_params',
'get_return_value'
]
import system.db
class _Result(object):
"""Result class."""
def __init__(self,
output_params=None,
result_set=None,
return_value=None,
update_count=None):
"""Result object initializer.
Args:
output_params (dict): All registered output parameters.
result_set (Dataset): The dataset that is the resulting
data of the stored procedure, if any.
return_value (int): The return value, if
registerReturnParam had been called.
update_count (int): The number of rows modified by the
stored procedure, or -1 if not applicable.
"""
self.output_params = output_params
self.result_set = result_set
self.return_value = return_value
self.update_count = update_count
def _execute_sp(stored_procedure, database='', transaction=None,
skip_audit=False, in_params=None, out_params=None,
get_out_params=False, get_result_set=False, get_ret_val=False,
return_type_code=None, get_update_count=False):
"""Executes a database stored procedure.
Args:
stored_procedure (str): The name of the stored procedure to
execute.
database (str): The name of the database connection to execute
against. If omitted or "", the project's default database
connection will be used. Optional.
transaction (str): A transaction identifier. If omitted, the
call will be executed in its own transaction. Optional.
skip_audit (bool): A flag which, if set to true, will cause
the procedure call to skip the audit system. Useful for
some queries that have fields which won't fit into the
audit log. Optional.
in_params (dict): A Dictionary containing INPUT parameters.
Optional.
out_params (dict): A Dictionary containing OUTPUT parameters.
Optional.
get_out_params (bool): A flag indicating whether or not to
return OUTPUT parameters after execution. Optional.
get_result_set (bool): A flag indicating whether or not to
return a dataset that is the resulting data of the stored
procedure, if any. Optional.
get_ret_val (bool): A flag indicating whether or not to return
the return value of the stored procedure Call. Optional.
return_type_code (int): The return value Type Code. Optional.
get_update_count (bool): A flag indicating whether or not to
return the number of rows modified by the stored
procedure, or -1 if not applicable. Optional.
Returns:
_Result: Result object.
"""
# Initialize variables.
_out_params = {}
_result = _Result()
call = system.db.createSProcCall(procedureName=stored_procedure,
database=database,
tx=transaction,
skipAudit=skip_audit)
# Register INPUT Parameters.
if in_params is not None:
for k, v in in_params.iteritems():
call.registerInParam(k, v[0], v[1])
# Register OUTPUT Parameters.
if out_params is not None:
for k, v in out_params.iteritems():
call.registerOutParam(k, v)
# Register RETURN Parameter.
if get_ret_val:
call.registerReturnParam(return_type_code)
# Execute the call.
system.db.execSProcCall(call)
if out_params is not None:
for k in out_params.iterkeys():
_out_params[k] = call.getOutParamValue(k)
if get_out_params:
_result.output_params = _out_params
if get_result_set:
_result.result_set = call.getResultSet()
if get_ret_val:
_result.return_value = call.getReturnValue()
if get_update_count:
_result.update_count = call.getUpdateCount()
return _result
def check(stored_procedure, database='', params=None):
"""Executes a stored procedure that returns a flag set to TRUE or
FALSE.
Args:
stored_procedure (str): The name of the stored procedure to
execute.
database (str): The name of the database connection to execute
against. If omitted or "", the project's default database
connection will be used. Optional.
params (dict): A Dictionary containing all parameters.
Optional.
Returns:
bool: The flag.
"""
output = {'flag': system.db.BIT}
output_params = get_output_params(stored_procedure,
output=output,
database=database,
params=params)
return output_params['flag']
def execute_non_query(stored_procedure, database='', transaction=None,
params=None):
"""Executes a stored procedure against the connection and returns
the number of rows affected.
Used for UPDATE, INSERT, and DELETE statements.
Args:
stored_procedure (str): The name of the stored procedure to
execute.
database (str): The name of the database connection to execute
against. If omitted or "", the project's default database
connection will be used. Optional.
transaction (str): A transaction identifier. If omitted, the
call will be executed in its own transaction. Optional.
params (dict): A Dictionary containing all parameters.
Optional.
Returns:
int: The number of rows modified by the stored procedure, or
-1 if not applicable.
"""
result = _execute_sp(stored_procedure,
database=database,
transaction=transaction,
in_params=params,
get_update_count=True)
return result.update_count
def get_data(stored_procedure, database='', params=None):
"""Returns data by executing a stored procedure.
Args:
stored_procedure (str): The name of the stored procedure to
execute.
database (str): The name of the database connection to execute
against. If omitted or "", the project's default database
connection will be used. Optional.
params (dict): A Dictionary containing all parameters.
Optional.
Returns:
Dataset: A Dataset that is the resulting data of the stored
procedure call, if any.
"""
result = _execute_sp(stored_procedure,
database=database,
in_params=params,
get_result_set=True)
return result.result_set
def get_output_params(stored_procedure, output, database='', transaction=None,
params=None):
"""Gets the Output parameters from the Stored Procedure.
Args:
stored_procedure (str): The name of the stored procedure to
execute.
output (dict): A Dictionary containing all output parameters.
database (str): The name of the database connection to execute
against. If omitted or "", the project's default database
connection will be used. Optional.
transaction (str): A transaction identifier. If omitted, the
call will be executed in its own transaction. Optional.
params (dict): A Dictionary containing all parameters.
Optional.
Returns:
dict: Result's output_params.
"""
result = _execute_sp(stored_procedure,
database=database,
transaction=transaction,
in_params=params,
out_params=output,
get_out_params=True)
return result.output_params
def get_return_value(stored_procedure, return_type_code, database='',
transaction=None, params=None):
"""Gets the Return Value from the Stored Procedure.
Args:
stored_procedure (str): The name of the stored procedure to
execute.
return_type_code (int): The Type Code of the Return Value.
database (str): The name of the database connection to execute
against. If omitted or "", the project's default database
connection will be used. Optional.
transaction (str): A transaction identifier. If omitted, the
call will be executed in its own transaction. Optional.
params (dict): A Dictionary containing all parameters.
Optional.
Returns:
int: The return value.
"""
result = _execute_sp(stored_procedure,
database=database,
transaction=transaction,
in_params=params,
return_type_code=return_type_code,
get_ret_val=True)
return result.return_value