Author: eallen
Date: 2008-10-17 22:18:27 -0400 (Fri, 17 Oct 2008)
New Revision: 2656
Modified:
mgmt/trunk/cumin/python/wooly/tables.py
mgmt/trunk/cumin/python/wooly/tables.strings
Log:
A more complete find_item method that will allow more advanced finds in the future.
Modified: mgmt/trunk/cumin/python/wooly/tables.py
===================================================================
--- mgmt/trunk/cumin/python/wooly/tables.py 2008-10-18 02:17:01 UTC (rev 2655)
+++ mgmt/trunk/cumin/python/wooly/tables.py 2008-10-18 02:18:27 UTC (rev 2656)
@@ -177,6 +177,7 @@
self.__sql_tmpl = Template(self, "sql")
self.__count_sql_tmpl = Template(self, "count_sql")
+ self.__find_sql_tmpl = Template(self, "find_sql")
def render_sql(self, session, *args):
writer = Writer()
@@ -186,6 +187,9 @@
def render_sql_where(self, session, *args):
pass
+ def render_find_sql_where(self, session, *args):
+ pass
+
def render_sql_orderby(self, session, *args):
scol = self.get_selected_column(session)
@@ -199,6 +203,11 @@
self.__count_sql_tmpl.render(writer, session, *args)
return writer.to_string()
+ def render_find_sql(self, session, *args):
+ writer = Writer()
+ self.__find_sql_tmpl.render(writer, session, *args)
+ return writer.to_string()
+
def build_sql(self, elems):
writer = Writer()
@@ -212,6 +221,9 @@
def get_sql_values(self, session, *args):
return None
+ def get_find_sql_values(self, session, *args):
+ return None
+
def get_connection(self, session):
pass
@@ -258,19 +270,39 @@
return writer.to_string()
- def find_item(self, session, colname, value, *args):
- cursor = self.get_items(session, *args)
- cols = [spec[0] for spec in cursor.description]
- if colname in cols:
- data = dict()
- for tuple in cursor:
- for col, datum in zip(cols, tuple):
- data[col] = datum
-
- if data[colname] == value:
- return data
+ def find_item(self, session, *args):
+ """ Find items in the current ItemSet
+
+ To use this an SqlTable derived object needs to have a
+ [<class>.find_sql] section and override render_find_sql_where
+ and get_find_sql_values.
+ Returns a list of dictionaries. Each dictionary is a matched row.
"""
+
+ conn = self.get_connection(session)
+ if conn:
+ cursor = conn.cursor()
+ select = self.render_find_sql(session, *args)
+ where = self.render_sql_where(session, *args)
+ find_where = self.render_find_sql_where(session, *args)
+ find_values = self.get_find_sql_values(session, *args)
+ sql = " and ".join(["%s %s" % (select, where),
find_where])
+ try:
+ cursor.execute(sql, find_values)
+ cols = [spec[0] for spec in cursor.description]
+ row = dict()
+ rows = list()
+ for tuple in cursor:
+ for col, datum in zip(cols, tuple):
+ row[col] = datum
+
+ rows.append(row)
+
+ return rows
+ except:
+ pass
+
class SqlTableColumn(ItemTableColumn):
# XXX to be consistent with similar methods, rename to
# get_sql_order_by
Modified: mgmt/trunk/cumin/python/wooly/tables.strings
===================================================================
--- mgmt/trunk/cumin/python/wooly/tables.strings 2008-10-18 02:17:01 UTC (rev 2655)
+++ mgmt/trunk/cumin/python/wooly/tables.strings 2008-10-18 02:18:27 UTC (rev 2656)
@@ -42,3 +42,6 @@
[ItemTableColumnHeader.html]
<th {class_attr}><a class="ItemTableColumnHeader"
href="{href}"><span><span
class="{sorted_dir}">{content}</span></span></a></th>
+
+[SqlTable.find_sql]
+select * from %s
\ No newline at end of file