Commit 575a5354 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

trace: use temporary connections to the database

When used within multiple threads, the previous code would raise:

ProgrammingError: SQLite objects created in a thread can only be used in
that same thread. The object was created in thread id XXX and this is
thread id YYY.

Let's find a better fix.
parent 132d18a2
......@@ -4,20 +4,22 @@ from lib2to3.pgen2 import tokenize
from lib2to3.refactor import get_fixers_from_package, RefactoringTool
database = "traces.db"
conn = sqlite3.connect(database)
tracing_functions = []
def create_table(table, *columns):
conn = sqlite3.connect(database)
with conn:
v = ', '.join(columns)
conn.execute(
"CREATE TABLE IF NOT EXISTS %s (%s, UNIQUE (%s))" % (table, v, v)
)
conn.close()
def insert_unique(*values):
conn = sqlite3.connect(database)
with conn:
try:
conn.execute(
......@@ -27,6 +29,7 @@ def create_table(table, *columns):
except sqlite3.IntegrityError as e:
if not str(e).startswith('UNIQUE constraint failed:'):
raise
conn.close()
return insert_unique
......@@ -43,8 +46,10 @@ def get_data(table, columns_to_select='*', conditions={}):
)
if conditions:
query += " WHERE " + ' AND '.join(k + " = :" + k for k in conditions.keys())
conn = sqlite3.connect(database)
with conn:
return conn.execute(query, conditions).fetchall()
conn.close()
def apply_fixers(string, name):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment