aboutsummaryrefslogtreecommitdiff
path: root/scripts/rpki/sql.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2007-09-06 10:50:34 +0000
committerRob Austein <sra@hactrn.net>2007-09-06 10:50:34 +0000
commitbb513a23f4486e0529509ef7ac3d15bbb255daa5 (patch)
treeb1aeb7dc7d66bfaa2c3b72f27d1969ba0bcb8add /scripts/rpki/sql.py
parent802423c4fbf0c4f993f079551ca016981e7148de (diff)
Checkpoint
svn path=/scripts/rpki/sql.py; revision=930
Diffstat (limited to 'scripts/rpki/sql.py')
-rw-r--r--scripts/rpki/sql.py38
1 files changed, 29 insertions, 9 deletions
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)