- provide base class __init__() and delete() implementations
authorMark Huang <mlhuang@cs.princeton.edu>
Fri, 20 Oct 2006 17:43:12 +0000 (17:43 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Fri, 20 Oct 2006 17:43:12 +0000 (17:43 +0000)
PLC/Table.py

index e76707d..4174f10 100644 (file)
@@ -16,10 +16,18 @@ class Row(dict):
     # sync() is called.
     primary_key = None
 
+    # Set this to the names of tables that reference this table's
+    # primary key.
+    join_tables = []
+
     # Set this to a dict of the valid fields of this object. Not all
     # fields (e.g., joined fields) may be updated via sync().
     fields = {}
 
+    def __init__(self, api, fields = {}):
+        dict.__init__(self, fields)
+        self.api = api
+
     def validate(self):
         """
         Validates values. Will validate a value with a custom function
@@ -87,15 +95,22 @@ class Row(dict):
 
     def delete(self, commit = True):
         """
-        Delete row from its primary table.
+        Delete row from its primary table, and from any tables that
+        reference it.
         """
 
         assert self.primary_key in self
 
-        sql = "DELETE FROM %s" % self.table_name + \
-              " WHERE %s = %s" % \
-              (self.primary_key,
-               self.api.db.param(self.primary_key, self[self.primary_key]))
+        for table in self.join_tables + [self.table_name]:
+            if isinstance(table, tuple):
+                key = table[1]
+                table = table[0]
+            else:
+                key = self.primary_key
+
+            sql = "DELETE FROM %s WHERE %s = %s" % \
+                  (table, key,
+                   self.api.db.param(self.primary_key, self[self.primary_key]))
 
         self.api.db.do(sql, self)