Skip to content

Improve ORM definitions #1270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions python/lib/db/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DbFile(Base):
acquisition_order_per_modality : Mapped[int | None] = mapped_column('AcqOrderPerModality')
acquisition_date : Mapped[date | None] = mapped_column('AcquisitionDate')

session : Mapped['db_session.DbSession'] = relationship('DbSession', back_populates='files')
parameters: Mapped[list['db_parameter_file.DbParameterFile']] \
session : Mapped['db_session.DbSession'] \
= relationship('DbSession', back_populates='files')
parameters : Mapped[list['db_parameter_file.DbParameterFile']] \
= relationship('DbParameterFile', back_populates='file')
10 changes: 10 additions & 0 deletions python/lib/db/models/imaging_file_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from sqlalchemy.orm import Mapped, mapped_column

from lib.db.base import Base


class DbImagingFileType(Base):
__tablename__ = 'ImagingFileTypes'

type : Mapped[str] = mapped_column('type', primary_key=True)
description : Mapped[str | None] = mapped_column('description')
2 changes: 1 addition & 1 deletion python/lib/db/models/mri_violation_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ class DbMriViolationLog(Base):
= relationship('DbCandidate', back_populates='violations_log')
scan_type : Mapped[Optional['db_mri_scan_type.DbMriScanType']] \
= relationship('DbMriScanType', back_populates='violations_log')
protocol_check_group: Mapped[Optional['db_mri_protocol_check_group.DbMriProtocolCheckGroup']] \
protocol_check_group : Mapped[Optional['db_mri_protocol_check_group.DbMriProtocolCheckGroup']] \
= relationship('DbMriProtocolCheckGroup', back_populates='violations_log')
6 changes: 4 additions & 2 deletions python/lib/db/models/parameter_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ class DbParameterFile(Base):
value : Mapped[str | None] = mapped_column('Value')
insert_time : Mapped[int] = mapped_column('InsertTime')

file: Mapped[list['db_file.DbFile']] = relationship('DbFile', back_populates='parameters')
type: Mapped['db_parameter_type.DbParameterType'] = relationship('DbParameterType', back_populates='parameter_file')
file: Mapped['db_file.DbFile'] \
= relationship('DbFile', back_populates='parameters')
type: Mapped['db_parameter_type.DbParameterType'] \
= relationship('DbParameterType', back_populates='parameter_files')
2 changes: 1 addition & 1 deletion python/lib/db/models/parameter_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class DbParameterType(Base):
queryable : Mapped[bool | None] = mapped_column('Queryable')
is_file : Mapped[bool | None] = mapped_column('IsFile')

parameter_file: Mapped[list['db_parameter_file.DbParameterFile']] \
parameter_files: Mapped[list['db_parameter_file.DbParameterFile']] \
= relationship('DbParameterFile', back_populates='type')
3 changes: 1 addition & 2 deletions python/lib/db/queries/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

def try_get_candidate_with_cand_id(db: Database, cand_id: int) -> DbCandidate | None:
"""
Get a candidate from the database using its CandID, or return `None` if no candidate is
found.
Get a candidate from the database using its CandID, or return `None` if no candidate is found.
"""

return db.execute(select(DbCandidate)
Expand Down
14 changes: 14 additions & 0 deletions python/lib/db/queries/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ def try_get_parameter_value_with_file_id_parameter_name(
.where(DbParameterType.name == parameter_name)
.where(DbParameterFile.file_id == file_id)
).scalar_one_or_none()


def try_get_file_with_hash(db: Database, file_hash: str) -> DbFile | None:
"""
Get an imaging file from the database using its BLAKE2b or MD5 hash, or return `None` if no
imaging file is found.
"""

return db.execute(select(DbFile)
.join(DbFile.parameters)
.join(DbParameterFile.type)
.where(DbParameterType.name.in_(['file_blake2b_hash', 'md5hash']))
.where(DbParameterFile.value == file_hash)
).scalar_one_or_none()
14 changes: 14 additions & 0 deletions python/lib/db/queries/imaging_file_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from collections.abc import Sequence

from sqlalchemy import select
from sqlalchemy.orm import Session as Database

from lib.db.models.imaging_file_type import DbImagingFileType


def get_all_imaging_file_types(db: Database) -> Sequence[DbImagingFileType]:
"""
Get a sequence of all imaging file types from the database.
"""

return db.execute(select(DbImagingFileType)).scalars().all()
15 changes: 15 additions & 0 deletions python/lib/db/queries/mri_scan_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from sqlalchemy import select
from sqlalchemy.orm import Session as Database

from lib.db.models.mri_scan_type import DbMriScanType


def try_get_mri_scan_type_with_name(db: Database, name: str) -> DbMriScanType | None:
"""
Get an MRI scan type from the database using its name, or return `None` if no scan type is
found.
"""

return db.execute(select(DbMriScanType)
.where(DbMriScanType.name == name)
).scalar_one_or_none()
14 changes: 14 additions & 0 deletions python/lib/db/queries/parameter_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from collections.abc import Sequence

from sqlalchemy import select
from sqlalchemy.orm import Session as Database

from lib.db.models.parameter_type import DbParameterType


def get_all_parameter_types(db: Database) -> Sequence[DbParameterType]:
"""
Get a sequence of all parameter types from the database.
"""

return db.execute(select(DbParameterType)).scalars().all()
Loading