fulvia/db.py

36 lines
823 B
Python
Executable File

#!/usr/bin/env python3
"""
The bot's database connection class.
"""
import os
import sqlite3
import threading
class FulviaDB(object):
"""
Defines a basic interface and some convenience functionsfor the bot's
database.
"""
def __init__(self, config):
path = config.core.db_filename
self.filename = path
self.db_lock = threading.Lock()
def connect(self):
"""Return a raw database connection object."""
return sqlite3.connect(self.filename, timeout=10)
def execute(self, *args, **kwargs):
"""
Execute an arbitrary SQL query against the database.
Returns a cursor object, on which things like `.fetchall()` can be
called per PEP 249.
"""
with self.connect() as conn:
self.db_lock.acquire()
cur = conn.cursor()
res = cur.execute(*args, **kwargs)
self.db_lock.release()
return res