Skip to content

Commit 89e2c19

Browse files
committed
refactored to improve code completion
1 parent d9c4cd8 commit 89e2c19

34 files changed

+450
-364
lines changed

docs/src/piccolo/design-decisions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ At any time you can access the __str__ method of a query, to see the underlying
2424

2525
.. code-block:: python
2626
27-
query = Band.select.columns(Band.name).where(Band.popularity >= 100)
27+
query = Band.select().columns(Band.name).where(Band.popularity >= 100)
2828
2929
print(query)
3030
'SELECT name from band where popularity > 100'

docs/src/piccolo/transactions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ Usage
1212
.. code-block:: python
1313
1414
transaction = Band.Meta.db.transaction()
15-
transaction.add(Manager.create)
16-
transaction.add(Concert.create)
15+
transaction.add(Manager.create())
16+
transaction.add(Concert.create())
1717
await transaction.run()

piccolo/columns/column_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ForeignKey(Integer):
9292
9393
Can also use it to perform joins:
9494
95-
Band.select.columns(Band.manager.name)
95+
Band.select().columns(Band.manager.name)
9696
9797
To get a referenced row as an object:
9898

piccolo/commands/playground.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ def populate():
5656
"""
5757
for _table in reversed(TABLES):
5858
try:
59-
_table.drop.run_sync()
59+
_table.drop().run_sync()
6060
except Exception as e:
6161
print(e)
6262

6363
for _table in TABLES:
6464
try:
65-
_table.create.run_sync()
65+
_table.create().run_sync()
6666
except Exception as e:
6767
print(e)
6868

piccolo/engine/postgres.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Transaction():
1616
Usage:
1717
1818
transaction = engine.Transaction()
19-
transaction.add(Foo.create)
19+
transaction.add(Foo.create())
2020
transaction.run_sync()
2121
"""
2222

piccolo/extensions/user.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BaseUser(Table):
2222
class Meta():
2323
# This is required because 'user' is a reserved keyword in SQL, so
2424
# using it as a tablename causes issues.
25-
tablename = 'a_user'
25+
tablename = 'piccolo_user'
2626

2727
def __init__(self, **kwargs):
2828
"""
@@ -70,10 +70,11 @@ async def login(cls, username: str, password: str) -> t.Optional[int]:
7070
"""
7171
Returns the user_id if a match is found.
7272
"""
73+
query = cls.select().columns(cls.id, cls.password).where(
74+
(cls.username == username)
75+
).first()
7376
try:
74-
response = await cls.select.columns(cls.id, cls.password).where(
75-
(cls.username == username)
76-
).first.run()
77+
response = await query.run()
7778
except ValueError:
7879
# No match found
7980
return None

piccolo/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def _create_migration_table() -> bool:
6262
Creates the migration table in the database. Returns True/False depending
6363
on whether it was created or not.
6464
"""
65-
if not Migration.table_exists.run_sync():
66-
Migration.create.run_sync()
65+
if not Migration.table_exists().run_sync():
66+
Migration.create().run_sync()
6767
return True
6868
return False
6969

@@ -74,7 +74,7 @@ def _get_migrations_which_ran() -> t.List[str]:
7474
database.
7575
"""
7676
return [
77-
i['name'] for i in Migration.select.columns(Migration.name).run_sync()
77+
i['name'] for i in Migration.select().columns(Migration.name).run_sync()
7878
]
7979

8080

@@ -195,7 +195,7 @@ def backwards(migration_name: str):
195195
MIGRATION_MODULES[s].backwards() # type: ignore
196196
)
197197

198-
Migration.delete.where(
198+
Migration.delete().where(
199199
Migration.name == s
200200
).run_sync()
201201
else:

piccolo/query/base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ def __init__(
2222
) -> None:
2323
self.base = base
2424
self.table = table
25+
self.setup_delegates()
2526
super().__init__()
2627

28+
def setup_delegates(self):
29+
pass
30+
2731
@property
2832
def engine_type(self) -> str:
2933
return self.table.Meta.db.engine_type
@@ -55,18 +59,18 @@ async def run(self, as_dict=True, in_pool=False):
5559
# Might try and merge them.
5660
raw = self.response_handler(raw)
5761

58-
output = getattr(self, '_output', None)
62+
output = getattr(self, 'output_delegate', None)
5963

6064
if output:
61-
if output.as_objects:
65+
if output._output.as_objects:
6266
# When using .first() we get a single row, not a list
6367
# of rows.
6468
if type(raw) is list:
6569
raw = [self.table(**columns) for columns in raw]
6670
else:
6771
raw = self.table(**raw)
6872
elif type(raw) is list:
69-
if output.as_list:
73+
if output._output.as_list:
7074
if len(raw[0].keys()) != 1:
7175
raise ValueError(
7276
'Each row returned more than one value'
@@ -75,7 +79,7 @@ async def run(self, as_dict=True, in_pool=False):
7579
raw = list(
7680
itertools.chain(*[j.values() for j in raw])
7781
)
78-
if output.as_json:
82+
if output._output.as_json:
7983
raw = json.dumps(raw)
8084

8185
return raw

piccolo/query/methods/alter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@dataclasses.dataclass
1111
class Rename():
1212
"""
13-
Band.alter.rename(Band.popularity, ‘rating’)
13+
Band.alter().rename(Band.popularity, ‘rating’)
1414
"""
1515
column: Column
1616
new_name: str
@@ -28,7 +28,7 @@ def __str__(self) -> str:
2828
@dataclasses.dataclass
2929
class Drop():
3030
"""
31-
Band.alter.drop('popularity')
31+
Band.alter().drop('popularity')
3232
"""
3333
column: Column
3434

@@ -45,7 +45,7 @@ def __str__(self) -> str:
4545
@dataclasses.dataclass
4646
class Add():
4747
"""
48-
Band.alter.add(‘members’, Integer())
48+
Band.alter().add(‘members’, Integer())
4949
"""
5050
name: str
5151
column: Column

piccolo/query/methods/delete.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
from __future__ import annotations
22

3+
from piccolo.custom_types import Combinable
34
from piccolo.query.base import Query
4-
from piccolo.query.mixins import WhereMixin
5+
from piccolo.query.mixins import WhereDelegate
56
from piccolo.querystring import QueryString
67

78

8-
class Delete(Query, WhereMixin):
9+
class Delete(Query):
10+
11+
def setup_delegates(self):
12+
self.where_delegate = WhereDelegate()
13+
14+
def where(self, where: Combinable) -> Delete:
15+
self.where_delegate.where(where)
16+
return self
917

1018
@property
1119
def querystring(self) -> QueryString:
1220
query = f'DELETE FROM {self.table.Meta.tablename}'
13-
if self._where:
21+
if self.where_delegate._where:
1422
query += ' WHERE {}'
1523
return QueryString(
1624
query,
17-
self._where.querystring
25+
self.where_delegate._where.querystring
1826
)
1927
else:
2028
return QueryString(query)
2129

2230
def __str__(self) -> str:
2331
query = f'DELETE FROM {self.table.Meta.tablename}'
24-
if self._where:
25-
query += f' WHERE {self._where.__str__()}'
32+
if self.where_delegate._where:
33+
query += f' WHERE {self.where_delegate._where.__str__()}'
2634
return query

0 commit comments

Comments
 (0)