Initial commit
This commit is contained in:
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