| 1 |
<?php
|
| 2 |
class pressflow_transaction {
|
| 3 |
private static $layers = 0;
|
| 4 |
private static $allow_commit = TRUE;
|
| 5 |
|
| 6 |
function __construct()
|
| 7 |
{
|
| 8 |
if (self::$layers == 0)
|
| 9 |
db_query('BEGIN');
|
| 10 |
self::$layers++;
|
| 11 |
}
|
| 12 |
|
| 13 |
function __destruct()
|
| 14 |
{
|
| 15 |
self::$layers--;
|
| 16 |
if (self::$layers == 0) {
|
| 17 |
if (self::$allow_commit) {
|
| 18 |
db_query('COMMIT');
|
| 19 |
} else {
|
| 20 |
db_query('ROLLBACK');
|
| 21 |
}
|
| 22 |
// Reset the ROLLBACK propagator
|
| 23 |
self::$allow_commit = TRUE;
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
public function rollback_if_false($var)
|
| 28 |
{
|
| 29 |
if ($var === FALSE) {
|
| 30 |
$this->rollback();
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
public function rollback()
|
| 35 |
{
|
| 36 |
self::$allow_commit = FALSE;
|
| 37 |
}
|
| 38 |
|
| 39 |
static public function will_rollback()
|
| 40 |
{
|
| 41 |
return !self::$allow_commit;
|
| 42 |
}
|
| 43 |
}
|