2018-03-16 03:13:43 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
The bot's database connection class.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sqlite3
|
2018-03-25 14:26:48 -04:00
|
|
|
import threading
|
2018-03-16 03:13:43 -04:00
|
|
|
|
|
|
|
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
|
2018-03-25 14:26:48 -04:00
|
|
|
self.db_lock = threading.Lock()
|
2018-03-16 03:13:43 -04:00
|
|
|
|
|
|
|
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:
|
2018-03-25 14:26:48 -04:00
|
|
|
self.db_lock.acquire()
|
2018-03-16 03:13:43 -04:00
|
|
|
cur = conn.cursor()
|
2018-03-25 14:26:48 -04:00
|
|
|
res = cur.execute(*args, **kwargs)
|
|
|
|
self.db_lock.release()
|
|
|
|
return res
|