diff options
| author | user@node5.net <user@node5.net> | 2025-07-19 12:14:56 +0200 |
|---|---|---|
| committer | user@node5.net <user@node5.net> | 2025-07-19 12:14:56 +0200 |
| commit | 0e692b0306594697557a9917f8e41c70c16cd5f6 (patch) | |
| tree | 7d744525ced5bc2798beccb27adb217fccbe1684 /src/db_handler.py | |
| parent | d82877b9424f2543bcb85b5f472005caf782dad1 (diff) | |
Migrate from Postgres to SQLite
Diffstat (limited to 'src/db_handler.py')
| -rw-r--r-- | src/db_handler.py | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/src/db_handler.py b/src/db_handler.py index cd40112..660e00a 100644 --- a/src/db_handler.py +++ b/src/db_handler.py @@ -1,9 +1,7 @@ -import os import logging from dataclasses import dataclass -import psycopg -import yaml +import sqlite3 logger = logging.getLogger(__name__) # Set the logger name, to the name of the module @@ -13,29 +11,31 @@ logger = logging.getLogger(__name__) # Set the logger name, to the name of the m class DBHandler: config: dict - def conn_factory(self) -> psycopg.Connection: - conn: psycopg.Connection = psycopg.connect(**self.config, row_factory=psycopg.rows.dict_row) - return conn + def con_factory(self) -> sqlite3.Connection: + con = sqlite3.connect("blog.node5.net.db") + return con def get_comments(self, url: str) -> list[dict]: - with self.conn_factory() as conn: - with conn.cursor() as cur: - cur.execute( - "SELECT id, comment, page_url, visitor_url, nickname, " - "(CASE WHEN show_contact THEN contact ELSE NULL END) as contact, created_at " - "FROM comment WHERE approved AND public AND page_url = %(url)s ORDER BY created_at DESC;", {'url': url}) - comments = cur.fetchall() - return comments + with self.con_factory() as con: + cur = con.cursor() + cur.execute( + ''' + SELECT id, comment, page_url, visitor_url, nickname, + (CASE WHEN show_contact THEN contact ELSE NULL END) as contact, created_at + FROM comment WHERE approved AND public AND page_url = ? ORDER BY created_at DESC; + ''', (url)) + comments = cur.fetchall() + return comments def post_comment(self, comment: str, page_url: str, visitor_url: str=None, nickname: str=None, contact: str=None, show_contact: bool=True, public: bool=True): - with self.conn_factory() as conn: - with conn.cursor() as cur: - cur.execute( - "INSERT INTO comment (comment, page_url, visitor_url, nickname, contact, show_contact, public)" - "VALUES (%(comment)s, %(page_url)s, %(visitor_url)s, %(nickname)s, %(contact)s, %(show_contact)s, " - "%(public)s);", - {'comment': comment, 'page_url': page_url, 'visitor_url': visitor_url, 'nickname': nickname, - 'contact': contact, 'show_contact': show_contact, 'public': public} - ) + with self.con_factory() as con: + cur = con.cursor() + cur.execute( + ''' + INSERT INTO comment (comment, page_url, visitor_url, nickname, contact, show_contact, public)" + VALUES (?, ?, ?, ?, ?, ?, ?); + ''', + (comment, page_url, visitor_url, nickname, contact, show_contact, public) + ) |
