Skip to content

Commit 3e74063

Browse files
author
Daniel Townsend
committed
using joins when required by order by
1 parent f6071b7 commit 3e74063

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

dev-requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
ipdb==0.11
22
ipython==7.1.1
33
twine==1.12.1
4-
mypy==0.650
5-
4+
mypy==0.710

piccolo/query/methods/select.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,14 @@ def querystring(self) -> QueryString:
9494

9595
select_joins = self.get_joins(self.selected_columns)
9696
where_joins = self.get_joins(self.get_where_columns())
97+
order_by_joins = self.get_joins(self.get_order_by_columns())
9798

9899
# Combine all joins, and remove duplicates
99-
joins = list(OrderedDict.fromkeys(select_joins + where_joins))
100+
joins = list(
101+
OrderedDict.fromkeys(
102+
select_joins + where_joins + order_by_joins
103+
)
104+
)
100105

101106
#######################################################################
102107

piccolo/query/mixins.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class OrderBy():
3535
@property
3636
def querystring(self) -> QueryString:
3737
order = 'ASC' if self.ascending else 'DESC'
38-
columns_names = ', '.join([i._name for i in self.columns])
38+
columns_names = ', '.join(
39+
[i.get_full_name(just_alias=True) for i in self.columns]
40+
)
3941
return QueryString(
4042
f' ORDER BY {columns_names} {order}'
4143
)
@@ -64,15 +66,15 @@ def get_where_columns(self):
6466
needed.
6567
"""
6668
self._where_columns = []
67-
self.extract_columns(self._where)
69+
self._extract_columns(self._where)
6870
return self._where_columns
6971

70-
def extract_columns(self, combinable: Combinable):
72+
def _extract_columns(self, combinable: Combinable):
7173
if isinstance(combinable, Where):
7274
self._where_columns.append(combinable.column)
7375
elif isinstance(combinable, And) or isinstance(combinable, Or):
74-
self.extract_columns(combinable.first)
75-
self.extract_columns(combinable.second)
76+
self._extract_columns(combinable.first)
77+
self._extract_columns(combinable.second)
7678

7779
def where(self, where: Combinable):
7880
if self._where:
@@ -88,6 +90,9 @@ def __init__(self):
8890
super().__init__()
8991
self._order_by: t.Optional[OrderBy] = None
9092

93+
def get_order_by_columns(self) -> t.List[Column]:
94+
return list(self._order_by.columns) if self._order_by else []
95+
9196
def order_by(self, *columns: Column, ascending=True):
9297
self._order_by = OrderBy(columns, ascending)
9398
return self

0 commit comments

Comments
 (0)