postgres.py icon indicating copy to clipboard operation
postgres.py copied to clipboard

cursor.get_cursor should return self

Open chadwhitacre opened this issue 10 years ago • 2 comments

That will make it easier to interchange db and cursor objects. Cropped up at https://github.com/gratipay/gratipay.com/issues/2729#issuecomment-54536544. Similar to #39.

chadwhitacre avatar Sep 05 '14 15:09 chadwhitacre

It shouldn't return itself, if it does then the transaction will be committed at the end of the inner with block, instead of the outer one. In gratipay/gratipay.com#2718 I use a context that doesn't do anything when it's entered or exited.

Changaco avatar Sep 05 '14 15:09 Changaco

I don't think this is a good idea, because it makes it easy to introduce subtle issues. Requiring explicit changes to support both DB and cursor objects in a function seems less likely to result in bugs.

A method that explicitly supports DB and cursor objects can look like this:

def foo(self, cursor=None):
    (self.db or cursor).run("...")

or that:

def foo(self, cursor=None):
    with self.db.get_cursor(cursor=cursor) as c:
        ...

This second pattern currently isn't supported by postgres.py, it's implemented in Liberapay by subclassing Postgres.

Changaco avatar Sep 26 '19 07:09 Changaco