X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fxdb.php;h=c353f54349021e1ee5b6547655d22e21a06c578a;hb=f23d33e1b114e9032b4b966baebdeb14b4215313;hp=28010c9fd43c4ca63c1edba7e389ce253b7ce66d;hpb=e49b22c0de68ab13d41c9e2160584fe8964cff46;p=platal.git diff --git a/classes/xdb.php b/classes/xdb.php index 28010c9..c353f54 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -212,6 +212,35 @@ class XDB self::rawExecute('SET AUTOCOMMIT = 1'); } + public static function runTransactionV($callback, array $args) + { + self::startTransaction(); + try { + if (call_user_func_array($callback, $args)) { + self::commit(); + return true; + } else { + self::rollback(); + return false; + } + } catch (Exception $e) { + self::rollback(); + throw $e; + } + } + + /** This function takes a callback followed by the arguments to be passed to the callback + * as arguments. It starts a transaction and execute the callback. If the callback fails + * (return false or raise an exception), the transaction is rollbacked, if the callback + * succeeds (return true), the transaction is committed. + */ + public static function runTransaction() + { + $args = func_get_args(); + $cb = array_shift($args); + self::runTransactionV($cb, $args); + } + public static function iterator() { return new XDBIterator(self::prepare(func_get_args()));