Author: eallen
Date: 2010-11-29 10:51:15 -0500 (Mon, 29 Nov 2010)
New Revision: 4416
Modified:
mgmt/trunk/cumin/python/cumin/objectselector.py
mgmt/trunk/cumin/python/cumin/sqladapter.py
mgmt/trunk/rosemary/python/rosemary/model.py
mgmt/trunk/rosemary/python/rosemary/sqlfilter.py
Log:
Implemented BZ 64721: Added partial match filters to selector tables. Added Begins and
Contains filters. Filters are case sensitive.
Modified: mgmt/trunk/cumin/python/cumin/objectselector.py
===================================================================
--- mgmt/trunk/cumin/python/cumin/objectselector.py 2010-11-20 15:14:52 UTC (rev 4415)
+++ mgmt/trunk/cumin/python/cumin/objectselector.py 2010-11-29 15:51:15 UTC (rev 4416)
@@ -25,6 +25,9 @@
# (RosemaryAttribute this, RosemaryAttribute that, Attribute object)
self.filter_specs = list()
+ # ((RosemaryAttribute this, Attribute value, SqlLikeFilter.BEGINS)
+ self.like_specs = list()
+
def init(self):
if not self.adapter:
self.adapter = ObjectSqlAdapter(self.app, self.cls)
@@ -34,6 +37,9 @@
for this, that, fobj in self.filter_specs:
self.adapter.add_value_filter(this)
+ for this, vattr, type in self.like_specs:
+ self.adapter.add_like_filter(this)
+
if self.sort.default is None:
for col in self.columns:
if col.sortable:
@@ -67,6 +73,12 @@
self.add_filter(attribute, this, that)
+ def add_like_filter(self, attribute, this, type=SqlLikeFilter.BEGINS):
+ assert isinstance(attribute, Attribute), attribute
+ assert isinstance(this, RosemaryAttribute), this
+
+ self.like_specs.append((this, attribute, type))
+
def get_data_values(self, session):
values = dict()
@@ -74,6 +86,11 @@
obj = fobj.get(session)
values[this.name] = getattr(obj, that.name)
+ for this, vattr, type in self.like_specs:
+ value = vattr.get(session)
+ pre = type == SqlLikeFilter.CONTAINS and "%%" or ""
+ values[this.name] = "%s%s%%" % (pre, value)
+
return values
def render_title(self, session):
Modified: mgmt/trunk/cumin/python/cumin/sqladapter.py
===================================================================
--- mgmt/trunk/cumin/python/cumin/sqladapter.py 2010-11-20 15:14:52 UTC (rev 4415)
+++ mgmt/trunk/cumin/python/cumin/sqladapter.py 2010-11-29 15:51:15 UTC (rev 4416)
@@ -89,6 +89,12 @@
filter = SqlComparisonFilter(attr.sql_column, value)
self.query.add_filter(filter)
+ def add_like_filter(self, attr):
+ assert attr
+
+ filter = SqlLikeFilter(attr.sql_column)
+ self.query.add_filter(filter)
+
class ObjectSqlField(SqlField):
def __init__(self, adapter, attr):
assert isinstance(adapter, ObjectSqlAdapter), adapter
Modified: mgmt/trunk/rosemary/python/rosemary/model.py
===================================================================
--- mgmt/trunk/rosemary/python/rosemary/model.py 2010-11-20 15:14:52 UTC (rev 4415)
+++ mgmt/trunk/rosemary/python/rosemary/model.py 2010-11-29 15:51:15 UTC (rev 4416)
@@ -375,6 +375,25 @@
return selection
+ def get_selection_like(self, cursor, **criteria):
+ selection = list()
+ sql = SqlSelect(self.sql_table)
+
+ for name in criteria:
+ column = self.sql_table._columns_by_name[name]
+ sql.add_filter(SqlLikeFilter(column))
+
+ sql.execute(cursor, criteria)
+
+ for record in cursor.fetchall():
+ obj = RosemaryObject(self, None)
+
+ self.set_object_attributes(obj, self.sql_table._columns, record)
+
+ selection.append(obj)
+
+ return selection
+
def get_object_by_id(self, cursor, id):
assert id
Modified: mgmt/trunk/rosemary/python/rosemary/sqlfilter.py
===================================================================
--- mgmt/trunk/rosemary/python/rosemary/sqlfilter.py 2010-11-20 15:14:52 UTC (rev 4415)
+++ mgmt/trunk/rosemary/python/rosemary/sqlfilter.py 2010-11-29 15:51:15 UTC (rev 4416)
@@ -32,3 +32,9 @@
def emit(self):
return "%s (%s)" % (self.operator, self.subquery)
+
+class SqlLikeFilter(SqlValueFilter):
+ BEGINS = "B"
+ CONTAINS = "C"
+ def __init__(self, this, operator="like"):
+ super(SqlLikeFilter, self).__init__(this, operator)