from collections.abc import Callable from typing import Optional from sqlalchemy import Boolean, Column from sqlalchemy.engine.interfaces import Dialect from sqlmodel import Field, SQLModel class _Bit1Bool(Boolean): r"""Boolean subtype that correctly reads MySQL BIT(1) columns. aiomysql returns BIT(1) values as bytes (b'\x00' / b'\x01'). SQLAlchemy's standard Boolean result-processor calls bool(), which makes b'\x00' truthy (any non-empty bytes object is truthy in Python). We override result_processor to intercept the raw bytes before that coercion, while keeping normal int/bool handling for SQLite. """ cache_ok = True def result_processor( self, dialect: Dialect, coltype: object ) -> Callable[[object], bool]: # type: ignore[override] def process(value: object) -> bool: if isinstance(value, (bytes, bytearray)): return value not in (b"\x00", b"") if value is None: return False return bool(value) return process class Application(SQLModel, table=True): """Maps to tblApplicationInstance. Mirrors Sony.Filtr.Contracts.Entities.Application. """ __tablename__ = "tblApplicationInstance" # PK id: Optional[int] = Field( default=None, sa_column_kwargs={"name": "intApplicationInstanceID"}, primary_key=True, ) name: Optional[str] = Field(default=None, sa_column_kwargs={"name": "strName"}) language_id: Optional[str] = Field( default=None, sa_column_kwargs={"name": "strLanguageID"} ) spotify_region_code: Optional[str] = Field( default=None, sa_column_kwargs={"name": "strSpotifyRegionID"} ) main_application: bool = Field( default=False, sa_column_kwargs={"name": "blnMainApplication"} ) main_domain: Optional[str] = Field( default=None, sa_column_kwargs={"name": "strMainDomain"} ) active: bool = Field(default=False, sa_column_kwargs={"name": "blnActive"}) ga_country_name: Optional[str] = Field( default=None, sa_column_kwargs={"name": "strGACountryName"} ) fallback_application: bool = Field( default=False, sa_column_kwargs={"name": "blnFallbackApplication"} ) global_push_application: bool = Field( default=False, sa_column_kwargs={"name": "blnGlobalPush"} ) workout_market: bool = Field( default=False, sa_column=Column("blnWorkout", _Bit1Bool(), default=0) ) include_other_playlists: bool = Field( default=False, sa_column=Column("blnIncludeOtherPlaylists", _Bit1Bool(), default=0), ) default_service: Optional[str] = Field( default=None, sa_column_kwargs={"name": "strDefaultService"} ) service_list: Optional[str] = Field( default=None, sa_column_kwargs={"name": "strServiceList"} )