aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/py/lib/db.py
blob: 9fddbf5152937d73a63b33a53c7ad273d36f3e12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "cimple" project.
# For details, see https://github.com/egor-tensin/cimple.
# Distributed under the MIT License.

from contextlib import closing, contextmanager
import logging
import sqlite3


class Database:
    def __init__(self, path):
        logging.info('Opening SQLite database: %s', path)
        self.conn = sqlite3.connect(f'file:{path}?mode=ro', uri=True)

    def __enter__(self):
        return self

    def __exit__(*args):
        self.conn.close()

    @contextmanager
    def get_cursor(self):
        with closing(self.conn.cursor()) as cur:
            yield cur

    def get_all_runs(self):
        with self.get_cursor() as cur:
            cur.execute('SELECT * FROM cimple_runs_readable')
            return cur.fetchall()