Skip to content

Commit

Permalink
Merge pull request kayak#387 from kayak/fix_array_alias
Browse files Browse the repository at this point in the history
Added alias syntax to tuple and array values
  • Loading branch information
twheys authored Apr 9, 2020
2 parents f832f2a + f898a25 commit e77550f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
43 changes: 17 additions & 26 deletions pypika/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def wrap_constant(val, wrapper_cls=None):
querybuilder will be returned as inputted.
"""
from .queries import QueryBuilder

if isinstance(val, Node):
return val
Expand Down Expand Up @@ -476,14 +475,11 @@ def replace_table(self, current_table, new_table):
"""
self.table = new_table if self.table == current_table else self.table

def get_sql(
self,
with_alias=False,
with_namespace=False,
quote_char=None,
secondary_quote_char="'",
**kwargs
):
def get_sql(self, **kwargs):
with_alias = kwargs.pop("with_alias", False)
with_namespace = kwargs.pop("with_namespace", False)
quote_char = kwargs.pop("quote_char", None)

field_sql = format_quotes(self.name, quote_char)

# Need to add namespace if the table has an alias
Expand Down Expand Up @@ -541,7 +537,8 @@ def nodes_(self):
yield from value.nodes_()

def get_sql(self, **kwargs):
return "({})".format(",".join(term.get_sql(**kwargs) for term in self.values))
sql = "({})".format(",".join(term.get_sql(**kwargs) for term in self.values))
return format_alias_sql(sql, self.alias, **kwargs)

@property
def is_aggregate(self):
Expand All @@ -568,23 +565,19 @@ def replace_table(self, current_table, new_table):
class Array(Tuple):
def get_sql(self, **kwargs):
dialect = kwargs.get("dialect", None)
template = (
values = ",".join(term.get_sql(**kwargs) for term in self.values)
sql = (
"ARRAY[{}]"
if dialect in (Dialects.POSTGRESQL, Dialects.REDSHIFT)
else "[{}]"
)

return template.format(",".join(term.get_sql(**kwargs) for term in self.values))
).format(values)
return format_alias_sql(sql, self.alias, **kwargs)


class Bracket(Tuple):
def __init__(self, term):
super(Bracket, self).__init__(term)

def get_sql(self, **kwargs):
sql = super(Bracket, self).get_sql(**kwargs)
return format_alias_sql(sql=sql, alias=self.alias, **kwargs)


class NestedCriterion(Criterion):
def __init__(self, comparator, nested_comparator, left, right, nested, alias=None):
Expand Down Expand Up @@ -1156,14 +1149,12 @@ def get_function_sql(self, **kwargs):
special=(" " + special_params_sql) if special_params_sql else "",
)

def get_sql(
self,
with_alias=False,
with_namespace=False,
quote_char=None,
dialect=None,
**kwargs
):
def get_sql(self, **kwargs):
with_alias = kwargs.pop("with_alias", False)
with_namespace = kwargs.pop("with_namespace", False)
quote_char = kwargs.pop("quote_char", None)
dialect = kwargs.pop("dialect", None)

# FIXME escape
function_sql = self.get_function_sql(
with_namespace=with_namespace, quote_char=quote_char, dialect=dialect
Expand Down
21 changes: 20 additions & 1 deletion pypika/tests/dialects/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import OrderedDict

from pypika import (
Array,
JSON,
Table,
)
Expand Down Expand Up @@ -213,8 +214,26 @@ class DistinctOnTests(unittest.TestCase):
table_abc = Table("abc")

def test_distinct_on(self):
q = PostgreSQLQuery.from_(self.table_abc).distinct_on('lname', self.table_abc.fname).select('lname', 'id')
q = (
PostgreSQLQuery.from_(self.table_abc)
.distinct_on("lname", self.table_abc.fname)
.select("lname", "id")
)

self.assertEqual(
'''SELECT DISTINCT ON("lname","fname") "lname","id" FROM "abc"''', str(q)
)


class ArrayTests(unittest.TestCase):
def test_array_syntax(self):
tb = Table("tb")
q = PostgreSQLQuery.from_(tb).select(Array(1, "a", ["b", 2, 3]))

self.assertEqual(str(q), "SELECT ARRAY[1,'a',ARRAY['b',2,3]] FROM \"tb\"")

def test_render_alias_in_array_sql(self):
tb = Table("tb")

q = PostgreSQLQuery.from_(tb).select(Array(tb.col).as_("different_name"))
self.assertEqual(str(q), 'SELECT ARRAY["col"] "different_name" FROM "tb"')
19 changes: 11 additions & 8 deletions pypika/tests/test_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from pypika import (
Array,
Bracket,
Dialects,
PostgreSQLQuery,
Query,
Table,
Tables,
Tuple,
)
Expand Down Expand Up @@ -118,6 +118,12 @@ def test_tuples_in_join(self):
str(query),
)

def test_render_alias_in_array_sql(self):
tb = Table("tb")

q = Query.from_(tb).select(Tuple(tb.col).as_("different_name"))
self.assertEqual(str(q), 'SELECT ("col") "different_name" FROM "tb"')


class ArrayTests(unittest.TestCase):
table_abc, table_efg = Tables("abc", "efg")
Expand All @@ -126,15 +132,12 @@ def test_array_general(self):
query = Query.from_(self.table_abc).select(Array(1, "a", ["b", 2, 3]))

self.assertEqual("SELECT [1,'a',['b',2,3]] FROM \"abc\"", str(query))
self.assertEqual("SELECT [1,'a',['b',2,3]] FROM \"abc\"", str(query.get_sql()))

def test_array_postgresql(self):
query = PostgreSQLQuery.from_(self.table_abc).select(Array(1, "a", ["b", 2, 3]))
def test_render_alias_in_array_sql(self):
tb = Table("tb")

self.assertEqual("SELECT ARRAY[1,'a',ARRAY['b',2,3]] FROM \"abc\"", str(query))
self.assertEqual(
"SELECT ARRAY[1,'a',ARRAY['b',2,3]] FROM \"abc\"", query.get_sql()
)
q = Query.from_(tb).select(Array(tb.col).as_("different_name"))
self.assertEqual(str(q), 'SELECT ["col"] "different_name" FROM "tb"')


class BracketTests(unittest.TestCase):
Expand Down

0 comments on commit e77550f

Please sign in to comment.