Skip to content

reader

DB utils for getting custom list data from the DB.

DBReader

Class encapsulating functions to read custom list related data from the DB.

Source code in mlte/store/custom_list/underlying/rdbs/reader.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class DBReader:
    """Class encapsulating functions to read custom list related data from the DB."""

    @staticmethod
    def get_entry(
        name: str, session: Session
    ) -> tuple[CustomListEntryModel, DBCustomListEntry]:
        """Reads the custom list entry with the given name using the provided session and returns a CustomListEntryModel and DBCustomListEntry."""
        entry_orm = session.scalar(
            select(DBCustomListEntry).where(DBCustomListEntry.name == name)
        )

        if entry_orm is None:
            raise errors.ErrorNotFound(
                f"Custom List Entry with name {name} was not found in custom list store."
            )
        else:
            return (
                DBReader.create_custom_list_entry_model(entry_orm),
                entry_orm,
            )

    @staticmethod
    def get_list(
        list_name: str, session: Session
    ) -> tuple[list[CustomListEntryModel], list[DBCustomListEntry]]:
        """Reads all entries in specified list in the DB and returns list of CustomListEntryModel and DBCustomLIstEntry objects."""
        list_orm = list(
            session.scalars(
                select(DBCustomListEntry).where(
                    DBCustomListEntry.list_name == list_name
                )
            )
        )
        entries: list[CustomListEntryModel] = []
        for entry_orm in list_orm:
            entry = DBReader.create_custom_list_entry_model(entry_orm)
            entries.append(entry)

        return entries, list_orm

    @staticmethod
    def create_custom_list_entry_orm(
        entry: CustomListEntryModel,
        list_name: CustomListName,
        session: Session,
        entry_orm: Optional[DBCustomListEntry] = None,
    ) -> DBCustomListEntry:
        """Creates or updates the DB object from the corresponding internal model."""
        if entry_orm is None:
            entry_orm = DBCustomListEntry()
        if entry.parent:
            _, entry_orm.parent = DBReader.get_entry(entry.parent, session)

        entry_orm.list_name = list_name
        entry_orm.name = entry.name
        entry_orm.description = entry.description

        return entry_orm

    @staticmethod
    def create_custom_list_entry_model(
        entry_orm: DBCustomListEntry,
    ) -> CustomListEntryModel:
        """Creates the internal model object from the corresponding DB object."""
        return CustomListEntryModel(
            name=entry_orm.name,
            description=entry_orm.description,
            parent=entry_orm.parent.name if entry_orm.parent else None,
        )

create_custom_list_entry_model(entry_orm) staticmethod

Creates the internal model object from the corresponding DB object.

Source code in mlte/store/custom_list/underlying/rdbs/reader.py
74
75
76
77
78
79
80
81
82
83
@staticmethod
def create_custom_list_entry_model(
    entry_orm: DBCustomListEntry,
) -> CustomListEntryModel:
    """Creates the internal model object from the corresponding DB object."""
    return CustomListEntryModel(
        name=entry_orm.name,
        description=entry_orm.description,
        parent=entry_orm.parent.name if entry_orm.parent else None,
    )

create_custom_list_entry_orm(entry, list_name, session, entry_orm=None) staticmethod

Creates or updates the DB object from the corresponding internal model.

Source code in mlte/store/custom_list/underlying/rdbs/reader.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@staticmethod
def create_custom_list_entry_orm(
    entry: CustomListEntryModel,
    list_name: CustomListName,
    session: Session,
    entry_orm: Optional[DBCustomListEntry] = None,
) -> DBCustomListEntry:
    """Creates or updates the DB object from the corresponding internal model."""
    if entry_orm is None:
        entry_orm = DBCustomListEntry()
    if entry.parent:
        _, entry_orm.parent = DBReader.get_entry(entry.parent, session)

    entry_orm.list_name = list_name
    entry_orm.name = entry.name
    entry_orm.description = entry.description

    return entry_orm

get_entry(name, session) staticmethod

Reads the custom list entry with the given name using the provided session and returns a CustomListEntryModel and DBCustomListEntry.

Source code in mlte/store/custom_list/underlying/rdbs/reader.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@staticmethod
def get_entry(
    name: str, session: Session
) -> tuple[CustomListEntryModel, DBCustomListEntry]:
    """Reads the custom list entry with the given name using the provided session and returns a CustomListEntryModel and DBCustomListEntry."""
    entry_orm = session.scalar(
        select(DBCustomListEntry).where(DBCustomListEntry.name == name)
    )

    if entry_orm is None:
        raise errors.ErrorNotFound(
            f"Custom List Entry with name {name} was not found in custom list store."
        )
    else:
        return (
            DBReader.create_custom_list_entry_model(entry_orm),
            entry_orm,
        )

get_list(list_name, session) staticmethod

Reads all entries in specified list in the DB and returns list of CustomListEntryModel and DBCustomLIstEntry objects.

Source code in mlte/store/custom_list/underlying/rdbs/reader.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@staticmethod
def get_list(
    list_name: str, session: Session
) -> tuple[list[CustomListEntryModel], list[DBCustomListEntry]]:
    """Reads all entries in specified list in the DB and returns list of CustomListEntryModel and DBCustomLIstEntry objects."""
    list_orm = list(
        session.scalars(
            select(DBCustomListEntry).where(
                DBCustomListEntry.list_name == list_name
            )
        )
    )
    entries: list[CustomListEntryModel] = []
    for entry_orm in list_orm:
        entry = DBReader.create_custom_list_entry_model(entry_orm)
        entries.append(entry)

    return entries, list_orm