From: Mark Huang Date: Fri, 20 Oct 2006 17:43:12 +0000 (+0000) Subject: - provide base class __init__() and delete() implementations X-Git-Tag: pycurl-7_13_1~517 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=43e76cdc320ec4dc83fd29c59ee273b3a8328afb;p=plcapi.git - provide base class __init__() and delete() implementations --- diff --git a/PLC/Table.py b/PLC/Table.py index e76707d6..4174f103 100644 --- a/PLC/Table.py +++ b/PLC/Table.py @@ -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)