From 4c13852fedffd41bdf366061d53c8cc9309d80be Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 17 Jan 2014 14:27:35 +0100 Subject: [PATCH] dvr-managedb: -q takes a where-stmt rather than id --- dvr-managedb | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/dvr-managedb b/dvr-managedb index 4cb5588..a4dba38 100755 --- a/dvr-managedb +++ b/dvr-managedb @@ -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) -- 2.39.5