2020-01-19 01:56:35 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2020 - David Todd (c0de@c0defox.es)
|
|
|
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
2020-01-19 02:01:36 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
This file contains the database for a simple bookmarking application
|
|
|
|
"""
|
2020-01-19 01:56:35 +00:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import sqlite3
|
|
|
|
import uuid
|
|
|
|
class Database:
|
|
|
|
"""
|
2020-01-19 06:20:40 +00:00
|
|
|
Base database class that is shared by the bookmark
|
|
|
|
engine and the Telethon authenticator
|
2020-01-19 01:56:35 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, database):
|
|
|
|
"""
|
|
|
|
Sets up the database connection + cursor
|
|
|
|
as well as creates a new table if it doesn't
|
|
|
|
exist in the database that is provided via the
|
|
|
|
instantiation of this class
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Convert UUIDs to bytestring representation when INSERTing
|
|
|
|
sqlite3.register_adapter(uuid.UUID, lambda u: u.bytes_le)
|
|
|
|
# Convert UUID bytestring back to UUID object when SELECTing
|
|
|
|
sqlite3.register_converter('GUID', lambda b: uuid.UUID(bytes_le=b))
|
|
|
|
|
|
|
|
# Used to convert datetime objects (and others in the future)
|
|
|
|
detect_types = sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
|
|
|
|
|
|
|
|
# Automatically connect to the database when instanced
|
|
|
|
self.connection = sqlite3.connect(database, detect_types=detect_types)
|
|
|
|
with self.connection:
|
|
|
|
self.cursor = self.connection.cursor()
|