]> git.sthu.org Git - dvrdb.git/commitdiff
dvr-managedb: -q takes a where-stmt rather than id
authorStefan Huber <shuber@sthu.org>
Fri, 17 Jan 2014 13:27:35 +0000 (14:27 +0100)
committerStefan Huber <shuber@sthu.org>
Fri, 17 Jan 2014 13:30:19 +0000 (14:30 +0100)
dvr-managedb

index 4cb5588cd394a4fb0082cffe05ead564c5489052..a4dba387fd6ebe6760587ec5df85e6c1860a4a39 100755 (executable)
@@ -70,6 +70,17 @@ class Database:
         c.execute("SELECT dvr FROM dvrtable WHERE id=?", (id,))
         return c.fetchone()[0]
 
+    def query(self, wherestmt):
+        """Query database with given where statement. Return a list od IDs."""
+        stmt = "SELECT DISTINCT dvrtable.id FROM dvrtable, registrations, purposes"
+        stmt += " WHERE dvrtable.id=registrations.dvrid AND dvrtable.id=purposes.dvrid"
+        stmt += " AND " + wherestmt
+
+        c = self.conn.cursor()
+        c.execute(stmt)
+        rows = c.fetchall()
+        return [r[0] for r in rows]
+
     def add_dvr(self, id, dvr=None):
         """"Add a DVR with given ID to the dataset"""
         c = self.conn.cursor()
@@ -122,8 +133,7 @@ OPTIONS:
               a directory, read all files ending with ".html" or ".html.gz".
   -d FILE     Use given sqlite3 database.
   -h          Print this help text.
-  -q ID       Query dataset for dataset of given ID. Exit status is 1 in case
-              that no such dataset exists and 0 on success.
+  -q COND     Query for datasets where the given WHERE SQL-condition holds.
   -g          Query geographical location for address field of registration.
 """)
 
@@ -146,11 +156,12 @@ def queryLocation(address):
         print(e, file=sys.stderr)
         return None
 
-def processQuery(db, id, showLocation=False):
-    """Process query for given ID."""
+
+def printDataset(db, id):
+    """Print info for a dataset of given ID."""
 
     if not db.contains_id(id):
-        print("No such ID in the database.")
+        print("No ID %d in the database." % id)
         return False
     print("ID %d found in the database." % id)
 
@@ -186,6 +197,16 @@ def processQuery(db, id, showLocation=False):
     return True
 
 
+def processQuery(db, query, showLocation=False):
+    """Process query for given ID."""
+
+    ids = db.query(query)
+    print("Found %d results." % len(ids))
+
+    for id in ids:
+        printDataset(db, id)
+
+
 def processAdd(db, source):
     """Read data from source, parse data, and add to database."""
 
@@ -290,7 +311,7 @@ if __name__ == "__main__":
             elif opt == "-g":
                 showLocation = True
             elif opt == "-q":
-                query = int(arg)
+                query = arg
             else:
                 print("Unknown option '", opt, "'.", file=sys.stderr)
                 assert(False)