-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_handler.py
More file actions
374 lines (178 loc) · 8.1 KB
/
Copy pathsql_handler.py
File metadata and controls
374 lines (178 loc) · 8.1 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import pandas as pd
import pyodbc
import sqlalchemy
import urllib
from functools import wraps
from typing import Iterable, Tuple, Any, Union
class MsSqlHandler:
"""
A class used to represent a MsSqlHandler object, which is used to perform SQL CRUD operations on a Microsoft SQL Server Database using Pandas, Pyodbc, SQLAlchemy.
Attributes:
-----------
connection_string: str
Connnection string for the target database formatted for pyodbc and SQL Server.
read_only: str
Toggle this property to enable (read_only=False) or disable (read_only=True) ability to perform write operations with the MsSqlHandler Object. Default is False.
Methods:
--------
query(sql_query_statement, parameters):
Returns a pandas dataframe from a query results set on the target database.
execute(sql_statement, parameters):
Performs any Create, Update, Insert, or Delete operation on the target database. Method disabled when read_only=True.
bulk_insert(dataframe, table_name, remove_nulls, handle_nulls)
Bulk inserts data from a pandas dataframe on the target database (uses Pyodbc's fast execute_many). Method disabled when read_only=True.
iter_execute(parameterized_sql_statement, values, error_handling)
Loops through each row of data to perform create, update, insert, or delete operation on the target database. Method disabled when read_only=True.
Notes:
------
All database connection operations are handed internally. Initialize a MsSqlHandler object by simply setting the connection_string property. Nothing else is necessary to begin performing CRUD operations
using the query, execute, bulk_insert, or iter_execute method.
The execute, bulk-insert, and iter-execute methods perform write operations, and only work when the read_only property is set to False.
"""
def __init__(self, connection_string: str, read_only=False):
"""
connection_string (str): Formatted for pyodbc.
read_only (bool): Defaults to False.
"""
self.connection_string = connection_string
self.read_only = read_only
def allow_method(func):
@wraps(func)
def wrapper(*args, **kwargs):
if args[0].read_only:
print('Cannot perform operation. Access is read-only.')
return None
return func(*args, **kwargs)
return wrapper
def _conn(self):
"""Open a database connection"""
return pyodbc.connect(self.connection_string)
def _sqlalchemy_conn(self):
quoted = urllib.parse.quote_plus(self.connection_string)
x='mssql+pyodbc:///?odbc_connect={}'.format(quoted)
engine = sqlalchemy.create_engine(x)
return engine
def query(self, sql_query_statement: str, *parameters, **kwargs) -> pd.DataFrame:
"""
Purpose: Useful for querying SQL database.
Required arguments:
sql_query_statement (str): If parameterized than must pass a tuple of values to the parameters argument.
Optional arguments:
*parameters (int, str, float, or datetime)
pandas_dataframe (bool): True (default) of False. True returns pandas dataframe and False returns a list of tuples.
**kwargs: see pandas.read_sql documentation
"""
engine = self._sqlalchemy_conn()
return pd.read_sql(sql_query_statement, engine, params=parameters, **kwargs)
@allow_method
def execute(self, sql_statement: str, *parameters) -> None:
"""
Purpose: Perform any Create, Update, Insert, Delete operation.
Required arguments:
sql_statement (str): If parameterized than must include *parameters.
Optional arguments:
*parameters (int, str, float, or datetime)
"""
conn = self._conn()
with conn:
conn.cursor().execute(sql_statement, parameters)
@allow_method
def iter_execute(self, parameterized_sql_statement: str, values, error_handling ='raise') -> None:
"""
Purpose: Perform an update/insert operation several times using parameterized sql statement.
Required arguments:
parameterized_sql_statement (str): Used to perform either update or insert operation.
Must be parameterized (i.e., contain question marks in place of values; see pyodbc documentation)
Example statement: 'INSERT INTO USERS (ID, Name) VALUES (?, ?);'
values (iterable): The data to insert or update. Must be an iterable of tuples.
Each tuple in iterable must contain number of values that equal the number
of parameter markers (i.e., question marks).
For example, if there are two parameter markers,
each tuple must be of length equal to 2.
[(1, 'Mary'),
(2, 'Bob'),
(3, 'Bill')]
error_handling: If one statement fails raise exception ('raise') or ignore and continue inserting rows('ignore')
"""
if '?' not in parameterized_sql_statement:
raise ValueError('Sql statement must be parameterized.')
conn = self._conn()
if error_handling =='raise': # raise exception and rollback all transactions (pyodbc default)
with conn:
c = conn.cursor()
for row in values:
c.execute(parameterized_sql_statement, row)
elif error_handling == 'ignore': # commit each row of values and ignore exceptions
with conn:
c = conn.cursor()
for row in values:
try:
c.execute(parameterized_sql_statement, row)
except Exception as e:
print()
print(e)
print(row)
print()
else:
c.commit()
else:
raise TypeError("Invalid argument for 'error_handling'. Argument must be 'raise' or 'ignore'.")
@allow_method
def bulk_insert(self, df: pd.DataFrame, table_name: str, **kwargs) -> None:
"""
Purpose: Perform a bulk insert using pyodbc's fast execute_many.
Required arguments:
df (pd.DataFrame): Pandas DataFrame containing the data to insert.
table_name (str): Name of SQL database table to insert the data.
Optional arguments:
remove_nulls (bool): Remove rows with nulls before inserting. Defaults to False.
subset_cols (None, list): Use when remove_nulls is True. List of column name strings to look for nulls when deciding to remove rows with nulls.
handle_nulls (None, str): Prevent errors when inserting data. Specify '*' for all columns. This is usually the best option. Otherwise, specify a column name containing nulls.
identity_insert_on (bool): False (default) or True.
"""
remove_nulls = kwargs.get('remove_nulls', False)
subset_cols = kwargs.get('subset_cols', None)
handle_nulls = kwargs.get('handle_nulls', None)
identity_insert_on = kwargs.get('identity_insert_on', False)
if remove_nulls:
if subset_cols is not None:
df.dropna(inplace=True, subset=subset_cols)
else:
df.dropna(inplace=True)
if handle_nulls == '*':
df = df.astype(object).where(pd.notnull(df), None)
df = df.replace([np.nan, np.inf, np.NaN, -np.inf, pd.NA, pd.NaT], None)
self._insert_values(df, table_name, identity_insert_on)
elif handle_nulls is not None:
values, nulls = self._separate_nulls(df, handle_nulls)
self._insert_values(values, table_name, identity_insert_on)
self._insert_values(nulls, table_name, identity_insert_on)
else:
self._insert_values(df, table_name, identity_insert_on)
@allow_method
def _insert_values(self, df: pd.DataFrame, table_name: str, identity_insert_on: bool) -> None:
"""helper method for bulk_insert method"""
c = list(df.columns)
columns = ','.join(c)
params = ','.join(['?' for i in range(len(c))])
sql_insert_statement = f'INSERT INTO {table_name} ({columns}) VALUES ({params});'
if identity_insert_on:
sql_insert_statement = f'SET IDENTITY_INSERT {table_name} ON; ' + sql_insert_statement + f' SET IDENTITY_INSERT {table_name} OFF;'
values = df.values.tolist()
if len(values) > 0:
conn = self._conn()
conn.autocommit=False
with conn:
c = conn.cursor()
c.fast_executemany = True
c.executemany(sql_insert_statement, values)
c.commit()
def _separate_nulls(self, df: pd.DataFrame, column: str) -> (pd.DataFrame, pd.DataFrame):
"""helper method if a column for handle_nulls parameter is specified for bulk_insert method."""
values = df.copy()
values = values.loc[~values[column].isna()]
nulls = df.copy()
nulls = nulls.loc[nulls[column].isna()]
nulls[column] = nulls[column].astype(str)
nulls[column] = None
return values, nulls