This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
2011-11-20 10:23:31 +01:00
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import thread
|
|
|
|
|
2012-02-29 06:09:40 +01:00
|
|
|
threaddbs = {}
|
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
def get_db_connection(conn, name=''):
|
2013-09-04 12:30:04 +02:00
|
|
|
"""returns an sqlite3 connection to a persistent database"""
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
if not name:
|
2012-10-12 09:45:18 +02:00
|
|
|
name = '{}.db'.format(conn.name)
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
threadid = thread.get_ident()
|
|
|
|
if name in threaddbs and threadid in threaddbs[name]:
|
|
|
|
return threaddbs[name][threadid]
|
|
|
|
filename = os.path.join(bot.persist_dir, name)
|
|
|
|
|
|
|
|
db = sqlite3.connect(filename, timeout=10)
|
|
|
|
if name in threaddbs:
|
|
|
|
threaddbs[name][threadid] = db
|
|
|
|
else:
|
|
|
|
threaddbs[name] = {threadid: db}
|
|
|
|
return db
|
|
|
|
|
|
|
|
bot.get_db_connection = get_db_connection
|