-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
47 lines (31 loc) · 1009 Bytes
/
database.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sqlite3
from functools import cache
from pathlib import Path
from sqlite3 import Connection
from job_model import Job
DATABASE_NAME = "job_database.db"
SQL_FOLDER = "sql"
def get_sql_from_file(filename: str) -> str:
sql_file = Path.cwd() / SQL_FOLDER / f"{filename}.sql"
return sql_file.read_text(encoding="utf-8")
@cache
def get_connect() -> Connection:
return sqlite3.connect(DATABASE_NAME)
def prepare() -> None:
conn = get_connect()
cursor = conn.cursor()
sql = get_sql_from_file("create_table_jobs")
cursor.execute(sql)
def save_db(jobs: list[Job]) -> None:
conn = get_connect()
cursor = conn.cursor()
sql = get_sql_from_file("insert_new_jobs")
cursor.executemany(sql, jobs)
conn.commit()
def get_jobs_from_date(date: str) -> list[Job]:
conn = get_connect()
cursor = conn.cursor()
sql = get_sql_from_file("select_jobs")
cursor.execute(sql, (date,))
jobs = cursor.fetchall()
return [Job(*job) for job in jobs]