summaryrefslogtreecommitdiff
path: root/database.py
blob: aebd72eeb0828e56ce0c8086320954f59630ee8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from dataclasses import dataclass

import sqlite3
import datetime

@dataclass
class Database:
    def __enter__(self):
        self.__connection = sqlite3.connect("website.db")
        return self

    def __exit__(self, type, value, traceback):
        self.__connection.commit()
        self.__connection.close()

    def get_header_links(self):
        cursor = self.__connection.cursor()
        cursor.execute("SELECT name, link FROM headerLinks;")
        return cursor.fetchall()

    def get_image(self, imageName):
        cursor = self.__connection.cursor()
        cursor.execute("SELECT alt, url FROM images WHERE imageName = %s;",
                       (imageName, ))
        return cursor.fetchone()

    def get_pfp_images(self):
        cursor = self.__connection.cursor()
        cursor.execute("SELECT alt, url FROM images WHERE pfp_img = 1;")
        return cursor.fetchall()

    def get_sidebar_images(self):
        cursor = self.__connection.cursor()
        cursor.execute("SELECT alt, url FROM images WHERE sidebar_img = 1;")
        return cursor.fetchall()

    def get_all_thoughts(self):
        cursor = self.__connection.cursor()
        cursor.execute("SELECT id, title, datetime FROM thoughts")
        return cursor.fetchall()

    def get_thought(self, id_):
        cursor = self.__connection.cursor()
        cursor.execute("""
        SELECT title, datetime, markdown_text FROM thoughts
        WHERE id = ?;""", (id_, ))
        return cursor.fetchall()

    def add_thought(self, title, markdown):
        cursor = self.__connection.cursor()
        cursor.execute("""
        INSERT INTO thoughts (title, datetime, markdown_text)
        VALUES (?, datetime('now'), ?);""", (title, markdown))