(self, ctx)
| 559 | """Database transaction.""" |
| 560 | |
| 561 | def __init__(self, ctx): |
| 562 | self.ctx = ctx |
| 563 | self.transaction_count = transaction_count = len(ctx.transactions) |
| 564 | |
| 565 | class transaction_engine: |
| 566 | """Transaction Engine used in top level transactions.""" |
| 567 | |
| 568 | def do_transact(self): |
| 569 | ctx.commit(unload=False) |
| 570 | |
| 571 | def do_commit(self): |
| 572 | ctx.commit() |
| 573 | |
| 574 | def do_rollback(self): |
| 575 | ctx.rollback() |
| 576 | |
| 577 | class subtransaction_engine: |
| 578 | """Transaction Engine used in sub transactions.""" |
| 579 | |
| 580 | def query(self, q): |
| 581 | db_cursor = ctx.db.cursor() |
| 582 | ctx.db_execute(db_cursor, SQLQuery(q % transaction_count)) |
| 583 | |
| 584 | def do_transact(self): |
| 585 | self.query("SAVEPOINT webpy_sp_%s") |
| 586 | |
| 587 | def do_commit(self): |
| 588 | self.query("RELEASE SAVEPOINT webpy_sp_%s") |
| 589 | |
| 590 | def do_rollback(self): |
| 591 | self.query("ROLLBACK TO SAVEPOINT webpy_sp_%s") |
| 592 | |
| 593 | class dummy_engine: |
| 594 | """Transaction Engine used instead of subtransaction_engine |
| 595 | when sub transactions are not supported.""" |
| 596 | |
| 597 | do_transact = do_commit = do_rollback = lambda self: None |
| 598 | |
| 599 | if self.transaction_count: |
| 600 | # nested transactions are not supported in some databases |
| 601 | if self.ctx.get("ignore_nested_transactions"): |
| 602 | self.engine = dummy_engine() |
| 603 | else: |
| 604 | self.engine = subtransaction_engine() |
| 605 | else: |
| 606 | self.engine = transaction_engine() |
| 607 | |
| 608 | self.engine.do_transact() |
| 609 | self.ctx.transactions.append(self) |
| 610 | |
| 611 | def __enter__(self): |
| 612 | return self |
nothing calls this directly
no test coverage detected