[rql] support CAST() Function
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 27 Apr 2011 11:37:42 +0200
changeset 7256 ccd44caeb936
parent 7253 68b9e21efa63
child 7257 beea955b45e2
[rql] support CAST() Function
server/sources/rql2sql.py
server/test/unittest_rql2sql.py
--- a/server/sources/rql2sql.py	Wed Apr 27 09:41:55 2011 +0200
+++ b/server/sources/rql2sql.py	Wed Apr 27 11:37:42 2011 +0200
@@ -1407,6 +1407,8 @@
         if constant.type is None:
             return 'NULL'
         value = constant.value
+        if constant.type == 'etype':
+            return value
         if constant.type == 'Int' and  isinstance(constant.parent, SortTerm):
             return value
         if constant.type in ('Date', 'Datetime'):
--- a/server/test/unittest_rql2sql.py	Wed Apr 27 09:41:55 2011 +0200
+++ b/server/test/unittest_rql2sql.py	Wed Apr 27 11:37:42 2011 +0200
@@ -1331,6 +1331,11 @@
                     '''SELECT SUBSTR(_P.cw_nom, 1, 1)
 FROM cw_Personne AS _P''')
 
+    def test_cast(self):
+        self._check("Any CAST(String, P) WHERE P is Personne",
+                    '''SELECT CAST(_P.cw_eid AS text)
+FROM cw_Personne AS _P''')
+
     def test_parser_parse(self):
         for t in self._parse(PARSER):
             yield t
@@ -1726,6 +1731,10 @@
         for t in self._parse(WITH_LIMIT):# + ADVANCED_WITH_LIMIT_OR_ORDERBY):
             yield t
 
+    def test_cast(self):
+        self._check("Any CAST(String, P) WHERE P is Personne",
+                    '''SELECT CAST(_P.cw_eid AS nvarchar(max))
+FROM cw_Personne AS _P''')
 
 
 class SqliteSQLGeneratorTC(PostgresSQLGeneratorTC):
@@ -1879,6 +1888,11 @@
                     '''SELECT EXTRACT(MONTH from _P.cw_creation_date)
 FROM cw_Personne AS _P''')
 
+    def test_cast(self):
+        self._check("Any CAST(String, P) WHERE P is Personne",
+                    '''SELECT CAST(_P.cw_eid AS mediumtext)
+FROM cw_Personne AS _P''')
+
     def test_from_clause_needed(self):
         queries = [("Any 1 WHERE EXISTS(T is CWGroup, T name 'managers')",
                     '''SELECT 1