]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added contains operator (which generate a "LIKE %<other>%" clause).
authorGaëtan de Menten <gdementen@gmail.com>
Fri, 19 Oct 2007 10:27:06 +0000 (10:27 +0000)
committerGaëtan de Menten <gdementen@gmail.com>
Fri, 19 Oct 2007 10:27:06 +0000 (10:27 +0000)
- Added test coverage for endswith operator

CHANGES
lib/sqlalchemy/sql/expression.py
lib/sqlalchemy/sql/operators.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index eab3ec13744ba2b3b345842af93e4cd191b67410..c788f3b5cea517c27e7d08e8db70fb7b59db8f97 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -10,6 +10,9 @@ CHANGES
 
 - Fixed empty (zero column) sqlite inserts, allowing inserts on
   autoincrementing single column tables.
+
+- Added contains operator (which generate a "LIKE %<other>%" clause).
+
   
 0.4.0
 -----
index 73e23cc92ec50a6b8a5492f7754c50c662862430..fd96c52cd73ae692db03be30d51d3d3c6fee3b9e 100644 (file)
@@ -1088,6 +1088,9 @@ class ColumnOperators(Operators):
     def endswith(self, other):
         return self.operate(operators.endswith_op, other)
 
+    def contains(self, other):
+        return self.operate(operators.contains_op, other)
+
     def desc(self):
         return self.operate(operators.desc_op)
 
@@ -1195,6 +1198,9 @@ class _CompareMixin(ColumnOperators):
         if op == operators.add and isinstance(type_, (sqltypes.Concatenable)):
             op = operators.concat_op
         return _BinaryExpression(self.expression_element(), obj, op, type_=type_)
+
+    # a mapping of operators with the method they use, along with their negated
+    # operator for comparison operators
     operators = {
         operators.add : (__operate,),
         operators.mul : (__operate,),
@@ -1251,18 +1257,30 @@ class _CompareMixin(ColumnOperators):
     def startswith(self, other):
         """Produce the clause ``LIKE '<other>%'``"""
 
-        perc = isinstance(other,(str,unicode)) and '%' or literal('%',type_= sqltypes.String)
+        perc = isinstance(other, basestring) and '%' or literal('%', type_=sqltypes.String)
         return self.__compare(operators.like_op, other + perc)
 
     def endswith(self, other):
         """Produce the clause ``LIKE '%<other>'``"""
 
-        if isinstance(other,(str,unicode)): po = '%' + other
+        if isinstance(other, basestring): 
+            po = '%' + other
         else:
             po = literal('%', type_=sqltypes.String) + other
             po.type = sqltypes.to_instance(sqltypes.String)     #force!
         return self.__compare(operators.like_op, po)
 
+    def contains(self, other):
+        """Produce the clause ``LIKE '%<other>%'``"""
+
+        if isinstance(other, basestring): 
+            po = '%' + other + '%'
+        else:
+            perc = literal('%', type_=sqltypes.String)
+            po = perc + other + perc
+            po.type = sqltypes.to_instance(sqltypes.String)     #force!
+        return self.__compare(operators.like_op, po)
+
     def label(self, name):
         """Produce a column label, i.e. ``<columnname> AS <name>``"""
         return _Label(name, self, self.type)
index cf8aeb71db0dac0843674cfef5ad5914a1985e85..78159697be0ffc72bdf25c427f43b032af384341 100644 (file)
@@ -51,6 +51,9 @@ def startswith_op(a, b):
 def endswith_op(a, b):
     return a.endswith(b)
 
+def contains_op(a, b):
+    return a.contains(b)
+
 def comma_op(a, b):
     raise NotImplementedError()
 
index 5c8b570d7998336a4a5b47e59e8c895e1ea9292f..56bf1d92ca8377e13659476b0e7b3ce2a4d386e8 100644 (file)
@@ -417,6 +417,18 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
         clause = (table1.c.myid == 12) & table1.c.myid.between(15, 20) & table1.c.myid.like('hoho')
         assert str(clause) == str(util.pickle.loads(util.pickle.dumps(clause)))
 
+    def testextracomparisonoperators(self):
+        self.assert_compile(
+            table1.select(table1.c.name.contains('jo')),
+            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.name LIKE :mytable_name",
+            checkparams = {'mytable_name': u'%jo%'},
+        )
+        self.assert_compile(
+            table1.select(table1.c.name.endswith('hn')),
+            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.name LIKE :mytable_name",
+            checkparams = {'mytable_name': u'%hn'},
+        )
+
     def testunicodestartswith(self):
         string = u"hi \xf6 \xf5"
         self.assert_compile(