Hopefully fixed lockup bug.
This commit is contained in:
243
database.py
243
database.py
@@ -208,10 +208,14 @@ class SettingsDatabase:
|
||||
|
||||
def get(self, section, option, **kwargs):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
result = cursor.execute("SELECT value FROM botamusique WHERE section=? AND option=?",
|
||||
(section, option)).fetchall()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
result = cursor.execute("SELECT value FROM botamusique WHERE section=? AND option=?",
|
||||
(section, option)).fetchall()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
if len(result) > 0:
|
||||
return result[0][0]
|
||||
@@ -232,18 +236,26 @@ class SettingsDatabase:
|
||||
|
||||
def set(self, section, option, value):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT OR REPLACE INTO botamusique (section, option, value) "
|
||||
"VALUES (?, ?, ?)", (section, option, value))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT OR REPLACE INTO botamusique (section, option, value) "
|
||||
"VALUES (?, ?, ?)", (section, option, value))
|
||||
conn.commit()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def has_option(self, section, option):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
result = cursor.execute("SELECT value FROM botamusique WHERE section=? AND option=?",
|
||||
(section, option)).fetchall()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
result = cursor.execute("SELECT value FROM botamusique WHERE section=? AND option=?",
|
||||
(section, option)).fetchall()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
if len(result) > 0:
|
||||
return True
|
||||
else:
|
||||
@@ -251,23 +263,35 @@ class SettingsDatabase:
|
||||
|
||||
def remove_option(self, section, option):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM botamusique WHERE section=? AND option=?", (section, option))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM botamusique WHERE section=? AND option=?", (section, option))
|
||||
conn.commit()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def remove_section(self, section):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM botamusique WHERE section=?", (section,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM botamusique WHERE section=?", (section,))
|
||||
conn.commit()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def items(self, section):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT option, value FROM botamusique WHERE section=?", (section,)).fetchall()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT option, value FROM botamusique WHERE section=?", (section,)).fetchall()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
if len(results) > 0:
|
||||
return list(map(lambda v: (v[0], v[1]), results))
|
||||
@@ -276,9 +300,13 @@ class SettingsDatabase:
|
||||
|
||||
def drop_table(self):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DROP TABLE botamusique")
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DROP TABLE botamusique")
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
class MusicDatabase:
|
||||
@@ -287,66 +315,79 @@ class MusicDatabase:
|
||||
|
||||
def insert_music(self, music_dict, _conn=None):
|
||||
conn = sqlite3.connect(self.db_path) if _conn is None else _conn
|
||||
cursor = conn.cursor()
|
||||
cursor = None
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
|
||||
id = music_dict['id']
|
||||
title = music_dict['title']
|
||||
type = music_dict['type']
|
||||
path = music_dict['path'] if 'path' in music_dict else ''
|
||||
keywords = music_dict['keywords']
|
||||
id = music_dict['id']
|
||||
title = music_dict['title']
|
||||
type = music_dict['type']
|
||||
path = music_dict['path'] if 'path' in music_dict else ''
|
||||
keywords = music_dict['keywords']
|
||||
|
||||
tags_list = list(dict.fromkeys(music_dict['tags']))
|
||||
tags = ''
|
||||
if tags_list:
|
||||
tags = ",".join(tags_list) + ","
|
||||
tags_list = list(dict.fromkeys(music_dict['tags']))
|
||||
tags = ''
|
||||
if tags_list:
|
||||
tags = ",".join(tags_list) + ","
|
||||
|
||||
del music_dict['id']
|
||||
del music_dict['title']
|
||||
del music_dict['type']
|
||||
del music_dict['tags']
|
||||
if 'path' in music_dict:
|
||||
del music_dict['path']
|
||||
del music_dict['keywords']
|
||||
del music_dict['id']
|
||||
del music_dict['title']
|
||||
del music_dict['type']
|
||||
del music_dict['tags']
|
||||
if 'path' in music_dict:
|
||||
del music_dict['path']
|
||||
del music_dict['keywords']
|
||||
|
||||
existed = cursor.execute("SELECT 1 FROM music WHERE id=?", (id,)).fetchall()
|
||||
if len(existed) == 0:
|
||||
cursor.execute(
|
||||
"INSERT INTO music (id, type, title, metadata, tags, path, keywords) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(id,
|
||||
type,
|
||||
title,
|
||||
json.dumps(music_dict),
|
||||
tags,
|
||||
path,
|
||||
keywords))
|
||||
else:
|
||||
cursor.execute("UPDATE music SET type=:type, title=:title, metadata=:metadata, tags=:tags, "
|
||||
"path=:path, keywords=:keywords WHERE id=:id",
|
||||
{'id': id,
|
||||
'type': type,
|
||||
'title': title,
|
||||
'metadata': json.dumps(music_dict),
|
||||
'tags': tags,
|
||||
'path': path,
|
||||
'keywords': keywords})
|
||||
existed = cursor.execute("SELECT 1 FROM music WHERE id=?", (id,)).fetchall()
|
||||
if len(existed) == 0:
|
||||
cursor.execute(
|
||||
"INSERT INTO music (id, type, title, metadata, tags, path, keywords) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(id,
|
||||
type,
|
||||
title,
|
||||
json.dumps(music_dict),
|
||||
tags,
|
||||
path,
|
||||
keywords))
|
||||
else:
|
||||
cursor.execute("UPDATE music SET type=:type, title=:title, metadata=:metadata, tags=:tags, "
|
||||
"path=:path, keywords=:keywords WHERE id=:id",
|
||||
{'id': id,
|
||||
'type': type,
|
||||
'title': title,
|
||||
'metadata': json.dumps(music_dict),
|
||||
'tags': tags,
|
||||
'path': path,
|
||||
'keywords': keywords})
|
||||
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
if not _conn:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def query_music_ids(self, condition: Condition):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT id FROM music WHERE id != 'info' AND %s" %
|
||||
condition.sql(conn), condition.filler).fetchall()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT id FROM music WHERE id != 'info' AND %s" %
|
||||
condition.sql(conn), condition.filler).fetchall()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return list(map(lambda i: i[0], results))
|
||||
|
||||
def query_all_paths(self):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT path FROM music WHERE id != 'info' AND type = 'file'").fetchall()
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT path FROM music WHERE id != 'info' AND type = 'file'").fetchall()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
paths = []
|
||||
for result in results:
|
||||
if result and result[0]:
|
||||
@@ -356,25 +397,33 @@ class MusicDatabase:
|
||||
|
||||
def query_all_tags(self):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT tags FROM music WHERE id != 'info'").fetchall()
|
||||
tags = []
|
||||
for result in results:
|
||||
for tag in result[0].strip(",").split(","):
|
||||
if tag and tag not in tags:
|
||||
tags.append(tag)
|
||||
conn.close()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT tags FROM music WHERE id != 'info'").fetchall()
|
||||
tags = []
|
||||
for result in results:
|
||||
for tag in result[0].strip(",").split(","):
|
||||
if tag and tag not in tags:
|
||||
tags.append(tag)
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return tags
|
||||
|
||||
def query_music_count(self, condition: Condition):
|
||||
filler = condition.filler
|
||||
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
condition_str = condition.sql(conn)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT COUNT(*) FROM music "
|
||||
"WHERE id != 'info' AND %s" % condition_str, filler).fetchall()
|
||||
conn.close()
|
||||
try:
|
||||
condition_str = condition.sql(conn)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT COUNT(*) FROM music "
|
||||
"WHERE id != 'info' AND %s" % condition_str, filler).fetchall()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
return results[0][0]
|
||||
|
||||
@@ -382,10 +431,15 @@ class MusicDatabase:
|
||||
filler = condition.filler
|
||||
|
||||
conn = sqlite3.connect(self.db_path) if _conn is None else _conn
|
||||
condition_str = condition.sql(conn)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT id, type, title, metadata, tags, path, keywords FROM music "
|
||||
"WHERE id != 'info' AND %s" % condition_str, filler).fetchall()
|
||||
cursor = None
|
||||
try:
|
||||
condition_str = condition.sql(conn)
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT id, type, title, metadata, tags, path, keywords FROM music "
|
||||
"WHERE id != 'info' AND %s" % condition_str, filler).fetchall()
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
if not _conn:
|
||||
conn.close()
|
||||
|
||||
@@ -393,9 +447,14 @@ class MusicDatabase:
|
||||
|
||||
def _query_music_by_plain_sql_cond(self, sql_cond, _conn=None):
|
||||
conn = sqlite3.connect(self.db_path) if _conn is None else _conn
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT id, type, title, metadata, tags, path, keywords FROM music "
|
||||
"WHERE id != 'info' AND %s" % sql_cond).fetchall()
|
||||
cursor = None
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
results = cursor.execute("SELECT id, type, title, metadata, tags, path, keywords FROM music "
|
||||
"WHERE id != 'info' AND %s" % sql_cond).fetchall()
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
if not _conn:
|
||||
conn.close()
|
||||
|
||||
|
Reference in New Issue
Block a user