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.utils import (
SortOrder,
add_where_clause,
add_where_clause_int_opt,
add_where_clause_str,
add_where_clause_str_opt,
parse_nested_field,
@@ -98,7 +98,7 @@ async def find(
.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_opt(stmt, models.Filament.name, name)
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.utils import (
SortOrder,
add_where_clause,
add_where_clause_int,
add_where_clause_int_opt,
add_where_clause_str,
add_where_clause_str_opt,
parse_nested_field,
@@ -111,8 +112,8 @@ async def find(
.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(stmt, models.Filament.vendor_id, vendor_id)
stmt = add_where_clause_int(stmt, models.Spool.filament_id, filament_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_opt(stmt, models.Filament.name, filament_name)
stmt = add_where_clause_str_opt(stmt, models.Filament.material, filament_material)

View File

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