Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cursor.get_cursor should return self #40

Open
chadwhitacre opened this issue Sep 5, 2014 · 2 comments
Open

cursor.get_cursor should return self #40

chadwhitacre opened this issue Sep 5, 2014 · 2 comments

Comments

@chadwhitacre
Copy link
Collaborator

That will make it easier to interchange db and cursor objects. Cropped up at gratipay/gratipay.com#2729 (comment). Similar to #39.

chadwhitacre added a commit to gratipay/gratipay.com that referenced this issue Sep 5, 2014
@Changaco
Copy link
Member

Changaco commented Sep 5, 2014

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
Copy link
Member

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 added a commit that referenced this issue Oct 1, 2019
This commit adds a `cursor` argument to the `get_cursor` method. The rationale for this is in <#40 (comment)>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants