From bb513a23f4486e0529509ef7ac3d15bbb255daa5 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Thu, 6 Sep 2007 10:50:34 +0000 Subject: Checkpoint svn path=/scripts/rpki/sql.py; revision=930 --- scripts/rpki/sql.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'scripts/rpki/sql.py') diff --git a/scripts/rpki/sql.py b/scripts/rpki/sql.py index f0e2aa6d..9762f9ea 100644 --- a/scripts/rpki/sql.py +++ b/scripts/rpki/sql.py @@ -22,6 +22,16 @@ class sql_persistant(object): # objects are stored. sql_children = {} + ## @var sql_in_db + # Whether this object is already in SQL or not. Perhaps this should + # instead be a None value in the object's ID field? + sql_in_db = False + + ## @var sql_dirty + # Whether this object has been modified and needs to be written back + # to SQL. + sql_dirty = False + @classmethod def sql_fetch(cls, db, **kwargs): """Fetch rows from SQL based on a canned query and a set of @@ -34,26 +44,36 @@ class sql_persistant(object): """ cur = db.cursor() - cur.execute(self.sql_fetch_cmd % kwargs) + cur.execute(self.sql_select_cmd % kwargs) rows = cur.fetchall() cur.close() objs = [] for row in rows: obj = cls() - obj.sql_objectify(row) + obj.in_sql = True + obj.sql_objectify(*row) objs.append(obj) if isinstance(obj, sql_persistant): for kid in obj.sql_children: setattr(obj, obj.sql_children[kid], kid.sql_fetch(db)) return objs - def sql_objectify(self, row): + def sql_objectify(self): """Initialize self with values returned by self.sql_fetch(). - - This method is also responsible for performing the - fetch/objectify() cycle on any of its children in the tree of - classes representing SQL tables in this database. But I'm trying - to move that responsibility to self.sql_fetch().... """ - raise NotImplementedError + + def sql_store(self, db, cur=None): + """Save an object and its descendents to SQL. + """ + if cur is None: + cur = db.cursor() + if not self.sql_in_db: + cur.execute(self.sql_insert_cmd % self.sql_makedict()) + elif self.sql_dirty: + cur.execute(self.sql_update_cmd % self.sql_makedict()) + self.sql_dirty = False + self.sql_in_db = True + for kids in self.sql_children.values(): + for kid in getattr(self, kids): + kid.sql_store(db, cur) -- cgit v1.2.3