Commits

Kirill Simonov committed 5c0dcc2

Added `string()` cast.

Comments (0)

Files changed (3)

src/htsql/tr/serializer.py

     def to_float(self, value):
         return "CAST(%s AS FLOAT)" % value
 
+    def to_string(self, value):
+        return "CAST(%s AS TEXT)" % value
+
     def is_null(self, arg):
         return "(%s IS NULL)" % arg
 
         return self.format.to_float(value)
 
 
+class SerializeToString(SerializeTo):
+
+    adapts(StringDomain, Domain, Serializer)
+
+    def serialize(self, phrase):
+        value = self.serializer.serialize(phrase)
+        return self.format.to_string(value)
+
+
 class SerializeLiteral(SerializePhrase):
 
     adapts(LiteralPhrase, Serializer)

test/input/pgsql.yaml

         - uri: /{round(35536e-3,1)}
           expect: 400
 
+      - title: String functions and operators
+        tests:
+        # String cast (from untyped, string, integer, decimal, float).
+        - uri: /{string('X'),string(string('X')), string(1), string(1.0), string(1e0)}
+
   # Simple (non-aggregate) filters.
   - title: Simple filters
     tests:

test/output/pgsql.yaml

             invalid argument: unexpected argument:
                 /{round(35536e-3,1)}
                                  ^
+      - id: string-functions-and-operators
+        tests:
+        - uri: /{string('X'),string(string('X')), string(1), string(1.0), string(1e0)}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | /{string('X'),string(string('X')),string(1),string(1.0),string(1e0)} |
+            -+----------------------------------------------------------------------+-
+             | string('X')  | string('X')  | string(1)  | string(1.0) | string(1e0) |
+            -+--------------+--------------+------------+-------------+-------------+-
+             | X            | X            | 1          | 1.0         | 1           |
+                                                                              (1 row)
+
+             ----
+             /{string('X'),string(string('X')),string(1),string(1.0),string(1e0)}
+             SELECT 'X', 'X', CAST(1 AS TEXT), CAST(1.0 AS TEXT), CAST(1.0::float8 AS TEXT)
   - id: simple-filters
     tests:
     - uri: /school?code='ns'