forked from exiorrealty/sql-db-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_utils.py
More file actions
634 lines (565 loc) · 24.7 KB
/
sql_utils.py
File metadata and controls
634 lines (565 loc) · 24.7 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import logging as logger
from typing import Generic, List, Tuple, TypeVar, Union
from fastapi.encoders import jsonable_encoder
from sqlalchemy import Select, Table, delete, func, insert, select, update
from sqlalchemy.dialects.postgresql import insert as postgres_insert
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm.decl_api import DeclarativeAttributeIntercept
from sql_db_utils.aggrid import AGGridUtils
from sql_db_utils.constants import QueryType
TableType = TypeVar("TableType", bound=[DeclarativeBase, Table])
class SqlAlchemyUtil(Generic[TableType]):
"""
A utility class for performing SQL operations using SQLAlchemy V2.
"""
def __init__(self, session: AsyncSession, table: TableType = None):
"""
Initializes a new instance of the SqlAlchemyUtil class.
Args:
session (Session): The SQLAlchemy session object.
table (TableType, optional): The SQLAlchemy declarative base object. Defaults to None.
"""
self.session = session
self.table = table
def __del__(self):
"""
Closes the SQLAlchemy session.
"""
logger.debug("Closing SQL session!")
import asyncio
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
if loop.is_running():
loop.create_task(self.session.close())
else:
loop.run_until_complete(self.session.close())
async def insert(self, data: Union[dict, list[dict]], return_keys: List[str] = None, table: TableType = None):
"""
Inserts a single row into the database.
Args:
data (dict): A dictionary containing the data to be inserted.
return_keys (List[str], optional): A list of column names to return after the insert. Defaults to None.
table (TableType, optional): The SQLAlchemy declarative base object. Defaults to None.
Returns:
A list of dictionaries containing the inserted data.
"""
table = table if table is not None else self.table
return_keys = return_keys or []
try:
insert_stmt = (
insert(table)
.values(data)
.returning(*(getattr(table.c if isinstance(table, Table) else table, key) for key in return_keys))
)
return_values = await self.session.execute(insert_stmt)
await self.session.commit()
if return_keys:
return jsonable_encoder(return_values.mappings().all())
except Exception as e:
logger.error(f"Error occurred while inserting: {e}", exc_info=True)
raise e
async def update_with_where(
self,
data: Union[dict, list[dict]],
where_conditions: List,
return_keys: List[str] = None,
table: TableType = None,
):
"""
Updates rows in the database based on the given conditions.
Args:
data (dict): A dictionary containing the data to be updated.
where_conditions (List): A list of conditions to filter the data.
table (TableType, optional): The SQLAlchemy declarative base object. Defaults to None.
"""
table = table if table is not None else self.table
return_keys = return_keys or []
try:
update_stmt = (
update(table)
.values(data)
.where(*where_conditions)
.returning(*(getattr(table.c if isinstance(table, Table) else table, key) for key in return_keys))
)
return_values = await self.session.execute(update_stmt)
await self.session.commit()
if return_keys:
return jsonable_encoder(return_values.mappings().all())
except Exception as e:
logger.error(f"Error occurred while updating: {e}", exc_info=True)
raise e
async def update(self, data: Union[dict, list[dict]], return_keys: List[str] = None, table: TableType = None):
"""
Updates multiple rows in the database.
Args:
data (List[dict]): A list of dictionaries containing the data to be updated.
table (TableType, optional): The SQLAlchemy declarative base object. Defaults to None.
"""
table = table if table is not None else self.table
return_keys = return_keys or []
try:
return_values = await self.session.execute(
update(table).returning(
*(getattr(table.c if isinstance(table, Table) else table, key) for key in return_keys)
),
data,
)
await self.session.commit()
if return_keys:
return jsonable_encoder(return_values.mappings().all())
except Exception as e:
logger.error(f"Error occurred while updating: {e}", exc_info=True)
raise e
async def upsert(
self, insert_json: dict, primary_keys: List[str] = None, return_keys: List[str] = None, table: TableType = None
):
"""
Inserts or updates a row in the database.
Args:
insert_json (dict): A dictionary containing the data to be inserted or updated.
primary_keys (List[str], optional): A list of primary key column names. Defaults to None.
return_keys (List[str], optional): A list of column names to return after the upsert. Defaults to None.
table (TableType, optional): The SQLAlchemy declarative base object. Defaults to None.
Returns:
A list of dictionaries containing the upserted data if return_keys is provided.
"""
table = table if table is not None else self.table
return_keys = return_keys or []
try:
insert_statement = (
postgres_insert(table)
.values(**insert_json)
.on_conflict_do_update(index_elements=primary_keys, set_=insert_json)
.returning(*(getattr(table.c if isinstance(table, Table) else table, key) for key in return_keys))
)
return_values = await self.session.execute(insert_statement)
await self.session.commit()
if return_keys:
return jsonable_encoder(return_values.mappings().all())
except Exception as e:
logger.error(f"Error while upserting the record {e}", exc_info=True)
raise e
async def delete(self, where_conditions: List, return_keys: List[str] = None, table: TableType = None):
"""
Deletes rows from the database based on the given conditions.
Args:
where_conditions (List): A list of conditions to filter the data.
table (TableType, optional): The SQLAlchemy declarative base object. Defaults to None.
"""
table = table if table is not None else self.table
return_keys = return_keys or []
try:
delete_stmt = (
delete(table)
.where(*where_conditions)
.returning(*(getattr(table.c if isinstance(table, Table) else table, key) for key in return_keys))
)
return_values = await self.session.execute(delete_stmt)
await self.session.commit()
if return_keys:
return jsonable_encoder(return_values.mappings().all())
except Exception as e:
logger.error(f"Error occurred while deleting: {e}", exc_info=True)
raise e
def _get_columns(self, columns: List, table: TableType) -> List:
columns_updated = []
for column in columns:
if isinstance(column, str):
columns_updated.append(getattr(table, column))
elif isinstance(column, DeclarativeAttributeIntercept):
columns_updated.extend(list(column.__table__.columns))
else:
columns_updated.append(column)
return columns_updated
def _build_select_query(
self,
table: TableType,
where_conditions: List,
offset: int = None,
columns: Tuple[str] = None,
order_by: List = None,
group_by: List = None,
):
"""
Builds a select query based on the given conditions.
Args:
table (TableType): The SQLAlchemy declarative base object.
where_conditions (List): A list of conditions to filter the data.
columns (Tuple[str], optional): A tuple of column names to select. Defaults to None.
Returns:
The built select query.
"""
order_by = order_by or []
group_by = group_by or []
select_stmt = select(*(table.columns.values() if isinstance(table, Table) else table.__table__.columns))
if columns:
select_stmt = select_stmt.with_only_columns(*self._get_columns(columns, table))
return select_stmt.where(*where_conditions).order_by(*order_by).group_by(*group_by).offset(offset)
async def _get_count(self, table: TableType, where_conditions: List):
"""
Returns the count of rows in the given table.
Args:
table (TableType): The SQLAlchemy declarative base object.
where_conditions (List): A list of conditions to filter the data.
Returns:
The count of rows in the given table.
"""
count_query = select(table).with_only_columns(func.count()).order_by(None).where(*where_conditions)
return await self.session.execute(count_query).scalar()
async def _get_count_advanced(self, select_stmt: Select):
"""
Returns the count of rows in the given table.
Args:
select_stmt (Select): The SQLAlchemy select object.
Returns:
The count of rows in the given table.
"""
count_query = select(func.count()).select_from(select_stmt.subquery())
data = await self.session.execute(count_query)
return data.scalar()
async def select_from_table(
self, # NOSONAR noqa
where_conditions: List,
columns: Tuple[str] = None,
select_one: bool = False,
offset: int = None,
limit: int = None,
return_count: bool = False,
return_type: QueryType = QueryType.JSON,
query_kwargs: dict = None,
order_by: List = None,
group_by: List = None,
table: TableType = None,
aggrid_filters: dict = None,
aggrid_column_mappings: dict = None,
aggrid_column_options: dict = None,
aggrid_options: dict = None,
_streamer: bool = False,
**__kwargs__,
):
"""
Selects data from a table based on the given conditions.
Args:
where_conditions (List): A list of conditions to filter the data by.
columns (Tuple[str], optional): A tuple of column names to select. Defaults to None.
select_one (bool, optional): Whether to select only one row. Defaults to False.
offset (int, optional): The number of rows to skip. Defaults to None.
limit (int, optional): The maximum number of rows to return. Defaults to None.
return_count (bool, optional): Whether to return the count of rows. Defaults to False.
return_type (QueryType, optional): The type of query to return. Defaults to QueryType.JSON.
table (TableType, optional): The table to select from. Defaults to None.
Returns:
The selected data.
"""
table = table if table is not None else self.table
order_by = order_by or []
group_by = group_by or []
aggrid_column_mappings = aggrid_column_mappings or {}
aggrid_filters = aggrid_filters or {}
query_kwargs = query_kwargs or {}
aggrid_options = aggrid_options or {}
try:
aggrid_util = AGGridUtils(
aggrid_column_mappings,
aggrid_filters,
aggrid_options.pop("date_trim", None),
aggrid_column_options,
aggrid_options.pop("tz", None),
)
ag_filters, ag_sorters = aggrid_util()
where_conditions.extend(ag_filters)
ag_sorters.extend(order_by)
order_by = ag_sorters
select_stmt = self._build_select_query(table, where_conditions, offset, columns, order_by, group_by)
if _streamer:
return select_stmt.limit(limit)
if select_one:
result = await self.session.execute(select_stmt)
return jsonable_encoder(result.mappings().first())
results = await self.fetch_by_query(select_stmt.limit(limit), return_type, **query_kwargs)
if return_count:
count_results = await self._get_count(table, where_conditions)
return (count_results, results)
return results
except Exception as e:
logger.error(f"Error occurred while fetching: {e}", exc_info=True)
raise e
async def select_from_table_streamer(self, **__kwargs__):
"""
Streams data from the database using the provided query and returns the result in the specified format.
Args:
query (str): The SQL query to execute.
query_type (QueryType, optional): The format in which to return the result. Defaults to QueryType.JSON.
Returns:
The result of the query in the specified format.
"""
try:
query = await self.select_from_table(**__kwargs__, _streamer=True)
import pandas as pd
from pandas.io.sql import _wrap_result
cursor = await self.session.execute(query)
columns = cursor.keys()
has_read_data = False
__kwargs__ = __kwargs__.get("query_kwargs")
while True:
data = cursor.fetchmany(__kwargs__.get("chunksize"))
if isinstance(data, tuple):
data = list(data)
if not data:
if not has_read_data:
result = pd.DataFrame.from_records([], columns=columns)
yield result
break
has_read_data = True
yield _wrap_result(
data,
columns,
index_col=None,
coerce_float=True,
parse_dates=None,
dtype=None,
dtype_backend=__kwargs__.get("pandas_dtype", "numpy_nullable"),
)
except Exception as e:
logger.error(f"Error occurred while fetching: {e}")
def _prepare_joins(self, select_stmt, joins, join_additional_where_conditions):
for join in joins:
if isinstance(join, tuple):
select_stmt = select_stmt.join(*join)
elif isinstance(join, dict):
select_stmt = select_stmt.join(**join)
if join_additional_where_conditions:
select_stmt = select_stmt.where(*join_additional_where_conditions)
return select_stmt
async def select_from_table_advanced(
self, # NOSONAR noqa
where_conditions: List,
columns: Tuple[str] = None,
select_stmt: Select = None,
select_one: bool = False,
offset: int = None,
limit: int = None,
return_count: bool = False,
return_type: QueryType = QueryType.JSON,
query_kwargs: dict = None,
order_by: List = None,
group_by: List = None,
aggrid_filters: dict = None,
aggrid_column_mappings: dict = None,
aggrid_column_options: dict = None,
aggrid_options: dict = None,
table: TableType = None,
joins: List = None,
join_additional_where_conditions: List = None,
_streamer: bool = False,
):
table = table if table is not None else self.table
order_by = order_by or []
group_by = group_by or []
query_kwargs = query_kwargs or {}
aggrid_filters = aggrid_filters or {}
aggrid_column_mappings = aggrid_column_mappings or {}
aggrid_options = aggrid_options or {}
try:
aggrid_util = AGGridUtils(
aggrid_column_mappings,
aggrid_filters,
aggrid_options.pop("date_trim", None),
aggrid_column_options,
aggrid_options.pop("tz", None),
)
ag_filters, ag_sorters = aggrid_util()
where_conditions.extend(ag_filters)
if select_stmt is None:
select_stmt = self._build_select_query(table, where_conditions, offset, columns, order_by, group_by)
else:
select_stmt = select_stmt.where(*where_conditions)
if joins:
select_stmt = self._prepare_joins(select_stmt, joins, join_additional_where_conditions)
base_stmt = select_stmt.order_by(*ag_sorters, *order_by).group_by(*group_by)
select_stmt = base_stmt.offset(offset)
if _streamer:
return select_stmt.limit(limit)
if select_one:
result = await self.session.execute(select_stmt)
return jsonable_encoder(result.mappings().first())
results = await self.fetch_by_query(select_stmt.limit(limit), return_type, **query_kwargs)
if return_count:
count_results = await self._get_count_advanced(base_stmt)
return (count_results, results)
return results
except Exception as e:
logger.error(f"Error occurred while fetching: {e}", exc_info=True)
raise e
async def select_from_table_advanced_streamer(self, **__kwargs__):
"""
Streams data from the database using the provided query and returns the result in the specified format.
Args:
query (str): The SQL query to execute.
query_type (QueryType, optional): The format in which to return the result. Defaults to QueryType.JSON.
Returns:
The result of the query in the specified format.
"""
try:
query = await self.select_from_table_advanced(**__kwargs__, _streamer=True)
import pandas as pd
from pandas.io.sql import _wrap_result
cursor = await self.session.execute(query)
columns = cursor.keys()
has_read_data = False
__kwargs__ = __kwargs__.get("query_kwargs")
while True:
data = cursor.fetchmany(__kwargs__.get("chunksize"))
if isinstance(data, tuple):
data = list(data)
if not data:
if not has_read_data:
result = pd.DataFrame.from_records([], columns=columns)
yield result
break
has_read_data = True
yield _wrap_result(
data,
columns,
index_col=None,
coerce_float=True,
parse_dates=None,
dtype=None,
dtype_backend="pyarrow" if __kwargs__.get("arrow", False) else "numpy_nullable",
)
except Exception as e:
logger.error(f"Error occurred while fetching: {e}")
async def fetch_as_polars(self, query, stream: bool = False, chunksize: int = None, **__kwargs__):
"""
Fetches data from the database using Polars library.
Args:
query: SQLAlchemy query object.
Returns:
A Polars DataFrame object containing the fetched data.
Raises:
ImportError: If Polars library is not installed.
Exception: If an error occurs while fetching the data.
"""
try:
import polars as pl
return pl.read_database(
query,
connection=self.session.bind,
execute_options={"parameters": query.compile().params},
iter_batches=stream,
**{"batch_size": chunksize} if stream else {},
)
except ImportError as ie:
logger.debug("Polars not installed, Failed to fetch using polars")
raise ie
except Exception as e:
logger.error(f"Error occurred while fetching using polars: {e}")
async def fetch_by_stream(self, query, chunksize: int = None, arrow: bool = False, **__kwargs__):
"""
Streams data from the database using pandas.
Args:
cursor: The database cursor object.
chunksize (int): The number of rows to fetch at a time.
Returns:
A generator object containing the fetched data.
"""
try:
import pandas as pd
from pandas.io.sql import _wrap_result
cursor = await self.session.execute(query)
columns = cursor.keys()
has_read_data = False
while True:
data = cursor.fetchmany(chunksize)
if isinstance(data, tuple):
data = list(data)
if not data:
if not has_read_data:
result = pd.DataFrame.from_records([], columns=columns)
yield result
break
has_read_data = True
yield _wrap_result(
data,
columns,
index_col=None,
coerce_float=True,
parse_dates=None,
dtype=None,
**({"dtype_backend": "pyarrow"} if arrow else {}),
)
yield result
except Exception as e:
logger.error(f"Error occurred while fetching using pandas: {e}")
async def fetch_as_pandas(
self, query, stream: bool = False, chunksize: int = None, arrow: bool = False, **__kwargs__
):
"""
Fetches data from the database using pandas or polars library.
Args:
query (str): SQL query to execute.
Returns:
pandas.DataFrame: DataFrame containing the results of the query.
Raises:
ImportError: If neither pandas nor polars library is installed.
Exception: If an error occurs while fetching data using pandas or polars.
"""
try:
import pandas as pd
from pandas.io.sql import _wrap_result
cursor = await self.session.execute(query)
columns = cursor.keys()
if stream:
return self.fetch_by_stream(cursor, columns, chunksize, **__kwargs__)
data = cursor.fetchall()
res: pd.DataFrame | None = _wrap_result(
data,
columns,
index_col=None,
coerce_float=True,
parse_dates=None,
dtype=None,
**({"dtype_backend": "pyarrow"} if arrow else {}),
)
if res is None:
return pd.DataFrame()
return res
except Exception as e:
logger.error(f"Error occurred while fetching using pandas: {e}")
return pd.DataFrame()
async def fetch_as_json(self, query, **__kwargs__):
"""
Executes the given SQL query and returns the result as a list of dictionaries.
Args:
query (str): The SQL query to execute.
Returns:
list: A list of dictionaries representing the result of the query.
Raises:
Exception: If an error occurs while fetching data.
"""
try:
results = await self.session.execute(query)
return jsonable_encoder(results.mappings().all())
except Exception as e:
logger.error(f"Error occurred while fetching data: {e}")
async def fetch_by_query(self, query, query_type: QueryType = QueryType.JSON, **__kwargs__):
"""
Fetches data from the database using the provided query and returns the result in the specified format.
Args:
query (str): The SQL query to execute.
query_type (QueryType, optional): The format in which to return the result. Defaults to QueryType.JSON.
Returns:
The result of the query in the specified format.
"""
try:
callable_func = getattr(self, f"fetch_as_{query_type.value}")
result = await callable_func(query, **__kwargs__)
return result
except Exception as e:
logger.error(f"Error occurred while fetching: {e}")
import traceback
logger.error(traceback.format_exc())