db: Give Tables a get_all() function

Right now I only implement it for Libraries. More tables can be added as
needed.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-08-26 14:17:31 -04:00
parent 0e55bb25e9
commit 7dd3ff8473
4 changed files with 18 additions and 0 deletions

View File

@ -74,6 +74,9 @@ class LibraryTable(objects.Table):
return execute("SELECT libraryid FROM libraries "
"WHERE libraryid=?", [ rowid ])
def do_get_all(self):
return execute("SELECT libraryid FROM libraries")
def do_lookup(self, path):
return execute("SELECT libraryid FROM libraries "
"WHERE path=?", [ str(path) ])

View File

@ -43,6 +43,7 @@ class Table(GObject.GObject):
def do_delete(self, obj): raise NotImplementedError
def do_insert(self, *args): raise NotImplementedError
def do_get(self, rowid): raise NotImplementedError
def do_get_all(self): raise NotImplementedError
def do_lookup(self, *args): raise NotImplementedError
def insert(self, *args):
@ -64,6 +65,10 @@ class Table(GObject.GObject):
row = self.do_get(rowid).fetchone()
return self.table_type(row[0]) if row else None
def get_all(self):
rows = self.do_get_all().fetchall()
return [ self.table_type(row[0]) for row in rows ]
def lookup(self, *args):
row = self.do_lookup(*args).fetchone()
return self.table_type(row[0]) if row else None

View File

@ -43,6 +43,11 @@ class TestLibraryTable(unittest.TestCase):
self.assertEqual(db.library.Table.get(1), library)
self.assertIsNone(db.library.Table.get(2))
def test_library_table_get_all(self):
lib1 = db.library.Table.insert(pathlib.Path("/a/b/c"))
lib2 = db.library.Table.insert(pathlib.Path("/d/e/f"))
self.assertEqual(db.library.Table.get_all(), [ lib1, lib2 ])
def test_library_table_lookup(self):
library = db.library.Table.insert(pathlib.Path("/a/b/c"))
self.assertEqual(db.library.Table.lookup(pathlib.Path("/a/b/c")), library)

View File

@ -65,6 +65,7 @@ class FakeTable(objects.Table):
def do_insert(self, insert, args): return FakeCursor(1)
def do_delete(self, obj): self.delete_called = True
def do_get(self, rowid): return FakeCursor(1)
def do_get_all(self): return FakeCursor(1)
def do_lookup(self, lookup, args):
return FakeCursor(1) if lookup == "lookup" else FakeCursor(None)
@ -84,6 +85,8 @@ class TestTable(unittest.TestCase):
objects.Table.do_insert(None, "insert", "arguments")
with self.assertRaises(NotImplementedError):
objects.Table.do_get(None, 2)
with self.assertRaises(NotImplementedError):
objects.Table.do_get_all(None)
with self.assertRaises(NotImplementedError):
objects.Table.do_lookup(None, "lookup", "arguments")
@ -107,6 +110,8 @@ class TestTable(unittest.TestCase):
self.assertEqual(table.get(1), objects.Row(1))
self.assertEqual(table[1], objects.Row(1))
self.assertEqual(table.get_all(), [ objects.Row(2), objects.Row(3) ])
self.assertEqual(table.lookup("lookup", "arguments"), objects.Row(1))
self.row_inserted = None