Skip to content

Commit a8114b2

Browse files
committed
cleanup of get_rows to explicitly slice and filter based on gte and lte suffixes instead of specific column names
1 parent 0236cb3 commit a8114b2

6 files changed

Lines changed: 76 additions & 80 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ projects, this package will be continuously supported and developed. A close eye
2020
will be kept on the SQLModel's ongoing roadmap and eventual uplift to SQLAlchemy
2121
2.0 and Pydantic 2.0.
2222
## Development Roadmap
23-
- [ ] Release working Alpha version
24-
- [ ] Test across existing projects to ensure complete coverage
23+
- [x] Release working Alpha version
24+
- [x] Test across existing projects to ensure complete coverage
2525
- [ ] 100% test coverage
2626
- [ ] Complete autonomous CICD for on-demand testing and building
2727

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ build-backend = "setuptools.build_meta"
6262

6363
[project]
6464
name = "sqlmodel_crud_utilities"
65-
version = "0.0.1"
65+
version = "0.0.1a1"
6666
authors = [
6767
{ name="Francis Secada", email = "francis.secada@gmail.com" }
6868
]

sqlmodel_crud_utils/a_sync.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
3+
"""
4+
15
from typing import Type
26

37
from dateutil.parser import parse as date_parse
@@ -9,7 +13,7 @@
913
from sqlmodel.ext.asyncio.session import AsyncSession
1014
from sqlmodel.sql.expression import SelectOfScalar
1115

12-
from sqlmodel_crud_utils.utils import get_sql_dialect_import, get_val
16+
from sqlmodel_crud_utils.utils import get_sql_dialect_import, get_val, is_date
1317

1418
load_dotenv() # take environment variables from .env.
1519

@@ -235,39 +239,19 @@ async def get_rows(
235239
if stmnt is None:
236240
stmnt = select(model)
237241
if kwargs:
238-
if ["date" in x for x in kwargs] and any(
239-
x in y for y in kwargs for x in ("lte", "gte")
240-
):
241-
date_keys = [x for x in kwargs.keys() if "date" in x]
242-
for key in date_keys:
243-
if "lte" in key:
244-
model_key = key.replace("__lte", "")
245-
date_val = kwargs.pop(key)
246-
if isinstance(date_val, str):
247-
date_val = date_parse(date_val)
248-
stmnt = stmnt.where(
249-
getattr(model, model_key) < date_val
250-
)
251-
elif "gte" in key:
252-
model_key = key.replace("__gte", "")
253-
logger.info(model_key)
254-
date_val = kwargs.pop(key)
255-
if isinstance(date_val, str):
256-
date_val = date_parse(date_val)
257-
stmnt = stmnt.where(
258-
getattr(model, model_key) > date_val
259-
)
260-
else:
261-
date_val = kwargs.pop(key)
262-
if isinstance(date_val, str):
263-
date_val = date_parse(date_val)
264-
stmnt = stmnt.where(getattr(model, key) == date_val)
265-
elif "date" in kwargs:
266-
date_keys = [x for x in kwargs.keys() if "date" in x]
267-
for key in date_keys:
268-
stmnt = stmnt.where(getattr(model, key) == kwargs.pop(key))
269-
else:
270-
pass
242+
for key in kwargs:
243+
if "__lte" in key:
244+
model_key = key.replace("__lte", "")
245+
val = kwargs.pop(key)
246+
if is_date(val, fuzzy=True):
247+
val = date_parse(val)
248+
stmnt = stmnt.where(getattr(model, model_key) < val)
249+
elif "__gte" in key:
250+
model_key = key.replace("__gte", "")
251+
val = kwargs.pop(key)
252+
if is_date(val, fuzzy=True):
253+
val = date_parse(val)
254+
stmnt = stmnt.where(getattr(model, model_key) > val)
271255
sort_desc, sort_field = (
272256
kwargs.pop(x, None) for x in ("sort_desc", "sort_field")
273257
)

sqlmodel_crud_utils/sync.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
3+
"""
4+
15
from typing import Type
26

37
from dateutil.parser import parse as date_parse
@@ -8,7 +12,7 @@
812
from sqlmodel import Session, SQLModel, select
913
from sqlmodel.sql.expression import SelectOfScalar
1014

11-
from sqlmodel_crud_utils.utils import get_sql_dialect_import, get_val
15+
from sqlmodel_crud_utils.utils import get_sql_dialect_import, get_val, is_date
1216

1317
load_dotenv() # take environment variables from .env.
1418

