Initial commit
This commit is contained in:
1
folkugat_web/dal/sql/__init__.py
Normal file
1
folkugat_web/dal/sql/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from ._connection import get_connection, Connection
|
||||
23
folkugat_web/dal/sql/_connection.py
Normal file
23
folkugat_web/dal/sql/_connection.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import sqlite3
|
||||
from contextlib import contextmanager
|
||||
from typing import Iterator, Optional
|
||||
|
||||
from folkugat_web.config.db import DB_FILE
|
||||
|
||||
Connection = sqlite3.Connection
|
||||
|
||||
|
||||
@contextmanager
|
||||
def get_connection(con: Optional[Connection] = None) -> Iterator[Connection]:
|
||||
if con:
|
||||
yield con
|
||||
else:
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
try:
|
||||
yield con
|
||||
con.commit()
|
||||
except Exception:
|
||||
con.rollback()
|
||||
raise
|
||||
finally:
|
||||
con.close()
|
||||
8
folkugat_web/dal/sql/ddl.py
Normal file
8
folkugat_web/dal/sql/ddl.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from folkugat_web.dal.sql import get_connection, sessions
|
||||
from folkugat_web.dal.sql.temes import ddl as temes_ddl
|
||||
|
||||
|
||||
def create_db():
|
||||
with get_connection() as con:
|
||||
sessions.create_db(con)
|
||||
temes_ddl.create_db(con)
|
||||
194
folkugat_web/dal/sql/sessions.py
Normal file
194
folkugat_web/dal/sql/sessions.py
Normal file
@@ -0,0 +1,194 @@
|
||||
import datetime
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
from folkugat_web.model import sessions as model
|
||||
from folkugat_web.model.sql import OrderCol, Range
|
||||
from folkugat_web.typing import OptionalListOrValue as OLV
|
||||
|
||||
|
||||
def create_db(con: Optional[Connection] = None):
|
||||
with get_connection(con) as con:
|
||||
create_sessions_table(con)
|
||||
|
||||
|
||||
def drop_sessions_table(con: Connection):
|
||||
query = "DROP TABLE IF EXISTS sessions"
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
|
||||
|
||||
def create_sessions_table(con: Connection):
|
||||
query = """
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
date TEXT NOT NULL,
|
||||
start_time TEXT NOT NULL,
|
||||
end_time TEXT NOT NULL,
|
||||
venue_name TEXT,
|
||||
venue_url TEXT,
|
||||
is_live BOOLEAN DEFAULT false
|
||||
)
|
||||
"""
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
|
||||
|
||||
def _session_to_row(sessio: model.Session) -> dict:
|
||||
return {
|
||||
'id': sessio.id,
|
||||
'date': sessio.date,
|
||||
'start_time': sessio.start_time.isoformat(),
|
||||
'end_time': sessio.end_time.isoformat(),
|
||||
'venue_name': sessio.venue.name,
|
||||
'venue_url': sessio.venue.url,
|
||||
'is_live': sessio.is_live,
|
||||
}
|
||||
|
||||
|
||||
def _row_to_session(row: tuple) -> model.Session:
|
||||
return model.Session(
|
||||
id=row[0],
|
||||
date=datetime.date.fromisoformat(row[1]),
|
||||
start_time=datetime.time.fromisoformat(row[2]),
|
||||
end_time=datetime.time.fromisoformat(row[3]),
|
||||
venue=model.SessionVenue(
|
||||
name=row[4],
|
||||
url=row[5],
|
||||
),
|
||||
is_live=row[6],
|
||||
)
|
||||
|
||||
|
||||
def insert_session(session: model.Session, con: Optional[Connection] = None):
|
||||
query = """
|
||||
INSERT INTO sessions
|
||||
(id, date, start_time, end_time, venue_name, venue_url, is_live)
|
||||
VALUES
|
||||
(:id, :date, :start_time, :end_time, :venue_name, :venue_url, :is_live)
|
||||
RETURNING *
|
||||
"""
|
||||
data = _session_to_row(session)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
row = cur.fetchone()
|
||||
return _row_to_session(row)
|
||||
|
||||
|
||||
def update_session(session: model.Session, con: Optional[Connection] = None):
|
||||
query = """
|
||||
UPDATE sessions SET
|
||||
date = :date, start_time = :start_time, end_time = :end_time,
|
||||
venue_name = :venue_name, venue_url = :venue_url, is_live = :is_live
|
||||
WHERE id = :id
|
||||
"""
|
||||
data = _session_to_row(session)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
|
||||
|
||||
def _filter_clause(session_id: Optional[int] = None,
|
||||
date_range: Optional[Range[datetime.date]] = None,
|
||||
is_live: Optional[bool] = None) -> tuple[str, dict]:
|
||||
filter_clauses = []
|
||||
filter_data = {}
|
||||
|
||||
if session_id is not None:
|
||||
filter_clauses.append(f"id = :session_id")
|
||||
filter_data["session_id"] = session_id
|
||||
|
||||
if date_range:
|
||||
if ub := date_range.upper_bound():
|
||||
operator = "<=" if ub[1] else "<"
|
||||
filter_clauses.append(f"date {operator} :date_ub")
|
||||
filter_data["date_ub"] = ub[0]
|
||||
if lb := date_range.lower_bound():
|
||||
operator = ">=" if lb[1] else ">"
|
||||
filter_clauses.append(f"date {operator} :date_lb")
|
||||
filter_data["date_lb"] = lb[0]
|
||||
|
||||
if is_live is not None:
|
||||
filter_clauses.append(f"is_live = :is_live")
|
||||
filter_data["is_live"] = is_live
|
||||
|
||||
if filter_clauses:
|
||||
filter_clause_str = " AND ".join(filter_clauses)
|
||||
filter_clause = f"WHERE {filter_clause_str}"
|
||||
return filter_clause, filter_data
|
||||
else:
|
||||
return "", {}
|
||||
|
||||
|
||||
def _order_clause(order_by: OLV[OrderCol[model.SessionCols]]) -> str:
|
||||
if not order_by:
|
||||
return ""
|
||||
if not isinstance(order_by, list):
|
||||
order_by = [order_by]
|
||||
|
||||
order_clauses = [f"{ocol.column.value} {ocol.order.value}" for ocol in order_by]
|
||||
order_clauses_str = " ".join(order_clauses)
|
||||
return f"ORDER BY {order_clauses_str}"
|
||||
|
||||
|
||||
def get_sessions(session_id: Optional[int] = None,
|
||||
date_range: Optional[Range[datetime.date]] = None,
|
||||
is_live: Optional[bool] = None,
|
||||
order_by: OLV[OrderCol[model.SessionCols]] = None,
|
||||
limit: Optional[int] = None, offset: Optional[int] = None,
|
||||
con: Optional[Connection] = None) -> list[model.Session]:
|
||||
clauses = []
|
||||
filter_clause, data = _filter_clause(session_id=session_id, date_range=date_range, is_live=is_live)
|
||||
if filter_clause:
|
||||
clauses.append(filter_clause)
|
||||
if order_clause := _order_clause(order_by=order_by):
|
||||
clauses.append(order_clause)
|
||||
if limit is not None:
|
||||
clauses.append("LIMIT :limit")
|
||||
data["limit"] = limit
|
||||
if offset is not None:
|
||||
clauses.append("OFFSET :offset")
|
||||
data["offset"] = offset
|
||||
|
||||
clauses_str = " ".join(clauses)
|
||||
query = f"""
|
||||
SELECT id, date, start_time, end_time, venue_name, venue_url, is_live
|
||||
FROM sessions
|
||||
{clauses_str}
|
||||
"""
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
return list(map(_row_to_session, cur.fetchall()))
|
||||
|
||||
|
||||
def delete_session_by_id(session_id: int, con: Optional[Connection] = None):
|
||||
query = """
|
||||
DELETE FROM sessions
|
||||
WHERE id = :id
|
||||
"""
|
||||
data = dict(id=session_id)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
|
||||
|
||||
def stop_live_sessions(con: Optional[Connection] = None):
|
||||
query = """
|
||||
UPDATE sessions SET is_live = false WHERE is_live = true
|
||||
"""
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
|
||||
|
||||
def set_live_session(session_id: int, con: Optional[Connection] = None):
|
||||
query = """
|
||||
UPDATE sessions SET is_live = true WHERE id = :id
|
||||
"""
|
||||
data = dict(id=session_id)
|
||||
with get_connection(con) as con:
|
||||
stop_live_sessions(con=con)
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
0
folkugat_web/dal/sql/temes/__init__.py
Normal file
0
folkugat_web/dal/sql/temes/__init__.py
Normal file
38
folkugat_web/dal/sql/temes/_conversion.py
Normal file
38
folkugat_web/dal/sql/temes/_conversion.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import datetime
|
||||
import json
|
||||
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
|
||||
def tema_to_row(tema: model.Tema) -> dict:
|
||||
return {
|
||||
'id': tema.id,
|
||||
'title': tema.title,
|
||||
'properties': json.dumps(list(map(lambda p: p.to_dict(), tema.properties))),
|
||||
'links': json.dumps(list(map(lambda l: l.to_dict(), tema.links))),
|
||||
'lyrics': json.dumps(list(map(lambda l: l.to_dict(), tema.lyrics))),
|
||||
'alternatives': json.dumps(tema.alternatives),
|
||||
'ngrams': json.dumps(tema.ngrams),
|
||||
'modification_date': tema.modification_date.isoformat(),
|
||||
'creation_date': tema.creation_date.isoformat(),
|
||||
'hidden': 1 if tema.hidden else 0,
|
||||
}
|
||||
|
||||
|
||||
def cell_to_ngrams(cell: str) -> model.NGrams:
|
||||
return {int(n): ngrams_ for n, ngrams_ in json.loads(cell).items()}
|
||||
|
||||
|
||||
def row_to_tema(row: tuple) -> model.Tema:
|
||||
return model.Tema(
|
||||
id=row[0],
|
||||
title=row[1],
|
||||
properties=list(map(model.Property.from_dict, json.loads(row[2]))),
|
||||
links=list(map(model.Link.from_dict, json.loads(row[3]))),
|
||||
lyrics=list(map(model.Lyrics.from_dict, json.loads(row[4]))),
|
||||
alternatives=json.loads(row[5]),
|
||||
ngrams=cell_to_ngrams(row[6]),
|
||||
modification_date=datetime.datetime.fromisoformat(row[7]),
|
||||
creation_date=datetime.datetime.fromisoformat(row[8]),
|
||||
hidden=bool(row[9]),
|
||||
)
|
||||
40
folkugat_web/dal/sql/temes/ddl.py
Normal file
40
folkugat_web/dal/sql/temes/ddl.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web import data
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
|
||||
from .write import insert_tema
|
||||
|
||||
|
||||
def create_db(con: Optional[Connection] = None):
|
||||
with get_connection(con) as con:
|
||||
drop_temes_table(con)
|
||||
create_temes_table(con)
|
||||
|
||||
for tema in data.TEMES:
|
||||
insert_tema(tema, con)
|
||||
|
||||
|
||||
def drop_temes_table(con: Connection):
|
||||
query = "DROP TABLE IF EXISTS temes"
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
|
||||
|
||||
def create_temes_table(con: Connection):
|
||||
query = """
|
||||
CREATE TABLE IF NOT EXISTS temes (
|
||||
id INTEGER PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
properties TEXT,
|
||||
links TEXT,
|
||||
lyrics TEXT,
|
||||
alternatives TEXT,
|
||||
ngrams TEXT,
|
||||
creation_date TEXT NOT NULL,
|
||||
modification_date TEXT NOT NULL,
|
||||
hidden INTEGER NOT NULL
|
||||
)
|
||||
"""
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
49
folkugat_web/dal/sql/temes/query.py
Normal file
49
folkugat_web/dal/sql/temes/query.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
from ._conversion import cell_to_ngrams, row_to_tema
|
||||
|
||||
TEMA_ID_TO_NGRAMS_CACHE = None
|
||||
|
||||
|
||||
def get_tema_by_id(tema_id: int, con: Optional[Connection] = None) -> Optional[model.Tema]:
|
||||
query = """
|
||||
SELECT
|
||||
id, title, properties, links, lyrics, alternatives, ngrams,
|
||||
creation_date, modification_date, hidden
|
||||
FROM temes
|
||||
WHERE id = :id
|
||||
"""
|
||||
data = dict(id=tema_id)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
row = cur.fetchone()
|
||||
return row_to_tema(row) if row else None
|
||||
|
||||
|
||||
def evict_tema_id_to_ngrams_cache():
|
||||
global TEMA_ID_TO_NGRAMS_CACHE
|
||||
TEMA_ID_TO_NGRAMS_CACHE = None
|
||||
|
||||
|
||||
def get_tema_id_to_ngrams(con: Optional[Connection] = None) -> dict[int, model.NGrams]:
|
||||
global TEMA_ID_TO_NGRAMS_CACHE
|
||||
if TEMA_ID_TO_NGRAMS_CACHE is None:
|
||||
TEMA_ID_TO_NGRAMS_CACHE = _get_tema_id_to_ngrams(con)
|
||||
return TEMA_ID_TO_NGRAMS_CACHE
|
||||
|
||||
|
||||
def _get_tema_id_to_ngrams(con: Optional[Connection] = None) -> dict[int, model.NGrams]:
|
||||
query = """
|
||||
SELECT id, ngrams
|
||||
FROM temes
|
||||
WHERE hidden = 0
|
||||
"""
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
rows = cur.fetchall()
|
||||
return {id_: cell_to_ngrams(ng) for id_, ng in rows}
|
||||
44
folkugat_web/dal/sql/temes/write.py
Normal file
44
folkugat_web/dal/sql/temes/write.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
from ._conversion import row_to_tema, tema_to_row
|
||||
from .query import evict_tema_id_to_ngrams_cache
|
||||
|
||||
|
||||
def insert_tema(tema: model.Tema, con: Optional[Connection] = None) -> model.Tema:
|
||||
query = """
|
||||
INSERT INTO temes
|
||||
(id, title, properties, links, lyrics, alternatives, ngrams,
|
||||
creation_date, modification_date, hidden)
|
||||
VALUES
|
||||
(:id, :title, :properties, :links, :lyrics, :alternatives, :ngrams,
|
||||
:creation_date, :modification_date, :hidden)
|
||||
RETURNING *
|
||||
"""
|
||||
data = tema_to_row(tema)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
row = cur.fetchone()
|
||||
evict_tema_id_to_ngrams_cache()
|
||||
return row_to_tema(row)
|
||||
|
||||
|
||||
def update_tema(tema: model.Tema, con: Optional[Connection] = None):
|
||||
query = """
|
||||
UPDATE temes
|
||||
SET
|
||||
title = :title, properties = :properties, links = :links, lyrics = :lyrics,
|
||||
alternatives = :alternatives, ngrams = :ngrams, creation_date = :creation_date,
|
||||
modification_date = :modification_date, hidden = :hidden
|
||||
WHERE
|
||||
id = :id
|
||||
"""
|
||||
data = tema_to_row(tema)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
evict_tema_id_to_ngrams_cache()
|
||||
return
|
||||
Reference in New Issue
Block a user