From ca330a99e2e2bbf9a1be7ea6ffb24fa109efb05d Mon Sep 17 00:00:00 2001 From: Massimo Date: Fri, 25 Jan 2013 15:29:04 -0600 Subject: [PATCH] issue 1302, support for expressions in CONTAINS, thanks Paolo --- VERSION | 2 +- gluon/dal.py | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/VERSION b/VERSION index da23749e..629d9d45 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.4.1-alpha.2+timestamp.2013.01.24.15.51.43 +Version 2.4.1-alpha.2+timestamp.2013.01.25.15.28.24 diff --git a/gluon/dal.py b/gluon/dal.py index 69c0af9e..055f4eaa 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -1234,11 +1234,22 @@ class BaseAdapter(ConnectionPool): self.expand('%'+second, 'string')) def CONTAINS(self, first, second): - if first.type in ('string', 'text', 'json'): - key = '%'+str(second).replace('%','%%')+'%' - elif first.type.startswith('list:'): - key = '%|'+str(second).replace('|','||').replace('%','%%')+'|%' - return '(%s LIKE %s)' % (self.expand(first),self.expand(key,'string')) + field = self.expand(first) + if isinstance(second,Expression): + expr = self.expand(second,'string') + if first.type.startswith('list:'): + expr = 'CONCAT("|", %s, "|")' % expr + elif not first.type in ('string', 'text', 'json'): + raise RuntimeError("Expression Not Supported") + return 'INSTR(%s,%s)' % (field, expr) + else: + if first.type in ('string', 'text', 'json'): + key = '%'+str(second).replace('%','%%')+'%' + elif first.type.startswith('list:'): + key = '%|'+str(second).replace('|','||').replace('%','%%')+'|%' + else: + raise RuntimeError("Expression Not Supported") + return '(%s LIKE %s)' % (field,self.expand(key,'string')) def EQ(self, first, second=None): if second is None: