Skip to content

Commit 5212450

Browse files
committed
order_by using Column object instead of string
1 parent 42500de commit 5212450

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

piccolo/query/base.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,3 @@ def response_handler(self, response):
8888
the database driver.
8989
"""
9090
return response
91-
92-
def _is_valid_column_name(self, column_name: str):
93-
if column_name.startswith('-'):
94-
column_name = column_name[1:]
95-
if column_name not in [i._name for i in self.table.Meta.columns]:
96-
raise ValueError(f"{column_name} isn't a valid column name")

piccolo/query/mixins.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ def __str__(self):
2121

2222
@dataclasses.dataclass
2323
class OrderBy():
24-
column_name: str
24+
columns: t.Tuple[Column]
2525
ascending: bool
2626

2727
def __str__(self):
2828
order = 'ASC' if self.ascending else 'DESC'
29-
return f' ORDER BY {self.column_name} {order}'
29+
columns_names = ', '.join([i._name for i in self.columns])
30+
return f' ORDER BY {columns_names} {order}'
3031

3132

3233
@dataclasses.dataclass
@@ -56,15 +57,8 @@ def __init__(self):
5657
super().__init__()
5758
self._order_by: t.Optional[OrderBy] = None
5859

59-
def order_by(self, column_name: str):
60-
self._is_valid_column_name(column_name)
61-
62-
ascending = True
63-
if column_name.startswith('-'):
64-
ascending = False
65-
column_name = column_name[1:]
66-
67-
self._order_by = OrderBy(column_name, ascending)
60+
def order_by(self, *columns: Column, ascending=True):
61+
self._order_by = OrderBy(columns, ascending)
6862
return self
6963

7064

tests/table/test_select.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_where_greater_equal_than(self):
7171
).where(
7272
Band.popularity >= 1000
7373
).order_by(
74-
'name'
74+
Band.name
7575
).run_sync()
7676

7777
print(f'response = {response}')
@@ -136,6 +136,8 @@ def test_where_or(self):
136136
Band.name
137137
).where(
138138
(Band.name == 'Rustaceans') | (Band.name == 'CSharps')
139+
).order_by(
140+
Band.name
139141
).run_sync()
140142

141143
print(f'response = {response}')
@@ -182,6 +184,8 @@ def test_complex_where(self):
182184
).where(
183185
((Band.popularity == 2000) & (Band.manager == 'Graydon')) |
184186
((Band.popularity == 10) & (Band.manager == 'Mads'))
187+
).order_by(
188+
Band.name
185189
).run_sync()
186190

187191
print(f'response = {response}')
@@ -197,7 +201,7 @@ def test_limit(self):
197201
response = Band.select.columns(
198202
Band.name
199203
).order_by(
200-
'name'
204+
Band.name
201205
).limit(
202206
1
203207
).run_sync()
@@ -215,7 +219,7 @@ def test_first(self):
215219
response = Band.select.columns(
216220
Band.name
217221
).order_by(
218-
'name'
222+
Band.name
219223
).first().run_sync()
220224

221225
print(f'response = {response}')
@@ -231,7 +235,7 @@ def test_order_by_ascending(self):
231235
response = Band.select.columns(
232236
Band.name
233237
).order_by(
234-
'name'
238+
Band.name
235239
).limit(1).run_sync()
236240

237241
print(f'response = {response}')
@@ -247,7 +251,8 @@ def test_order_by_decending(self):
247251
response = Band.select.columns(
248252
Band.name
249253
).order_by(
250-
'-name'
254+
Band.name,
255+
ascending=False
251256
).limit(1).run_sync()
252257

253258
print(f'response = {response}')

0 commit comments

Comments
 (0)