Small database command optimisations

This commit is contained in:
CasVT
2023-05-22 21:56:43 +02:00
parent 054c7f798a
commit c60da4c0bc
7 changed files with 73 additions and 42 deletions

View File

@@ -8,12 +8,13 @@ from backend.custom_exceptions import (InvalidURL, NotificationServiceInUse,
NotificationServiceNotFound)
from backend.db import get_db
class NotificationService:
def __init__(self, notification_service_id: int) -> None:
self.id = notification_service_id
if not get_db().execute(
"SELECT 1 FROM notification_services WHERE id = ? LIMIT 1",
"SELECT 1 FROM notification_services WHERE id = ? LIMIT 1;",
(self.id,)
).fetchone():
raise NotificationServiceNotFound
@@ -61,13 +62,15 @@ class NotificationService:
# Update database
get_db().execute("""
UPDATE notification_services
SET title=?, url=?
SET title = ?, url = ?
WHERE id = ?;
""", (
""",
(
data["title"],
data["url"],
self.id
))
)
)
return self.get()
@@ -103,17 +106,22 @@ class NotificationService:
class NotificationServices:
def __init__(self, user_id: int) -> None:
self.user_id = user_id
def fetchall(self) -> List[dict]:
"""Get a list of all notification services
Returns:
List[dict]: The list of all notification services
"""
result = list(map(dict, get_db(dict).execute(
"SELECT id, title, url FROM notification_services WHERE user_id = ? ORDER BY title, id",
result = list(map(dict, get_db(dict).execute("""
SELECT
id, title, url
FROM notification_services
WHERE user_id = ?
ORDER BY title, id;
""",
(self.user_id,)
).fetchall()))
)))
return result