33
44use PDO ;
55use PDOException ;
6+ use RuntimeException ;
67use UnexpectedValueException ;
78use Kir \MySQL \Builder ;
89use Kir \MySQL \Builder \Exception ;
@@ -277,6 +278,7 @@ public function transactionRollback() {
277278 * @param callable|null $callback
278279 * @return mixed
279280 * @throws \Exception
281+ * @throws \Error
280282 * @throws null
281283 */
282284 public function dryRun ($ callback = null ) {
@@ -286,78 +288,92 @@ public function dryRun($callback = null) {
286288 $ this ->transactionStart ();
287289 try {
288290 $ result = call_user_func ($ callback , $ this );
291+ $ this ->transactionRollback ();
289292 } catch (\Exception $ e ) {
290- $ exception = $ e ;
291- }
292- $ this -> transactionRollback ();
293- if ( $ exception !== null ) {
294- throw $ exception ;
293+ $ this -> transactionRollback () ;
294+ throw $ e ;
295+ } catch ( \ Error $ e ) {
296+ $ this -> transactionRollback ();
297+ throw $ e ;
295298 }
296299 } else {
297300 $ uniqueId = $ this ->genUniqueId ();
298301 $ this ->exec ("SAVEPOINT {$ uniqueId }" );
299302 try {
300303 $ result = call_user_func ($ callback , $ this );
304+ $ this ->exec ("ROLLBACK TO {$ uniqueId }" );
301305 } catch (\Exception $ e ) {
302- $ exception = $ e ;
303- }
304- $ this -> exec ( " ROLLBACK TO { $ uniqueId }" );
305- if ( $ exception !== null ) {
306- throw $ exception ;
306+ $ this -> exec ( " ROLLBACK TO { $ uniqueId }" ) ;
307+ throw $ e ;
308+ } catch ( \ Error $ e ) {
309+ $ this -> exec ( " ROLLBACK TO { $ uniqueId }" );
310+ throw $ e ;
307311 }
308312 }
309313 return $ result ;
310314 }
311-
315+
312316 /**
313317 * @param int|callable $tries
314318 * @param callable|null $callback
315319 * @return mixed
316320 * @throws \Exception
317- * @throws null
321+ * @throws \Error
318322 */
319323 public function transaction ($ tries = 1 , $ callback = null ) {
320324 if (is_callable ($ tries )) {
321325 $ callback = $ tries ;
322326 $ tries = 1 ;
323327 } elseif (!is_callable ($ callback )) {
324- throw new \ Exception ('$callback must be a callable ' );
328+ throw new RuntimeException ('$callback must be a callable ' );
325329 }
326- $ e = null ;
327- if (!$ this ->pdo ->inTransaction ()) {
328- for (; $ tries --;) {
330+ $ result = null ;
331+ $ exception = null ;
332+ for (; $ tries --;) {
333+ if (!$ this ->pdo ->inTransaction ()) {
334+ $ this ->transactionStart ();
329335 try {
330- $ this ->transactionStart ();
331336 $ result = call_user_func ($ callback , $ this );
337+ $ exception = null ;
332338 $ this ->transactionCommit ();
333- return $ result ;
334339 } catch (\Exception $ e ) {
335340 $ this ->transactionRollback ();
341+ $ exception = $ e ;
342+ } catch (\Error $ e ) {
343+ $ this ->transactionRollback ();
344+ $ exception = $ e ;
336345 }
337- }
338- } else {
339- $ uniqueId = $ this ->genUniqueId ();
340- try {
346+ } else {
347+ $ uniqueId = $ this ->genUniqueId ();
341348 $ this ->exec ("SAVEPOINT {$ uniqueId }" );
342- $ result = call_user_func ($ callback , $ this );
343- $ this ->exec ("RELEASE SAVEPOINT {$ uniqueId }" );
344- return $ result ;
345- } catch (\Exception $ e ) {
346- $ this ->exec ("ROLLBACK TO {$ uniqueId }" );
349+ try {
350+ $ result = call_user_func ($ callback , $ this );
351+ $ exception = null ;
352+ $ this ->exec ("RELEASE SAVEPOINT {$ uniqueId }" );
353+ } catch (\Exception $ e ) {
354+ $ this ->exec ("ROLLBACK TO {$ uniqueId }" );
355+ $ exception = $ e ;
356+ } catch (\Error $ e ) {
357+ $ this ->exec ("ROLLBACK TO {$ uniqueId }" );
358+ $ exception = $ e ;
359+ }
347360 }
348361 }
349- throw $ e ;
362+ if ($ exception !== null ) {
363+ throw $ exception ;
364+ }
365+ return $ result ;
350366 }
351367
352368 /**
353369 * @param callable $fn
354370 * @return $this
355- * @throws \Exception
371+ * @throws RuntimeException
356372 */
357373 private function transactionEnd ($ fn ) {
358374 $ this ->transactionLevel --;
359375 if ($ this ->transactionLevel < 0 ) {
360- throw new \ Exception ("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction " );
376+ throw new RuntimeException ("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction " );
361377 }
362378 if ((int ) $ this ->transactionLevel === 0 ) {
363379 if ($ this ->outerTransaction ) {
@@ -373,12 +389,12 @@ private function transactionEnd($fn) {
373389 * @param string $query
374390 * @param callable $fn
375391 * @return QueryStatement
376- * @throws Exception
392+ * @throws RuntimeException
377393 */
378394 private function buildQueryStatement ($ query , $ fn ) {
379395 $ stmt = call_user_func ($ fn , $ query );
380396 if (!$ stmt ) {
381- throw new Exception ("Could not execute statement: \n{$ query }" );
397+ throw new RuntimeException ("Could not execute statement: \n{$ query }" );
382398 }
383399 $ stmtWrapper = new QueryStatement ($ stmt , $ query , $ this ->exceptionInterpreter , $ this ->queryLoggers );
384400 return $ stmtWrapper ;
0 commit comments