]> git.sthu.org Git - dvrdb.git/commitdiff
Fix: (dvrid, num) are not unique for purposes
authorStefan Huber <shuber@sthu.org>
Wed, 15 Jan 2014 13:31:05 +0000 (14:31 +0100)
committerStefan Huber <shuber@sthu.org>
Fri, 17 Jan 2014 13:30:19 +0000 (14:30 +0100)
dvr-db.py

index c46aec1d38432aac183db937a5c5917238c14bd8..dbf7791c88c76c3f1b4a6c1119149b192478648e 100755 (executable)
--- a/dvr-db.py
+++ b/dvr-db.py
@@ -38,20 +38,23 @@ class Database:
                      )")
 
         c.execute("CREATE TABLE IF NOT EXISTS registrations (\
-                     id INTEGER PRIMARY KEY \
+                     dvrid INTEGER PRIMARY KEY \
                         REFERENCES dvrtable(id) ON DELETE CASCADE, \
                      name TEXT NOT NULL, \
                      address TEXT NOT NULL \
                      )")
 
         c.execute("CREATE TABLE IF NOT EXISTS purposes (\
-                     id INTEGER REFERENCES dvrtable(id) ON DELETE CASCADE, \
-                     num INTEGER, \
+                     id INTEGER PRIMARY KEY AUTOINCREMENT, \
+                     dvrid INTEGER REFERENCES dvrtable(id) ON DELETE CASCADE, \
+                     num INTEGER NOT NULL, \
                      purpose TEXT NOT NULL, \
                      date TEXT NOT NULL, \
-                     status TEXT NOT NULL, \
-                     PRIMARY KEY(id, num) \
+                     status TEXT NOT NULL \
                      )")
+
+        c.execute("CREATE INDEX IF NOT EXISTS purposes_dvrid ON purposes (dvrid)")
+
         c.close()
         conn.commit()
 
@@ -74,31 +77,32 @@ class Database:
         c.close()
         self.conn.commit()
 
-    def get_registration(self, id):
-        """Return the registration of given ID, if any."""
+    def get_registration(self, dvrid):
+        """Return the registration of given DVR-ID, if any."""
         c = self.conn.cursor()
-        c.execute("SELECT * FROM registrations WHERE id=?", (id,))
+        c.execute("SELECT * FROM registrations WHERE dvrid=?", (dvrid,))
         return c.fetchone()
 
-    def add_registration(self, id, name, address):
-        """Add a registration for the given ID."""
+    def add_registration(self, dvrid, name, address):
+        """Add a registration for the given DVR-ID."""
         c = self.conn.cursor()
         c.execute("INSERT INTO registrations VALUES (?, ?, ?)",
-                  (id, name, address))
+                  (dvrid, name, address))
         c.close()
         self.conn.commit()
 
-    def get_purposes(self, id):
-        """Return all known purposes of given ID, if any."""
+    def get_purposes(self, dvrid):
+        """Return all known purposes of given DVR-ID, if any."""
         c = self.conn.cursor()
-        c.execute("SELECT * FROM purposes WHERE id=?", (id,))
+        c.execute("SELECT * FROM purposes WHERE dvrid=?", (dvrid,))
         return c.fetchall()
 
-    def add_purpose(self, id, num, purpose, date, status):
-        """Add a purpose for a given ID."""
+    def add_purpose(self, dvrid, num, purpose, date, status):
+        """Add a purpose for a given DVR-ID."""
         c = self.conn.cursor()
-        c.execute("INSERT INTO purposes VALUES (?, ?, ?, ?, ?)",
-                  (id, num, purpose, date, status))
+        c.execute("INSERT INTO purposes (dvrid, num, purpose, date, status) \
+                  VALUES (?, ?, ?, ?, ?)",
+                  (dvrid, num, purpose, date, status))
         c.close()
         self.conn.commit()
 
@@ -175,10 +179,10 @@ def processQuery(db, id, showLocation=False):
             print("  No purposes known.")
         else:
             for purp in purposes:
-                print("  Purpose %d:" % purp[1])
-                print("    Text:  ", purp[2])
-                print("    Date:  ", purp[3])
-                print("    Status:", purp[4])
+                print("  Purpose %d:" % purp[2])
+                print("    Text:  ", purp[3])
+                print("    Date:  ", purp[4])
+                print("    Status:", purp[5])
     return True