Am 31.10.2012 15:01, schrieb D'Arcy J.M. Cain:
>> We also have ticket #20 that needs Py 2.5 and was originally planned
>> for 4.2. It would be nice to get that into 4.1 already then.
>
> Go for it.
Ok, so I've started thinking how to make use of context managers in
PyGreSQL. For pgdb, it's pretty clear, we should follow the example of
sqlite and cx_oracle which use them on the connection objection in order
to wrap everything in a transaction. In pg, we can make creative use of
them as we like, and I came up with the following:
First, I added the following convenience methods for transaction
handling to the existing DB wrapper class:
begin, start, commit, end, rollback, savepoint, release
Like in Postgres, start and end are just synonyms for begin and commit.
You can also pass a transaction mode to begin(), and you can pass a name
to rollback() in order to rollback to a given savepoint.
These methods are useful in themselves, and I also use them in the
implementation of the context managers. There are two context managers,
the DB class itself (which just closes the connection when finished) and
the DB.transaction property (which allows wrapping a block of statements
into a transaction). Internally, both use the DB class itself as context
manager, but that's an implementation detail.
You would use the two context managers as follows:
with DB('mydb') as db:
with db.transaction:
db.query('insert into credit values (100)')
db.query('insert into debit values (-100)')
If there is any error in the transaction block, the two insert commands
will be automatically rolled back.
The implementation is already in the trunk. I will also add tests and
documentation, but first want to get feedback.
-- Christoph
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql