summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichaël Ball <michael.ball@gmail.com>2015-08-03 06:52:00 +0100
committerMichaël Ball <michael.ball@gmail.com>2015-08-03 06:52:00 +0100
commit122ec1c9b283776b8f1fea5559d4317bdba5b977 (patch)
tree874f4a1e2644f165f08295e56e150be9e3cafe97
parent35a21c4d12ae85aed699d4117ebbdb4c510ad40f (diff)
Initially use memory based db, then dump on exit
-rw-r--r--db/db_manager.py19
-rw-r--r--models/artist.py22
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