Skip to content

Commit 79f2e22

Browse files
authored
refactor: apply_* take table instead of column mapping (#900)
1 parent 5c93b67 commit 79f2e22

2 files changed

Lines changed: 9 additions & 13 deletions

File tree

diracx-db/src/diracx/db/sql/utils/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ async def _search(
273273

274274
stmt = select(*columns)
275275

276-
stmt = apply_search_filters(table.__table__.columns.__getitem__, stmt, search)
277-
stmt = apply_sort_constraints(table.__table__.columns.__getitem__, stmt, sorts)
276+
stmt = apply_search_filters(table.__table__, stmt, search)
277+
stmt = apply_sort_constraints(table.__table__, stmt, sorts)
278278

279279
if distinct:
280280
stmt = stmt.distinct()
@@ -314,7 +314,7 @@ async def _summary(
314314
count_col = pk_columns[0]
315315

316316
stmt = select(*columns, func.count(count_col).label("count"))
317-
stmt = apply_search_filters(table.__table__.columns.__getitem__, stmt, search)
317+
stmt = apply_search_filters(table.__table__, stmt, search)
318318
stmt = stmt.group_by(*columns)
319319

320320
# Execute the query
@@ -453,10 +453,10 @@ def _build_datetime_range_multi_expr(
453453
)
454454

455455

456-
def apply_search_filters(column_mapping, stmt, search):
456+
def apply_search_filters(table, stmt, search):
457457
for query in search:
458458
try:
459-
column = column_mapping(query["parameter"])
459+
column = table.columns[query["parameter"]]
460460
except KeyError as e:
461461
raise InvalidQueryError(f"Unknown column {query['parameter']}") from e
462462

@@ -525,11 +525,11 @@ def apply_search_filters(column_mapping, stmt, search):
525525
return stmt
526526

527527

528-
def apply_sort_constraints(column_mapping, stmt, sorts):
528+
def apply_sort_constraints(table, stmt, sorts):
529529
sort_columns = []
530530
for sort in sorts or []:
531531
try:
532-
column = column_mapping(sort["parameter"])
532+
column = table.columns[sort["parameter"]]
533533
except KeyError as e:
534534
raise InvalidQueryError(
535535
f"Cannot sort by {sort['parameter']}: unknown column"

diracx-testing/src/diracx/testing/mock_osdb.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,10 @@ async def search(
128128
stmt = stmt.distinct()
129129

130130
# Apply filtering
131-
stmt = sql_utils.apply_search_filters(
132-
self._table.columns.__getitem__, stmt, search
133-
)
131+
stmt = sql_utils.apply_search_filters(self._table, stmt, search)
134132

135133
# Apply sorting
136-
stmt = sql_utils.apply_sort_constraints(
137-
self._table.columns.__getitem__, stmt, sorts
138-
)
134+
stmt = sql_utils.apply_sort_constraints(self._table, stmt, sorts)
139135

140136
# Apply pagination
141137
if page is not None:

0 commit comments

Comments
 (0)