Skip to content

Commit df47473

Browse files
committed
Rename rows to all; #16
1 parent 602cd7d commit df47473

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

postgres.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
{'bar': 'baz'}
3636
3737
38-
Use :py:meth:`~postgres.Postgres.rows` to fetch all results:
38+
Use :py:meth:`~postgres.Postgres.all` to fetch all results:
3939
40-
>>> db.rows("SELECT * FROM foo ORDER BY bar")
40+
>>> db.all("SELECT * FROM foo ORDER BY bar")
4141
[{'bar': 'baz'}, {'bar': 'buz'}]
4242
4343
@@ -64,7 +64,7 @@
6464
6565
Eighty percent of your database usage should be covered by the simple
6666
:py:meth:`~postgres.Postgres.run`, :py:meth:`~postgres.Postgres.one`,
67-
:py:meth:`~postgres.Postgres.rows` API introduced above. For the other 20%,
67+
:py:meth:`~postgres.Postgres.all` API introduced above. For the other 20%,
6868
:py:mod:`postgres` provides context managers for working at increasingly lower
6969
levels of abstraction. The lowest level of abstraction in :py:mod:`postgres` is
7070
a :py:mod:`psycopg2` `connection pool
@@ -100,10 +100,10 @@
100100
101101
>>> with db.get_transaction() as txn:
102102
... txn.execute("INSERT INTO foo VALUES ('blam')")
103-
... db.rows("SELECT * FROM foo ORDER BY bar")
103+
... db.all("SELECT * FROM foo ORDER BY bar")
104104
...
105105
[{'bar': 'baz'}, {'bar': 'buz'}]
106-
>>> db.rows("SELECT * FROM foo ORDER BY bar")
106+
>>> db.all("SELECT * FROM foo ORDER BY bar")
107107
[{'bar': 'baz'}, {'bar': 'blam'}, {'bar': 'buz'}]
108108
109109
The :py:func:`~postgres.Postgres.get_transaction` manager gives you a cursor
@@ -237,7 +237,7 @@ class Postgres(object):
237237
:py:class:`NamedTupleCursor`.
238238
239239
The names in our simple API, :py:meth:`~postgres.Postgres.run`,
240-
:py:meth:`~postgres.Postgres.one`, and :py:meth:`~postgres.Postgres.rows`,
240+
:py:meth:`~postgres.Postgres.one`, and :py:meth:`~postgres.Postgres.all`,
241241
were chosen to be short and memorable, and to not conflict with the DB-API
242242
2.0 :py:meth:`execute`, :py:meth:`fetchone`, and :py:meth:`fetchall`
243243
methods, which have slightly different semantics (under DB-API 2.0 you call
@@ -249,7 +249,7 @@ class Postgres(object):
249249
Note that when working inside a block under one of the context managers,
250250
you're using DB-API 2.0 (:py:meth:`execute` + :py:meth:`fetch*`), not our
251251
simple API (:py:meth:`~postgres.Postgres.run` /
252-
:py:meth:`~postgres.Postgres.one` / :py:meth:`~postgres.Postgres.rows`).
252+
:py:meth:`~postgres.Postgres.one` / :py:meth:`~postgres.Postgres.all`).
253253
254254
.. _this ticket: https://github.com/gittip/postgres.py/issues/16
255255
@@ -332,15 +332,15 @@ def one(self, sql, parameters=None, strict=None):
332332

333333
return cursor.fetchone()
334334

335-
def rows(self, sql, parameters=None):
335+
def all(self, sql, parameters=None):
336336
"""Execute a query and return all resulting rows.
337337
338338
:param unicode sql: the SQL statement to execute
339339
:param parameters: the bind parameters for the SQL statement
340340
:type parameters: dict or tuple
341341
:returns: :py:class:`list` of rows
342342
343-
>>> for row in db.rows("SELECT bar FROM foo"):
343+
>>> for row in db.all("SELECT bar FROM foo"):
344344
... print(row["bar"])
345345
...
346346
baz
@@ -351,13 +351,21 @@ def rows(self, sql, parameters=None):
351351
cursor.execute(sql, parameters)
352352
return cursor.fetchall()
353353

354+
def rows(self, *a, **kw):
355+
356+
# This is for backwards compatibility, see #16. It is stubbed instead
357+
# of aliased to avoid showing up in our docs via sphinx autodoc.
358+
359+
return self.all(*a, **kw)
360+
361+
354362
def get_cursor(self, *a, **kw):
355363
"""Return a :py:class:`~postgres.CursorContextManager` that uses our
356364
connection pool.
357365
358366
This is what :py:meth:`~postgres.Postgres.run`,
359367
:py:meth:`~postgres.Postgres.one`, and
360-
:py:meth:`~postgres.Postgres.rows` use under the hood. You might
368+
:py:meth:`~postgres.Postgres.all` use under the hood. You might
361369
use it if you want to access `cursor attributes
362370
<http://initd.org/psycopg/docs/cursor.html>`_, for example.
363371

