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)
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,),
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)
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(