aboutsummaryrefslogtreecommitdiff
path: root/scripts/rpki/sql.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2007-09-11 17:57:06 +0000
committerRob Austein <sra@hactrn.net>2007-09-11 17:57:06 +0000
commit39f8909f26b1e58329796077f09bcdac0dc39cdf (patch)
tree353cbd4a124e854f6fa814b3f69e6f415a16c388 /scripts/rpki/sql.py
parent29e465d18be72562c2b031ffee753dd52c5d4b4a (diff)
Checkpoint
svn path=/scripts/rpki/left_right.py; revision=941
Diffstat (limited to 'scripts/rpki/sql.py')
-rw-r--r--scripts/rpki/sql.py63
1 files changed, 37 insertions, 26 deletions
diff --git a/scripts/rpki/sql.py b/scripts/rpki/sql.py
index f7214a47..ed8bfe58 100644
--- a/scripts/rpki/sql.py
+++ b/scripts/rpki/sql.py
@@ -32,16 +32,30 @@ class sql_persistant(object):
# to SQL.
sql_dirty = False
- ## @var sql_attributes
- # Tuple of attributes to translate between this Python object and its SQL representation.
- sql_attributes = None # Must be overriden by derived type
-
## @var sql_id_name
- # Name of the (auto-increment) ID column for this table, or None if it doesn't have one.
+ # Name of the attribute in which to store the auto-increment ID
+ # column for this table; None if it doesn't have an auto-increment
+ # ID or we don't want to store it.
sql_id_name = None
+ ## @var sql_select_cmd
+ # Command to SELECT this object from SQL
+ sql_select_cmd = None
+
+ ## @var sql_insert_cmd
+ # Command to INSERT this object into SQL
+ sql_insert_cmd = None
+
+ ## @var sql_update_cmd
+ # Command to UPDATE this object into SQL
+ sql_update_cmd = None
+
+ ## @var sql_delete_cmd
+ # Command to DELETE this object from SQL
+ sql_delete_cmd = None
+
@classmethod
- def sql_fetch(cls, db, cur=None, arg_dict=None, **kwargs):
+ def sql_fetch(cls, db, cur=None, select_dict=None, sql_parent=None):
"""Fetch rows from SQL based on a canned query and a set of
keyword arguments, and instantiate them as objects, returning a
list of the instantiated objects.
@@ -54,25 +68,16 @@ class sql_persistant(object):
result = []
if cur is None:
cur = db.cursor()
- if arg_dict is None:
- arg_dict = kwargs
- else:
- assert len(kwargs) == 0
- cur.execute(self.sql_select_cmd % arg_dict)
+ cur.execute(self.sql_select_cmd, select_dict)
for row in cur.fetchall():
self = cls()
self.in_sql = True
- self.sql_objectify(*row)
+ self.sql_decode(sql_parent, *row)
result.append(self)
- attr_dict = self.sql_makedict()
+ self_dict = self.sql_encode()
for kid_name,kid_type in self.sql_children.items():
- setattr(self, kid_name, kid_type.sql_fetch(db, cur, attr_dict))
+ setattr(self, kid_name, kid_type.sql_fetch(db, cur, self_dict, self))
return result
-
- def sql_objectify(self):
- """Initialize self with values returned by self.sql_fetch().
- """
- raise NotImplementedError
def sql_store(self, db, cur=None):
"""Save an object and its descendents to SQL.
@@ -80,11 +85,11 @@ class sql_persistant(object):
if cur is None:
cur = db.cursor()
if not self.sql_in_db:
- cur.execute(self.sql_insert_cmd % self.sql_makedict())
+ cur.execute(self.sql_insert_cmd, self.sql_encode())
if self.sql_id_name is not None:
setattr(self, self.sql_id_name, cur.lastrowid)
elif self.sql_dirty:
- cur.execute(self.sql_update_cmd % self.sql_makedict())
+ cur.execute(self.sql_update_cmd, self.sql_encode())
self.sql_dirty = False
self.sql_in_db = True
for kids in self.sql_children:
@@ -97,14 +102,20 @@ class sql_persistant(object):
if cur is None:
cur = db.cursor()
if self.sql_in_db:
- cur.execute(self.sql_delete_cmd % self.sql_makedict())
+ cur.execute(self.sql_delete_cmd, self.sql_encode())
self.sql_in_db = False
for kids in self.sql_children:
for kid in getattr(self, kids):
kid.sql_delete(db, cur)
- def sql_makedict(self):
- """Copy attributes from this object into a dict for use with
- canned SQL queries.
+ def sql_encode(self):
+ """Convert object attributes into a dict for use with canned
+ SQL queries.
"""
- return dict((a, getattr(self, a)) for a in self.sql_attributes)
+ raise NotImplementedError
+ #return dict((a, getattr(self, a)) for a in self.sql_attributes)
+
+ def sql_decode(self):
+ """Initialize an object with values returned by self.sql_fetch().
+ """
+ raise NotImplementedError