tests.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class TestRun(WithSchema):
4242

4343
def test_run_runs(self):
4444
self.db.run("CREATE TABLE foo (bar text)")
45-
actual = self.db.rows("SELECT tablename FROM pg_tables "
46-
"WHERE schemaname='public'")
45+
actual = self.db.all("SELECT tablename FROM pg_tables "
46+
"WHERE schemaname='public'")
4747
assert actual == [{"tablename": "foo"}]
4848

4949
def test_run_inserts(self):
@@ -168,22 +168,22 @@ def test_one_raises_TooMany(self):
168168
self.assertRaises(TooMany, self.db.one, "SELECT * FROM foo")
169169

170170

171-
# db.rows
172-
# =======
171+
# db.all
172+
# ======
173173

174174
class TestRows(WithData):
175175

176176
def test_rows_fetches_all_rows(self):
177-
actual = self.db.rows("SELECT * FROM foo ORDER BY bar")
177+
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
178178
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
179179

180180
def test_bind_parameters_as_dict_work(self):
181181
params = {"bar": "baz"}
182-
actual = self.db.rows("SELECT * FROM foo WHERE bar=%(bar)s", params)
182+
actual = self.db.all("SELECT * FROM foo WHERE bar=%(bar)s", params)
183183
assert actual == [{"bar": "baz"}]
184184

185185
def test_bind_parameters_as_tuple_work(self):
186-
actual = self.db.rows("SELECT * FROM foo WHERE bar=%s", ("baz",))
186+
actual = self.db.all("SELECT * FROM foo WHERE bar=%s", ("baz",))
187187
assert actual == [{"bar": "baz"}]
188188

189189

@@ -227,14 +227,14 @@ def test_transaction_is_isolated(self):
227227
with self.db.get_transaction() as txn:
228228
txn.execute("INSERT INTO foo VALUES ('blam')")
229229
txn.execute("SELECT * FROM foo ORDER BY bar")
230-
actual = self.db.rows("SELECT * FROM foo ORDER BY bar")
230+
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
231231
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
232232

233233
def test_transaction_commits_on_success(self):
234234
with self.db.get_transaction() as txn:
235235
txn.execute("INSERT INTO foo VALUES ('blam')")
236236
txn.execute("SELECT * FROM foo ORDER BY bar")
237-
actual = self.db.rows("SELECT * FROM foo ORDER BY bar")
237+
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
238238
assert actual == [{"bar": "baz"}, {"bar": "blam"}, {"bar": "buz"}]
239239

240240
def test_transaction_rolls_back_on_failure(self):
@@ -246,7 +246,7 @@ class Heck(Exception): pass
246246
raise Heck
247247
except Heck:
248248
pass
249-
actual = self.db.rows("SELECT * FROM foo ORDER BY bar")
249+
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
250250
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
251251

252252

@@ -279,11 +279,11 @@ def setUp(self): # override
279279
def test_NamedDictCursor_results_in_namedtuples(self):
280280
Record = namedtuple("Record", ["bar"])
281281
expected = [Record(bar="baz"), Record(bar="buz")]
282-
actual = self.db.rows("SELECT * FROM foo ORDER BY bar")
282+
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
283283
assert actual == expected
284284

285285
def test_NamedDictCursor_results_in_namedtuples(self):
286286
Record = namedtuple("Record", ["bar"])
287287
expected = [Record(bar="baz"), Record(bar="buz")]
288-
actual = self.db.rows("SELECT * FROM foo ORDER BY bar")
288+
actual = self.db.all("SELECT * FROM foo ORDER BY bar")
289289
assert actual == expected

0 commit comments

Comments
 (0)