@@ -27,11 +31,11 @@ def get_result_from_query(query: SelectOfScalar, session: Session):
2731
2832
:return: Row
2933
"""
30-
results = session.execute(query)
34+
results = session.exec(query)
3135
try:
3236
results = results.one_or_none()
3337
except MultipleResultsFound:
34-
results = session.execute(query)
38+
results = session.exec(query)
3539
results = results.first()
3640

3741
return results
@@ -187,7 +191,7 @@ def get_row(
187191
lazy_load_keys = [lazy_load_keys]
188192
for key in lazy_load_keys:
189193
stmnt = stmnt.options(lazyload(getattr(model, key)))
190-
results = session_inst.execute(stmnt)
194+
results = session_inst.exec(stmnt)
191195

192196
row = results.one_or_none()
193197

@@ -232,39 +236,19 @@ def get_rows(
232236
if stmnt is None:
233237
stmnt = select(model)
234238
if kwargs:
235-
if ["date" in x for x in kwargs] and any(
236-
x in y for y in kwargs for x in ("lte", "gte")
237-
):
238-
date_keys = [x for x in kwargs.keys() if "date" in x]
239-
for key in date_keys:
240-
if "lte" in key:
241-
model_key = key.replace("__lte", "")
242-
date_val = kwargs.pop(key)
243-
if isinstance(date_val, str):
244-
date_val = date_parse(date_val)
245-
stmnt = stmnt.where(
246-
getattr(model, model_key) < date_val
247-
)
248-
elif "gte" in key:
249-
model_key = key.replace("__gte", "")
250-
logger.info(model_key)
251-
date_val = kwargs.pop(key)
252-
if isinstance(date_val, str):
253-
date_val = date_parse(date_val)
254-
stmnt = stmnt.where(
255-
getattr(model, model_key) > date_val
256-
)
257-
else:
258-
date_val = kwargs.pop(key)
259-
if isinstance(date_val, str):
260-
date_val = date_parse(date_val)
261-
stmnt = stmnt.where(getattr(model, key) == date_val)
262-
elif "date" in kwargs:
263-
date_keys = [x for x in kwargs.keys() if "date" in x]
264-
for key in date_keys:
265-
stmnt = stmnt.where(getattr(model, key) == kwargs.pop(key))
266-
else:
267-
pass
239+
for key in kwargs:
240+
if "__lte" in key:
241+
model_key = key.replace("__lte", "")
242+
val = kwargs.pop(key)
243+
if is_date(val, fuzzy=True):
244+
val = date_parse(val)
245+
stmnt = stmnt.where(getattr(model, model_key) < val)
246+
elif "__gte" in key:
247+
model_key = key.replace("__gte", "")
248+
val = kwargs.pop(key)
249+
if is_date(val, fuzzy=True):
250+
val = date_parse(val)
251+
stmnt = stmnt.where(getattr(model, model_key) > val)
268252
sort_desc, sort_field = (
269253
kwargs.pop(x, None) for x in ("sort_desc", "sort_field")
270254
)
@@ -294,7 +278,7 @@ def get_rows(
294278
stmnt = stmnt.options(lazyload(getattr(model, key)))
295279

296280
stmnt = stmnt.offset(page - 1).limit(page_size)
297-
_result = session_inst.execute(stmnt)
281+
_result = session_inst.exec(stmnt)
298282
results = _result.all()
299283
success = True if len(results) > 0 else False
300284

@@ -317,7 +301,7 @@ def get_rows_within_id_list(
317301
:return:
318302
"""
319303
stmnt = select(model).where(getattr(model, pk_field).in_(id_str_list))
320-
results = session_inst.execute(stmnt)
304+
results = session_inst.exec(stmnt)
321305

322306
if results:
323307
success = True
@@ -344,7 +328,7 @@ def delete_row(
344328
"""
345329
success = False
346330
stmnt = select(model).where(getattr(model, pk_field) == id_str)
347-
results = session_inst.execute(stmnt)
331+
results = session_inst.exec(stmnt)
348332

349333
row = results.one_or_none()
350334

@@ -387,7 +371,7 @@ def bulk_upsert_mappings(
387371
index_elements=[getattr(model, x) for x in pk_fields],
388372
set_={k: getattr(stmnt.excluded, k) for k in payload[0].keys()},
389373
)
390-
session_inst.execute(stmnt)
374+
session_inst.exec(stmnt)
391375

392376
results = session_inst.scalars(
393377
stmnt.returning(model), execution_options={"populate_existing": True}
@@ -406,9 +390,18 @@ def update_row(
406390
model: type[SQLModel],
407391
pk_field: str = "id",
408392
):
393+
"""
394+
395+
:param id_str:
396+
:param data:
397+
:param session_inst:
398+
:param model:
399+
:param pk_field:
400+
:return:
401+
"""
409402
success = False
410403
stmnt = select(model).where(getattr(model, pk_field) == id_str)
411-
results = session_inst.execute(stmnt)
404+
results = session_inst.exec(stmnt)
412405

413406
row = results.one_or_none()
414407

sqlmodel_crud_utils/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import importlib
22
import os
33

4+
from dateutil.parser import parse as date_parse
5+
46

57
def get_val(val: str):
68
"""
@@ -24,3 +26,20 @@ def get_sql_dialect_import(dialect: str):
2426
:return: func
2527
"""
2628
return importlib.import_module(f"sqlalchemy.dialects" f".{dialect}").insert
29+
30+
31+
def is_date(val: str, fuzzy: bool = False):
32+
"""
33+
A simple utility to check if string is a possible datetime value. Returns
34+
False if not.
35+
36+
:param val: str
37+
:param fuzzy: bool = False
38+
:return:
39+
bool
40+
"""
41+
try:
42+
date_parse(val, fuzzy=fuzzy)
43+
return True
44+
except ValueError:
45+
return False

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)