Added support for finding empty vendor fields

This commit is contained in:
Donkie
2023-09-08 20:31:50 +02:00
parent 3b8db0b5f7
commit f47eacd2a9
3 changed files with 24 additions and 12 deletions

View File

@@ -11,7 +11,7 @@ from sqlalchemy.orm import contains_eager, joinedload
from spoolman.database import models, vendor from spoolman.database import models, vendor
from spoolman.database.utils import ( from spoolman.database.utils import (
SortOrder, SortOrder,
add_where_clause, add_where_clause_int_opt,
add_where_clause_str, add_where_clause_str,
add_where_clause_str_opt, add_where_clause_str_opt,
parse_nested_field, parse_nested_field,
@@ -98,7 +98,7 @@ async def find(
.join(models.Filament.vendor, isouter=True) .join(models.Filament.vendor, isouter=True)
) )
stmt = add_where_clause(stmt, models.Filament.vendor_id, vendor_id) stmt = add_where_clause_int_opt(stmt, models.Filament.vendor_id, vendor_id)
stmt = add_where_clause_str(stmt, models.Vendor.name, vendor_name) stmt = add_where_clause_str(stmt, models.Vendor.name, vendor_name)
stmt = add_where_clause_str_opt(stmt, models.Filament.name, name) stmt = add_where_clause_str_opt(stmt, models.Filament.name, name)
stmt = add_where_clause_str_opt(stmt, models.Filament.material, material) stmt = add_where_clause_str_opt(stmt, models.Filament.material, material)

View File

@@ -12,7 +12,8 @@ from sqlalchemy.orm import contains_eager, joinedload
from spoolman.database import filament, models from spoolman.database import filament, models
from spoolman.database.utils import ( from spoolman.database.utils import (
SortOrder, SortOrder,
add_where_clause, add_where_clause_int,
add_where_clause_int_opt,
add_where_clause_str, add_where_clause_str,
add_where_clause_str_opt, add_where_clause_str_opt,
parse_nested_field, parse_nested_field,
@@ -111,8 +112,8 @@ async def find(
.options(contains_eager(models.Spool.filament).contains_eager(models.Filament.vendor)) .options(contains_eager(models.Spool.filament).contains_eager(models.Filament.vendor))
) )
stmt = add_where_clause(stmt, models.Spool.filament_id, filament_id) stmt = add_where_clause_int(stmt, models.Spool.filament_id, filament_id)
stmt = add_where_clause(stmt, models.Filament.vendor_id, vendor_id) stmt = add_where_clause_int_opt(stmt, models.Filament.vendor_id, vendor_id)
stmt = add_where_clause_str(stmt, models.Vendor.name, vendor_name) stmt = add_where_clause_str(stmt, models.Vendor.name, vendor_name)
stmt = add_where_clause_str_opt(stmt, models.Filament.name, filament_name) stmt = add_where_clause_str_opt(stmt, models.Filament.name, filament_name)
stmt = add_where_clause_str_opt(stmt, models.Filament.material, filament_material) stmt = add_where_clause_str_opt(stmt, models.Filament.material, filament_material)

View File

@@ -1,7 +1,7 @@
"""Utility functions for the database module.""" """Utility functions for the database module."""
from enum import Enum from enum import Enum
from typing import Any, Optional, TypeVar from typing import Any, Optional
import sqlalchemy import sqlalchemy
from sqlalchemy import Select from sqlalchemy import Select
@@ -65,15 +65,26 @@ def add_where_clause_str(
return stmt return stmt
T = TypeVar("T") def add_where_clause_int(
def add_where_clause(
stmt: Select, stmt: Select,
field: attributes.InstrumentedAttribute[T], field: attributes.InstrumentedAttribute[int],
value: Optional[T], value: Optional[int],
) -> Select: ) -> Select:
"""Add a where clause to a select statement for a field.""" """Add a where clause to a select statement for a field."""
if value is not None: if value is not None:
stmt = stmt.where(field == value) stmt = stmt.where(field == value)
return stmt return stmt
def add_where_clause_int_opt(
stmt: Select,
field: attributes.InstrumentedAttribute[Optional[int]],
value: Optional[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)
return stmt