aboutsummaryrefslogtreecommitdiff
path: root/scripts/rpki/sql.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2007-09-06 03:59:08 +0000
committerRob Austein <sra@hactrn.net>2007-09-06 03:59:08 +0000
commit69d8f8fdb09f86984cd9ee23c009102e581b55c6 (patch)
tree387ec25a8dda835bf8d3ce7a4c896108639bbf26 /scripts/rpki/sql.py
parent51db2e40055f447b26836810e8f5cec50c7c40fc (diff)
Checkpoint
svn path=/scripts/rpki/sql.py; revision=927
Diffstat (limited to 'scripts/rpki/sql.py')
-rw-r--r--scripts/rpki/sql.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/scripts/rpki/sql.py b/scripts/rpki/sql.py
new file mode 100644
index 00000000..f0e2aa6d
--- /dev/null
+++ b/scripts/rpki/sql.py
@@ -0,0 +1,59 @@
+# $Id$
+
+import MySQLdb
+
+def connect(cfg, section="sql"):
+ """Connect to a MySQL database using connection parameters from an
+ rpki.config.parser object.
+ """
+
+ return MySQLdb.connect(user = cfg.get(section, "sql-username"),
+ db = cfg.get(section, "sql-database"),
+ passwd = cfg.get(section, "sql-password"))
+
+class sql_persistant(object):
+ """Mixin for persistant class that needs to be stored in SQL.
+ """
+
+ ## @var sql_children
+ # Dictionary listing this class's children in the tree of SQL
+ # tables. Key is the class object of a child, value is the name of
+ # the attribute in this class at which a list of the resulting child
+ # objects are stored.
+ sql_children = {}
+
+ @classmethod
+ def sql_fetch(cls, db, **kwargs):
+ """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.
+
+ This is a class method because in general we don't even know how
+ many matches the SQL lookup will return until after we've
+ performed it.
+ """
+
+ cur = db.cursor()
+ cur.execute(self.sql_fetch_cmd % kwargs)
+ rows = cur.fetchall()
+ cur.close()
+ objs = []
+ for row in rows:
+ obj = cls()
+ 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):
+ """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