diff options
-rw-r--r-- | db/db_manager.py | 19 | ||||
-rw-r--r-- | models/artist.py | 22 |
2 files changed, 36 insertions, 5 deletions
diff --git a/db/db_manager.py b/db/db_manager.py index eb813ec..ca47ae4 100644 --- a/db/db_manager.py +++ b/db/db_manager.py @@ -11,25 +11,34 @@ class DbManager: def __init__(self): new_db = False + cache_size_kb = 9766 if not os.path.isfile(self.config["DEFAULT"]["database"]): new_db = True - self.conn = apsw.Connection(self.config["DEFAULT"]["database"], - apsw.SQLITE_OPEN_READWRITE | - apsw.SQLITE_OPEN_CREATE) - if new_db: + self.conn = apsw.Connection(":memory:") self.create_tables() else: + self.conn = apsw.Connection(self.config["DEFAULT"]["database"]) library_info = os.stat(self.config["DEFAULT"]["database"]) cache_size_kb = round((library_info.st_size * 1.2) / 1024) cursor = self.conn.cursor() # Setting pragma with ? placeholder errors out - cursor.execute("pragma cache_size=%s" % cache_size_kb) + cursor.execute("pragma cache_size=-%s" % cache_size_kb) cursor.close() + def __del__(self): + if not os.path.isfile(self.config["DEFAULT"]["database"]): + tempconn = apsw.Connection(self.config["DEFAULT"]["database"], + apsw.SQLITE_OPEN_READWRITE | + apsw.SQLITE_OPEN_CREATE) + + with tempconn.backup("main", + self.conn, "main") as backup: + backup.step() + def __str__(self): return repr(self) diff --git a/models/artist.py b/models/artist.py index 1ac27d5..1afd516 100644 --- a/models/artist.py +++ b/models/artist.py @@ -131,3 +131,25 @@ class Artist: ) return artists + + def all(order="sortname", direction="ASC", limit=None, offset=None): + db = DbManager() + cursor = db.cursor() + artists = [] + + select_string = """SELECT * FROM artist ORDER BY %s %s""" % (order, + direction) + + if limit is not None and offset is not None: + select_string = " ".join((select_string, + "LIMIT %s OFFSET %s" % (limit, offset))) + + result = cursor.execute(select_string) + + for row in result: + artists.append( + Artist(id=row[0], name=row[1], sortname=row[2], + musicbrainz_artistid=row[3]) + ) + + return artist |