Skip to content

Commit cc8b2c9

Browse files
committed
columns can now be passed into the select method
1 parent c0a9cf4 commit cc8b2c9

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

docs/src/piccolo/query_types/select.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ To get certain rows:
1717

1818
.. code-block:: python
1919
20-
>>> Band.select().columns(Band.name).run_sync()
20+
>>> Band.select(Band.name).run_sync()
2121
[{'name': 'Rustaceans'}, {'name': 'Pythonistas'}]
2222
2323
Or making an alias to make it shorter:
2424

2525
.. code-block:: python
2626
2727
>>> b = Band
28-
>>> b.select().columns(b.name).run_sync()
28+
>>> b.select(b.name).run_sync()
2929
[{'id': 1, 'name': 'Pythonistas', 'manager': 1, 'popularity': 1000},
3030
{'id': 2, 'name': 'Rustaceans', 'manager': 2, 'popularity': 500}]
3131
@@ -39,7 +39,7 @@ One of the most powerful things about select is it's support for joins.
3939
.. code-block:: python
4040
4141
b = Band
42-
b.select().columns(
42+
b.select(
4343
b.name,
4444
b.manager.name
4545
).run_sync()
@@ -50,7 +50,7 @@ The joins can go several layers deep.
5050
.. code-block:: python
5151
5252
c = Concert
53-
c.select().columns(
53+
c.select(
5454
c.id,
5555
c.band_1.manager.name
5656
).run_sync()
@@ -74,7 +74,8 @@ By default all columns are returned from the queried table.
7474
# Equivalent to SELECT * from band
7575
b.select().run_sync()
7676
77-
To restrict the returned columns, used the `columns` method.
77+
To restrict the returned columns, either pass in the columns into the
78+
``select`` method, or use the `columns` method.
7879

7980
.. code-block:: python
8081

piccolo/query/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def __init__(
2222
self.base = base
2323
self.table = table
2424
self._setup_delegates()
25-
super().__init__()
2625

2726
def _setup_delegates(self):
2827
pass

piccolo/query/methods/exists.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def response_handler(self, response) -> bool:
2424
@property
2525
def querystring(self) -> QueryString:
2626
select = Select(
27-
self.table,
28-
QueryString(f"SELECT * FROM {self.table._meta.tablename}"),
27+
table=self.table,
28+
base=QueryString(f"SELECT * FROM {self.table._meta.tablename}"),
2929
)
3030
select.where_delegate._where = self.where_delegate._where
3131
return QueryString('SELECT EXISTS({}) AS "exists"', select.querystring)

piccolo/query/methods/objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def response_handler(self, response):
7272

7373
@property
7474
def querystring(self) -> QueryString:
75-
select = Select(table=self.table, column_names=[])
75+
select = Select(table=self.table)
7676

7777
for attr in (
7878
"limit_delegate",

piccolo/query/methods/select.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ class Select(Query):
3232
"where_delegate",
3333
)
3434

35+
def __init__(
36+
self,
37+
table: t.Type[Table],
38+
base: QueryString = QueryString(""),
39+
columns: t.List[Column] = [],
40+
):
41+
super().__init__(table=table, base=base)
42+
self.columns(*columns)
43+
3544
def _setup_delegates(self):
3645
self.columns_delegate = ColumnsDelegate()
3746
self.distinct_delegate = DistinctDelegate()

piccolo/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,14 @@ def raw(cls, sql: str, *args: t.Any) -> Raw:
285285
return Raw(table=cls, base=QueryString(sql, *args))
286286

287287
@classmethod
288-
def select(cls) -> Select:
288+
def select(cls, *columns: Column) -> Select:
289289
"""
290290
Get data in the form of a list of dictionaries, with each dictionary
291291
representing a row.
292292
293293
await Band.select().columns(Band.name).run()
294294
"""
295-
return Select(table=cls)
295+
return Select(table=cls, columns=columns)
296296

297297
@classmethod
298298
def delete(cls, force=False) -> Delete:

0 commit comments

Comments
 (0)