Repository: kayak/pypika
Pull request #35 (2017-03-31 13:48:50)

Replacing the overrides of get_sql in function implementations to imp…
…lement a separate function in order to preserve common behavior across all functions.


Patch:
diff --git a/pypika/functions.py b/pypika/functions.py
index acc7ab7..cdc0bd9 100644
--- a/pypika/functions.py
+++ b/pypika/functions.py
@@ -16,8 +16,8 @@ class Count(AggregateFunction):
         super(Count, self).__init__('COUNT', Star() if is_star else param, alias=alias)
         self._distinct = False
 
-    def get_sql(self, **kwargs):
-        s = super(Count, self).get_sql(**kwargs)
+    def get_function_sql(self, **kwargs):
+        s = super(Count, self).get_function_sql(**kwargs)
         if self._distinct:
             return s[:6] + 'DISTINCT ' + s[6:]
         return s
@@ -69,7 +69,7 @@ class Cast(Function):
     def __init__(self, term, as_type, alias=None):
         super(Cast, self).__init__('CAST', term, as_type, alias=alias)
 
-    def get_sql(self, **kwargs):
+    def get_function_sql(self, **kwargs):
         # FIXME escape
         return '{name}({field} AS {type})'.format(
             name=self.name,
@@ -82,7 +82,7 @@ class Convert(Function):
     def __init__(self, term, encoding, alias=None):
         super(Convert, self).__init__('CONVERT', term, encoding, alias=alias)
 
-    def get_sql(self, **kwargs):
+    def get_function_sql(self, **kwargs):
         # FIXME escape
         return '{name}({field} USING {type})'.format(
             name=self.name,
@@ -95,13 +95,6 @@ class ToChar(Function):
     def __init__(self, term, as_type, alias=None):
         super(ToChar, self).__init__('TO_CHAR', term, as_type, alias=alias)
 
-    def get_sql(self, **kwargs):
-        return '{name}({field}, {type})'.format(
-            name=self.name,
-            field=self.params[0],
-            type=self.params[1],
-        )
-
 
 class Signed(Cast):
     def __init__(self, term, alias=None):
@@ -205,7 +198,7 @@ class Extract(Function):
     def __init__(self, date_part, field, alias=None):
         super(Extract, self).__init__('EXTRACT', date_part, field, alias=alias)
 
-    def get_sql(self, **kwargs):
+    def get_function_sql(self, **kwargs):
         return '{name}({part} FROM {field})'.format(
             name=self.name,
             part=self.params[0],
diff --git a/pypika/terms.py b/pypika/terms.py
index 88d3494..acad034 100644
--- a/pypika/terms.py
+++ b/pypika/terms.py
@@ -599,14 +599,19 @@ class Function(Term):
                        for param in self.params]
         return self
 
-    def get_sql(self, with_alias=False, with_namespace=False, **kwargs):
-        # FIXME escape
-        return '{name}({params}){alias}'.format(
+    def get_function_sql(self, with_namespace=False):
+        return '{name}({params})'.format(
             name=self.name,
             params=','.join(p.get_sql(with_quotes=True, with_alias=False, with_namespace=with_namespace)
                             if hasattr(p, 'get_sql')
                             else str(p)
                             for p in self.params),
+        )
+
+    def get_sql(self, with_alias=False, with_namespace=False, **kwargs):
+        # FIXME escape
+        return '{function}{alias}'.format(
+            function=self.get_function_sql(with_namespace=with_namespace),
             alias=' \"{}\"'.format(self.alias) if self.alias is not None and with_alias else ''
         )
 


Test patch:
diff --git a/pypika/tests/test_functions.py b/pypika/tests/test_functions.py
index 0b16d8b..9ff6eac 100644
--- a/pypika/tests/test_functions.py
+++ b/pypika/tests/test_functions.py
@@ -394,7 +394,7 @@ class CastTests(unittest.TestCase):
     def test__tochar__(self):
         q = Q.from_(self.t).select(fn.ToChar(self.t.foo, "SomeFormat"))
 
-        self.assertEqual("SELECT TO_CHAR(\"foo\", 'SomeFormat') FROM \"abc\"", str(q))
+        self.assertEqual("SELECT TO_CHAR(\"foo\",'SomeFormat') FROM \"abc\"", str(q))
 
 
 class DateFunctionsTests(unittest.TestCase):