aboutsummaryrefslogtreecommitdiff
path: root/qc_app/db/averaging.py
blob: 62bbe6704ec8778312794fdfa81d7031ff59d281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Functions for db interactions for averaging methods"""
from typing import Optional

import MySQLdb as mdb
from MySQLdb.cursors import DictCursor

def averaging_methods(conn: mdb.Connection) -> tuple[dict, ...]:
    """Fetch all available averaging methods"""
    with conn.cursor(cursorclass=DictCursor) as cursor:
        cursor.execute("SELECT * FROM AvgMethod")
        return tuple(dict(row) for row in cursor.fetchall())

def averaging_method_by_id(
        conn: mdb.Connection, averageid: int) -> Optional[dict]:
    """Fetch the averaging method by its ID"""
    with conn.cursor(cursorclass=DictCursor) as cursor:
        cursor.execute("SELECT * FROM AvgMethod WHERE Id=%s",
                       (averageid,))
        result = cursor.fetchone()
        if bool(result):
            return dict(result)

    return None