webapp.py: fuse two sql queries in get_details master
authorHelmut Grohne <helmut@subdivi.de>
Wed, 12 Jan 2022 05:01:18 +0000 (06:01 +0100)
committerHelmut Grohne <helmut@subdivi.de>
Wed, 12 Jan 2022 05:01:18 +0000 (06:01 +0100)
webapp.py

index da66ed7..6ba2846 100755 (executable)
--- a/webapp.py
+++ b/webapp.py
@@ -105,23 +105,19 @@ class Application:
 
     def get_details(self, package):
         with self.cursor() as cur:
-            cur.execute("SELECT id, version, architecture FROM package WHERE name = ?;",
+            cur.execute("SELECT p.id, version, architecture, count(filename), ifnull(sum(size), 0) FROM package AS p LEFT JOIN content ON p.id = content.pid WHERE name = ? GROUP BY p.id;",
                         (package,))
             row = cur.fetchone()
-            if not row:
-                raise NotFound()
-            pid, version, architecture = row
-            details = dict(pid=pid,
-                           package=package,
-                           version=version,
-                           architecture=architecture)
-            cur.execute("SELECT count(filename), sum(size) FROM content WHERE pid = ?;",
-                        (pid,))
-            num_files, total_size = cur.fetchone()
-        if total_size is None:
-            total_size = 0
-        details.update(dict(num_files=num_files, total_size=total_size))
-        return details
+        if not row:
+            raise NotFound()
+        return dict(
+            package=package,
+            pid=row[0],
+            version=row[1],
+            architecture=row[2],
+            num_files=row[3],
+            total_size=row[4],
+        )
 
     def get_dependencies(self, pid):
         with self.cursor() as cur: