home · contact · privacy
Hide (almost all) remaining SQL code in DB module.
[plomtask] / plomtask / db.py
1 """Database management."""
2 from os.path import isfile
3 from difflib import Differ
4 from sqlite3 import connect as sql_connect, Cursor, Row
5 from typing import Any, Dict
6 from plomtask.exceptions import HandledException
7
8 PATH_DB_SCHEMA = 'scripts/init.sql'
9
10
11 class DatabaseFile:  # pylint: disable=too-few-public-methods
12     """Represents the sqlite3 database's file."""
13
14     def __init__(self, path: str) -> None:
15         self.path = path
16         self._check()
17
18     def remake(self) -> None:
19         """Create tables in self.path file as per PATH_DB_SCHEMA sql file."""
20         with sql_connect(self.path) as conn:
21             with open(PATH_DB_SCHEMA, 'r', encoding='utf-8') as f:
22                 conn.executescript(f.read())
23         self._check()
24
25     def _check(self) -> None:
26         """Check file exists, and is of proper schema."""
27         self.exists = isfile(self.path)
28         if self.exists:
29             self._validate_schema()
30
31     def _validate_schema(self) -> None:
32         """Compare found schema with what's stored at PATH_DB_SCHEMA."""
33         sql_for_schema = 'SELECT sql FROM sqlite_master ORDER BY sql'
34         msg_err = 'Database has wrong tables schema. Diff:\n'
35         with sql_connect(self.path) as conn:
36             schema_rows = [r[0] for r in conn.execute(sql_for_schema) if r[0]]
37             retrieved_schema = ';\n'.join(schema_rows) + ';'
38             with open(PATH_DB_SCHEMA, 'r', encoding='utf-8') as f:
39                 stored_schema = f.read().rstrip()
40                 if stored_schema != retrieved_schema:
41                     diff_msg = Differ().compare(retrieved_schema.splitlines(),
42                                                 stored_schema.splitlines())
43                     raise HandledException(msg_err + '\n'.join(diff_msg))
44
45
46 class DatabaseConnection:
47     """A single connection to the database."""
48
49     def __init__(self, db_file: DatabaseFile) -> None:
50         self.file = db_file
51         self.conn = sql_connect(self.file.path)
52         self.cached_todos: Dict[int, Any] = {}
53         self.cached_days: Dict[str, Any] = {}
54         self.cached_process_steps: Dict[int, Any] = {}
55         self.cached_processes: Dict[int, Any] = {}
56         self.cached_conditions: Dict[int, Any] = {}
57
58     def commit(self) -> None:
59         """Commit SQL transaction."""
60         self.conn.commit()
61
62     def exec(self, code: str, inputs: tuple[Any, ...] = tuple()) -> Cursor:
63         """Add commands to SQL transaction."""
64         return self.conn.execute(code, inputs)
65
66     def close(self) -> None:
67         """Close DB connection."""
68         self.conn.close()
69
70     def rewrite_relations(self, table_name: str, key: str, target: int,
71                           rows: list[list[Any]]) -> None:
72         """Rewrite relations in table_name to target, with rows values."""
73         self.delete_where(table_name, key, target)
74         for row in rows:
75             values = tuple([target] + row)
76             q_marks = self.__class__.q_marks_from_values(values)
77             self.exec(f'INSERT INTO {table_name} VALUES {q_marks}', values)
78
79     def row_where(self, table_name: str, key: str,
80                   target: int | str) -> list[Row]:
81         """Return list of Rows at table where key == target."""
82         return list(self.exec(f'SELECT * FROM {table_name} WHERE {key} = ?',
83                               (target,)))
84
85     def column_where(self, table_name: str, column: str, key: str,
86                      target: int | str) -> list[Any]:
87         """Return column of table where key == target."""
88         return [row[0] for row in
89                 self.exec(f'SELECT {column} FROM {table_name} '
90                           f'WHERE {key} = ?', (target,))]
91
92     def column_all(self, table_name: str, column: str) -> list[Any]:
93         """Return complete column of table."""
94         return [row[0] for row in
95                 self.exec(f'SELECT {column} FROM {table_name}')]
96
97     def delete_where(self, table_name: str, key: str, target: int) -> None:
98         """Delete from table where key == target."""
99         self.exec(f'DELETE FROM {table_name} WHERE {key} = ?', (target,))
100
101     @staticmethod
102     def q_marks_from_values(values: tuple[Any]) -> str:
103         """Return placeholder to insert values into SQL code."""
104         return '(' + ','.join(['?'] * len(values)) + ')'
105
106
107 class BaseModel:
108     """Template for most of the models we use/derive from the DB."""
109     table_name = ''
110     to_save: list[str] = []
111     id_: None | int | str
112     id_type: type[Any] = int
113
114     @classmethod
115     def from_table_row(cls, db_conn: DatabaseConnection,
116                        row: Row | list[Any]) -> Any:
117         """Make from DB row, write to DB cache."""
118         obj = cls(*row)
119         assert isinstance(obj.id_, cls.id_type)
120         cache = getattr(db_conn, f'cached_{cls.table_name}')
121         cache[obj.id_] = obj
122         return obj
123
124     @classmethod
125     def _by_id(cls,
126                db_conn: DatabaseConnection,
127                id_: int | str) -> tuple[Any, bool]:
128         """Return instance found by ID, or None, and if from cache or not."""
129         from_cache = False
130         obj = None
131         cache = getattr(db_conn, f'cached_{cls.table_name}')
132         if id_ in cache.keys():
133             obj = cache[id_]
134             from_cache = True
135         else:
136             for row in db_conn.row_where(cls.table_name, 'id', id_):
137                 obj = cls.from_table_row(db_conn, row)
138                 cache[id_] = obj
139                 break
140         return obj, from_cache
141
142     def set_int_id(self, id_: int | None) -> None:
143         """Set id_ if >= 1 or None, else fail."""
144         if (id_ is not None) and id_ < 1:
145             msg = f'illegal {self.__class__.__name__} ID, must be >=1: {id_}'
146             raise HandledException(msg)
147         self.id_ = id_
148
149     def save_core(self, db_conn: DatabaseConnection,
150                   update_with_lastrowid: bool = True) -> None:
151         """Write bare-bones self (sans connected items), ensuring self.id_."""
152         values = tuple([self.id_] + [getattr(self, key)
153                                      for key in self.to_save])
154         q_marks = DatabaseConnection.q_marks_from_values(values)
155         table_name = self.table_name
156         cursor = db_conn.exec(f'REPLACE INTO {table_name} VALUES {q_marks}',
157                               values)
158         if update_with_lastrowid:
159             self.id_ = cursor.lastrowid
160         cache = getattr(db_conn, f'cached_{table_name}')
161         cache[self.id_] = self