Author: justi9
Date: 2009-09-30 10:08:03 -0400 (Wed, 30 Sep 2009)
New Revision: 3653
Modified:
mgmt/trunk/wooly/python/wooly/sql.py
Log:
Move the lower-level sql machinery into a reusable superclass
Modified: mgmt/trunk/wooly/python/wooly/sql.py
===================================================================
--- mgmt/trunk/wooly/python/wooly/sql.py 2009-09-28 21:27:04 UTC (rev 3652)
+++ mgmt/trunk/wooly/python/wooly/sql.py 2009-09-30 14:08:03 UTC (rev 3653)
@@ -5,16 +5,58 @@
strings = StringCatalog(__file__)
log = logging.getLogger("wooly.sql")
-class SqlDataSet(object):
+class SqlOperation(object):
def __init__(self, app):
- super(SqlDataSet, self).__init__()
+ super(SqlOperation, self).__init__()
self.app = app
sql_string = self.get_string("sql")
+ self.sql_tmpl = ObjectTemplate(self, sql_string)
+
+ # XXX instead of this, make the lookup logic on Widget generic and
+ # use it here as well
+ def get_string(self, key):
+ cls = self.__class__
+ module = sys.modules[cls.__module__]
+ strs = module.__dict__.get("strings")
+
+ if strs:
+ return strs.get(cls.__name__ + "." + key)
+
+ def get_connection(self, session):
+ pass
+
+ def do_execute(self, session, sql):
+ conn = self.get_connection(session)
+
+ if not conn:
+ raise Exception("Database error")
+
+ cursor = conn.cursor()
+
+ log.debug("Query: \n%s", sql)
+
+ cursor.execute(sql)
+
+ return cursor
+
+ def execute(self, session):
+ sql = self.render_sql(session)
+
+ return self.do_execute(session, sql)
+
+ def render_sql(self, session):
+ writer = Writer()
+ self.sql_tmpl.render(writer, session)
+ return writer.to_string()
+
+class SqlDataSet(SqlOperation):
+ def __init__(self, app):
+ super(SqlDataSet, self).__init__(app)
+
count_sql_string = self.get_string("count_sql")
- self.sql_tmpl = ObjectTemplate(self, sql_string)
self.count_sql_tmpl = ObjectTemplate(self, count_sql_string)
self.where_exprs = SessionAttribute(self, "where")
@@ -29,16 +71,6 @@
exprs = self.where_exprs.get(session)
exprs.append(expr % args)
- # XXX instead of this, make the lookup logic on Widget generic and
- # use it here as well
- def get_string(self, key):
- cls = self.__class__
- module = sys.modules[cls.__module__]
- strs = module.__dict__.get("strings")
-
- if strs:
- return strs.get(cls.__name__ + "." + key)
-
def get_connection(self, session):
pass
Show replies by date