Added support for multiple int where's
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"""Helper functions for interacting with filament database objects."""
|
||||
|
||||
import logging
|
||||
from typing import Optional
|
||||
from collections.abc import Sequence
|
||||
from typing import Optional, Union
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
@@ -77,7 +78,7 @@ async def find(
|
||||
*,
|
||||
db: AsyncSession,
|
||||
vendor_name: Optional[str] = None,
|
||||
vendor_id: Optional[int] = None,
|
||||
vendor_id: Optional[Union[int, Sequence[int]]] = None,
|
||||
name: Optional[str] = None,
|
||||
material: Optional[str] = None,
|
||||
article_number: Optional[str] = None,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""Helper functions for interacting with spool database objects."""
|
||||
|
||||
from collections.abc import Sequence
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
from typing import Optional, Union
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import case, func
|
||||
@@ -87,10 +88,10 @@ async def find(
|
||||
*,
|
||||
db: AsyncSession,
|
||||
filament_name: Optional[str] = None,
|
||||
filament_id: Optional[int] = None,
|
||||
filament_id: Optional[Union[int, Sequence[int]]] = None,
|
||||
filament_material: Optional[str] = None,
|
||||
vendor_name: Optional[str] = None,
|
||||
vendor_id: Optional[int] = None,
|
||||
vendor_id: Optional[Union[int, Sequence[int]]] = None,
|
||||
location: Optional[str] = None,
|
||||
lot_nr: Optional[str] = None,
|
||||
allow_archived: bool = False,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""Utility functions for the database module."""
|
||||
|
||||
from collections.abc import Sequence
|
||||
from enum import Enum
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Optional, Union
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import Select
|
||||
@@ -68,23 +69,30 @@ def add_where_clause_str(
|
||||
def add_where_clause_int(
|
||||
stmt: Select,
|
||||
field: attributes.InstrumentedAttribute[int],
|
||||
value: Optional[int],
|
||||
value: Optional[Union[int, Sequence[int]]],
|
||||
) -> Select:
|
||||
"""Add a where clause to a select statement for a field."""
|
||||
if value is not None:
|
||||
stmt = stmt.where(field == value)
|
||||
if isinstance(value, int):
|
||||
value = [value]
|
||||
stmt = stmt.where(field.in_(value))
|
||||
return stmt
|
||||
|
||||
|
||||
def add_where_clause_int_opt(
|
||||
stmt: Select,
|
||||
field: attributes.InstrumentedAttribute[Optional[int]],
|
||||
value: Optional[int],
|
||||
value: Optional[Union[int, Sequence[int]]],
|
||||
) -> Select:
|
||||
"""Add a where clause to a select statement for a field."""
|
||||
if value is not None:
|
||||
if value == -1:
|
||||
stmt = stmt.where(field.is_(None))
|
||||
else:
|
||||
stmt = stmt.where(field == value)
|
||||
if isinstance(value, int):
|
||||
value = [value]
|
||||
statements = []
|
||||
for value_part in value:
|
||||
if value_part == -1:
|
||||
statements.append(field.is_(None))
|
||||
else:
|
||||
statements.append(field == value_part)
|
||||
stmt = stmt.where(sqlalchemy.or_(*statements))
|
||||
return stmt
|
||||
|
||||
Reference in New Issue
Block a user