3
3
4
4
use PDO ;
5
5
use PDOException ;
6
+ use RuntimeException ;
6
7
use UnexpectedValueException ;
7
8
use Kir \MySQL \Builder ;
8
9
use Kir \MySQL \Builder \Exception ;
@@ -277,6 +278,7 @@ public function transactionRollback() {
277
278
* @param callable|null $callback
278
279
* @return mixed
279
280
* @throws \Exception
281
+ * @throws \Error
280
282
* @throws null
281
283
*/
282
284
public function dryRun ($ callback = null ) {
@@ -286,78 +288,92 @@ public function dryRun($callback = null) {
286
288
$ this ->transactionStart ();
287
289
try {
288
290
$ result = call_user_func ($ callback , $ this );
291
+ $ this ->transactionRollback ();
289
292
} 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 ;
295
298
}
296
299
} else {
297
300
$ uniqueId = $ this ->genUniqueId ();
298
301
$ this ->exec ("SAVEPOINT {$ uniqueId }" );
299
302
try {
300
303
$ result = call_user_func ($ callback , $ this );
304
+ $ this ->exec ("ROLLBACK TO {$ uniqueId }" );
301
305
} 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 ;
307
311
}
308
312
}
309
313
return $ result ;
310
314
}
311
-
315
+
312
316
/**
313
317
* @param int|callable $tries
314
318
* @param callable|null $callback
315
319
* @return mixed
316
320
* @throws \Exception
317
- * @throws null
321
+ * @throws \Error
318
322
*/
319
323
public function transaction ($ tries = 1 , $ callback = null ) {
320
324
if (is_callable ($ tries )) {
321
325
$ callback = $ tries ;
322
326
$ tries = 1 ;
323
327
} elseif (!is_callable ($ callback )) {
324
- throw new \ Exception ('$callback must be a callable ' );
328
+ throw new RuntimeException ('$callback must be a callable ' );
325
329
}
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 ();
329
335
try {
330
- $ this ->transactionStart ();
331
336
$ result = call_user_func ($ callback , $ this );
337
+ $ exception = null ;
332
338
$ this ->transactionCommit ();
333
- return $ result ;
334
339
} catch (\Exception $ e ) {
335
340
$ this ->transactionRollback ();
341
+ $ exception = $ e ;
342
+ } catch (\Error $ e ) {
343
+ $ this ->transactionRollback ();
344
+ $ exception = $ e ;
336
345
}
337
- }
338
- } else {
339
- $ uniqueId = $ this ->genUniqueId ();
340
- try {
346
+ } else {
347
+ $ uniqueId = $ this ->genUniqueId ();
341
348
$ 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
+ }
347
360
}
348
361
}
349
- throw $ e ;
362
+ if ($ exception !== null ) {
363
+ throw $ exception ;
364
+ }
365
+ return $ result ;
350
366
}
351
367
352
368
/**
353
369
* @param callable $fn
354
370
* @return $this
355
- * @throws \Exception
371
+ * @throws RuntimeException
356
372
*/
357
373
private function transactionEnd ($ fn ) {
358
374
$ this ->transactionLevel --;
359
375
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 " );
361
377
}
362
378
if ((int ) $ this ->transactionLevel === 0 ) {
363
379
if ($ this ->outerTransaction ) {
@@ -373,12 +389,12 @@ private function transactionEnd($fn) {
373
389
* @param string $query
374
390
* @param callable $fn
375
391
* @return QueryStatement
376
- * @throws Exception
392
+ * @throws RuntimeException
377
393
*/
378
394
private function buildQueryStatement ($ query , $ fn ) {
379
395
$ stmt = call_user_func ($ fn , $ query );
380
396
if (!$ stmt ) {
381
- throw new Exception ("Could not execute statement: \n{$ query }" );
397
+ throw new RuntimeException ("Could not execute statement: \n{$ query }" );
382
398
}
383
399
$ stmtWrapper = new QueryStatement ($ stmt , $ query , $ this ->exceptionInterpreter , $ this ->queryLoggers );
384
400
return $ stmtWrapper ;
0 commit comments