summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorGaëtan de Menten <gdementen@gmail.com>2007-10-19 10:27:06 +0000
committerGaëtan de Menten <gdementen@gmail.com>2007-10-19 10:27:06 +0000
commitae553db3cdb71c3cc14ca02bf53d6a81ff99fb8a (patch)
treeb2b536c0b36a40b5d5ea65df62b61c6301f27e34 /lib/sqlalchemy/sql
parent974f59f091d562bb0e141c6b4087160d16190c1c (diff)
downloadsqlalchemy-ae553db3cdb71c3cc14ca02bf53d6a81ff99fb8a.tar.gz
- Added contains operator (which generate a "LIKE %<other>%" clause).
- Added test coverage for endswith operator
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/expression.py22
-rw-r--r--lib/sqlalchemy/sql/operators.py3
2 files changed, 23 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 73e23cc92..fd96c52cd 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -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)
diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py
index cf8aeb71d..78159697b 100644
--- a/lib/sqlalchemy/sql/operators.py
+++ b/lib/sqlalchemy/sql/operators.py
@@ -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()