mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
160 lines
4.8 KiB
Python
160 lines
4.8 KiB
Python
from collections.abc import Collection, Sequence
|
|
from typing import Any
|
|
|
|
from sqlalchemy import Select, delete, desc, or_, select, update
|
|
from sqlalchemy.orm import Session
|
|
|
|
from decorators.database import INJECTED_SESSION, begin_session
|
|
from models.assets import State
|
|
from models.base import with_file_name_parts
|
|
from models.rom import Rom
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
class DBStatesHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_state(
|
|
self,
|
|
state: State,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> State:
|
|
return session.merge(state)
|
|
|
|
@begin_session
|
|
def get_state(
|
|
self,
|
|
user_id: int,
|
|
id: int,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> State | None:
|
|
return session.scalar(select(State).filter_by(user_id=user_id, id=id).limit(1))
|
|
|
|
@begin_session
|
|
def get_state_by_filename(
|
|
self,
|
|
user_id: int,
|
|
rom_id: int,
|
|
file_name: str,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> State | None:
|
|
return session.scalar(
|
|
select(State)
|
|
.filter_by(rom_id=rom_id, user_id=user_id, file_name=file_name)
|
|
.limit(1)
|
|
)
|
|
|
|
def _states_query(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
) -> Select[tuple[State]]:
|
|
query = select(State).filter_by(user_id=user_id)
|
|
|
|
# An empty collection is an explicit empty scope, not an absent filter.
|
|
if rom_ids is not None:
|
|
query = query.filter(State.rom_id.in_(rom_ids))
|
|
|
|
if platform_id:
|
|
query = query.join(Rom, State.rom_id == Rom.id).filter(
|
|
Rom.platform_id == platform_id
|
|
)
|
|
|
|
return query
|
|
|
|
@begin_session
|
|
def get_states(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> Sequence[State]:
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def get_state_ids(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> list[int]:
|
|
"""Ids only, so no `State` is built and no eager rom or user join fires."""
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return list(session.scalars(query.with_only_columns(State.id)).all())
|
|
|
|
@begin_session
|
|
def get_state_by_id(
|
|
self,
|
|
id: int,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> State | None:
|
|
"""Fetch a state by id without scoping to an owner. Used for the
|
|
visibility toggle and community downloads, where the caller may not own
|
|
the state. Mirrors db_screenshot_handler.get_screenshot_by_id."""
|
|
return session.get(State, id)
|
|
|
|
@begin_session
|
|
def get_rom_shared_states(
|
|
self,
|
|
rom_id: int,
|
|
user_id: int,
|
|
public_only: bool = False,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> Sequence[State]:
|
|
"""States for a ROM visible to the requesting user: own (public +
|
|
private) plus other users' public ones. Mirrors
|
|
db_screenshot_handler.get_rom_gallery_screenshots."""
|
|
query = select(State).filter(State.rom_id == rom_id)
|
|
|
|
if public_only:
|
|
query = query.filter(State.is_public)
|
|
else:
|
|
query = query.filter(or_(State.user_id == user_id, State.is_public))
|
|
|
|
query = query.order_by(desc(State.updated_at))
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def update_state(
|
|
self,
|
|
id: int,
|
|
data: dict[str, Any],
|
|
touch: bool = True,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> State:
|
|
"""Write `data` onto a state.
|
|
|
|
Args:
|
|
touch: False keeps `updated_at`, since annotating is not a write
|
|
to the bytes and device sync reads it to detect staleness.
|
|
"""
|
|
data = with_file_name_parts(data)
|
|
values = data if touch else {**data, "updated_at": State.updated_at}
|
|
session.execute(
|
|
update(State)
|
|
.where(State.id == id)
|
|
.values(**values)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return session.scalars(select(State).filter_by(id=id)).one()
|
|
|
|
@begin_session
|
|
def delete_state(
|
|
self,
|
|
id: int,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> None:
|
|
session.execute(
|
|
delete(State)
|
|
.where(State.id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|