2021-10-09 11:44:00 +02:00
|
|
|
import sqlite3
|
2022-12-20 19:25:31 +02:00
|
|
|
import os
|
2021-10-09 11:44:00 +02:00
|
|
|
|
|
|
|
from expiringdict import ExpiringDict
|
|
|
|
|
2022-12-20 19:25:31 +02:00
|
|
|
DEFAULT_DB_PATH = "db/suggestions.db"
|
2021-10-09 11:44:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Database:
|
|
|
|
def __init__(self, db_path=DEFAULT_DB_PATH, max_cache_len=1000, max_cache_age=30):
|
2022-12-20 19:25:31 +02:00
|
|
|
# Legacy check - this can be removed at some point in the near future
|
|
|
|
if os.path.isfile("suggestions.db") and not os.path.isfile("db/suggestions.db"):
|
|
|
|
print("Migrating %s to %s" % ("suggestions.db", "db/suggestions.db"))
|
|
|
|
try:
|
|
|
|
os.rename("suggestions.db", "db/suggestions.db")
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
|
|
|
|
2021-10-09 11:44:00 +02:00
|
|
|
self.db_path = db_path
|
|
|
|
self.cache = ExpiringDict(max_len=max_cache_len, max_age_seconds=max_cache_age)
|
|
|
|
|
|
|
|
# Make sure to do data synchronization on writes!
|
|
|
|
self.c = sqlite3.connect(db_path, check_same_thread=False)
|
|
|
|
self.c.execute(
|
|
|
|
"""CREATE TABLE IF NOT EXISTS suggestions (
|
|
|
|
"q" TEXT NOT NULL,
|
|
|
|
"s" TEXT NOT NULL,
|
|
|
|
"source" TEXT NOT NULL,
|
|
|
|
"target" TEXT NOT NULL
|
|
|
|
);"""
|
|
|
|
)
|
|
|
|
|
|
|
|
def add(self, q, s, source, target):
|
|
|
|
self.c.execute(
|
|
|
|
"INSERT INTO suggestions (q, s, source, target) VALUES (?, ?, ?, ?)",
|
|
|
|
(q, s, source, target),
|
|
|
|
)
|
|
|
|
self.c.commit()
|
|
|
|
return True
|