Skip to content

Commit dbd0da6

Browse files
committed
cleaning up table docstrings
1 parent 89e022c commit dbd0da6

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

piccolo/table.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def __init__(self, **kwargs):
128128
if unrecognized:
129129
raise ValueError(f"Unrecognized columns - {unrecognized}")
130130

131+
###########################################################################
132+
131133
def save(self) -> t.Union[Insert, Update]:
132134
"""
133135
A proxy to an insert or update query.
@@ -148,7 +150,6 @@ def save(self) -> t.Union[Insert, Update]:
148150
else:
149151
return cls.insert().add(self)
150152

151-
@property
152153
def remove(self) -> Delete:
153154
"""
154155
A proxy to a delete query.
@@ -168,7 +169,7 @@ def get_related(self, column_name: str) -> Objects:
168169
"""
169170
cls = self.__class__
170171

171-
foreign_key = cls.get_column_by_name(column_name)
172+
foreign_key = cls._meta.get_column_by_name(column_name)
172173

173174
if isinstance(foreign_key, ForeignKey):
174175
references: t.Type[
@@ -178,7 +179,7 @@ def get_related(self, column_name: str) -> Objects:
178179
return (
179180
references.objects()
180181
.where(
181-
references.get_column_by_name("id")
182+
references._meta.get_column_by_name("id")
182183
== getattr(self, column_name)
183184
)
184185
.first()
@@ -225,15 +226,7 @@ def __str__(self) -> str:
225226
return self.querystring.__str__()
226227

227228
###########################################################################
228-
229-
@classmethod
230-
def get_column_by_name(cls, column_name: str) -> Column:
231-
columns = [i for i in cls._meta.columns if i._meta.name == column_name]
232-
233-
if len(columns) != 1:
234-
raise ValueError(f"Can't find a column called {column_name}.")
235-
236-
return columns[0]
229+
# Classmethods
237230

238231
@classmethod
239232
def ref(cls, column_name: str) -> Column:
@@ -244,24 +237,20 @@ def ref(cls, column_name: str) -> Column:
244237
"""
245238
local_column_name, reference_column_name = column_name.split(".")
246239

247-
local_column = cls.get_column_by_name(local_column_name)
240+
local_column = cls._meta.get_column_by_name(local_column_name)
248241

249242
if not isinstance(local_column, ForeignKey):
250243
raise ValueError(f"{local_column_name} isn't a ForeignKey")
251244

252-
reference_column = local_column.references.get_column_by_name(
245+
reference_column = local_column.references._meta.get_column_by_name(
253246
reference_column_name
254247
)
255248

256249
_reference_column = copy.deepcopy(reference_column)
257250
_reference_column.name = f"{local_column_name}.{reference_column_name}"
258251
return _reference_column
259252

260-
###########################################################################
261-
# Classmethods
262-
263253
@classmethod
264-
# TODO - needs refactoring into Band.insert.rows(some_table_instance)
265254
def insert(cls, *rows: "Table") -> Insert:
266255
"""
267256
await Band.insert(
@@ -276,14 +265,17 @@ def insert(cls, *rows: "Table") -> Insert:
276265
@classmethod
277266
def raw(cls, sql: str) -> Raw:
278267
"""
268+
Execute raw SQL queries on the underlying engine - use with caution!
269+
279270
await Band.raw('select * from foo')
280271
"""
281272
return Raw(table=cls, base=QueryString(sql))
282273

283274
@classmethod
284275
def select(cls) -> Select:
285276
"""
286-
Get data.
277+
Get data in the form of a list of dictionaries, with each dictionary
278+
representing a row.
287279
288280
await Band.select().columns(Band.name).run()
289281
"""
@@ -292,7 +284,7 @@ def select(cls) -> Select:
292284
@classmethod
293285
def delete(cls) -> Delete:
294286
"""
295-
await Band.delete().where(Band.name == 'CSharps').run()
287+
await Band.delete().where(Band.name == 'Pythonistas').run()
296288
"""
297289
return Delete(table=cls)
298290

@@ -310,7 +302,7 @@ def create_without_columns(cls) -> Raw:
310302
"""
311303
Create the table, but with no columns (useful for migrations).
312304
313-
await Band.create().run()
305+
await Band.create_without_columns().run()
314306
"""
315307
return Raw(
316308
table=cls,
@@ -329,30 +321,56 @@ def drop(cls) -> Drop:
329321
@classmethod
330322
def alter(cls) -> Alter:
331323
"""
324+
Used to modify existing tables and columns.
325+
332326
await Band.alter().rename_column(Band.popularity, 'rating')
333327
"""
334328
return Alter(table=cls)
335329

336330
@classmethod
337331
def objects(cls) -> Objects:
332+
"""
333+
Returns a list of table instances (each representing a row), which you
334+
can modify and then call 'save' on, or can delete by calling 'remove'.
335+
336+
pythonistas = await Band.objects().where(
337+
Band.name == 'Pythonistas'
338+
).first().run()
339+
340+
pythonistas.name = 'Pythonistas Reborn'
341+
342+
await pythonistas.save().run()
343+
344+
# Or to remove it from the database:
345+
await pythonistas.remove()
346+
"""
338347
return Objects(table=cls)
339348

340349
@classmethod
341350
def count(cls) -> Count:
342351
"""
343352
Count the number of matching rows.
353+
354+
await Band.count().where(Band.popularity > 1000).run()
344355
"""
345356
return Count(table=cls)
346357

347358
@classmethod
348359
def exists(cls) -> Exists:
349360
"""
350361
Use it to check if a row exists, not if the table exists.
362+
363+
await Band.exists().where(Band.name == 'Pythonistas').run()
351364
"""
352365
return Exists(table=cls)
353366

354367
@classmethod
355368
def table_exists(cls) -> TableExists:
369+
"""
370+
Check if the table exists in the database.
371+
372+
await Band.table_exists().run()
373+
"""
356374
return TableExists(table=cls)
357375

358376
@classmethod
@@ -365,4 +383,3 @@ def update(cls) -> Update:
365383
).where(Band.name=="Pythonistas")
366384
"""
367385
return Update(table=cls)
368-

tests/table/instance/test_delete.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44

55

66
class TestDelete(TestCase):
7-
87
def setUp(self):
98
Manager.create().run_sync()
109

1110
def tearDown(self):
1211
Manager.drop().run_sync()
1312

1413
def test_delete(self):
15-
manager = Manager(
16-
name='Maz'
17-
)
14+
manager = Manager(name="Maz")
1815

1916
manager.save().run_sync()
20-
manager.remove.run_sync()
17+
manager.remove().run_sync()
2118

2219
# how can I implement 'flat=True'
2320
# Band.select().columns(Band.name).output(as_list=True).run_sync()

0 commit comments

Comments
 (0)