Skip to content

Commit c131baa

Browse files
committed
- Fixed various problems with default parameters after last release
- Fixed `MySQL::dryRun` where PHP7-\Errors caused rollbacks on transactions - Fixed `MySQL::transaction` where tries were not handled correctly
1 parent 2041e7c commit c131baa

File tree

6 files changed

+62
-42
lines changed

6 files changed

+62
-42
lines changed

src/Builder/Insert.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function addOrUpdate($field, $value) {
8787
* @param string ...$_
8888
* @return $this
8989
*/
90-
public function addExpr($str, $_) {
90+
public function addExpr($str, $_ = null) {
9191
if(count(func_get_args()) > 1) {
9292
$this->fields[] = func_get_args();
9393
} else {
@@ -101,7 +101,7 @@ public function addExpr($str, $_) {
101101
* @param string ...$_
102102
* @return $this
103103
*/
104-
public function updateExpr($str, $_) {
104+
public function updateExpr($str, $_ = null) {
105105
if(count(func_get_args()) > 1) {
106106
$this->update[] = func_get_args();
107107
} else {

src/Builder/Traits/HavingBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait HavingBuilder {
1515
* @param mixed ...$_
1616
* @return $this
1717
*/
18-
public function having($expression, $_) {
18+
public function having($expression, $_ = null) {
1919
if($expression instanceof OptionalExpression) {
2020
if($expression->isValid()) {
2121
$this->having[] = [$expression->getExpression(), $expression->getValue()];

src/Builder/Traits/WhereBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait WhereBuilder {
1515
* @param mixed ...$_
1616
* @return $this
1717
*/
18-
public function where($expression, $_) {
18+
public function where($expression, $_ = null) {
1919
if($expression instanceof OptionalExpression) {
2020
if($expression->isValid()) {
2121
$this->where[] = [$expression->getExpression(), $expression->getValue()];

src/Builder/Update.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function setDefault($fieldName) {
6262
* @param string[] ...$_
6363
* @return $this
6464
*/
65-
public function setExpr($expr, $_) {
65+
public function setExpr($expr, $_ = null) {
6666
$this->fields[] = func_get_args();
6767
return $this;
6868
}

src/Database.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
<?php
22
namespace Kir\MySQL;
33

4-
use Kir\MySQL\Builder\Exception;
54
use Kir\MySQL\Database\DatabaseStatement;
5+
use RuntimeException;
66

77
interface Database {
88
/**
99
* @param string $query
10-
* @throws Exception
1110
* @return DatabaseStatement
11+
* @throws RuntimeException
1212
*/
1313
public function query($query);
1414

1515
/**
1616
* @param string $query
17-
* @throws Exception
1817
* @return DatabaseStatement
18+
* @throws RuntimeException
1919
*/
2020
public function prepare($query);
2121

2222
/**
2323
* @param string $query
2424
* @param array $params
2525
* @return int
26+
* @throws RuntimeException
2627
*/
2728
public function exec($query, array $params = array());
2829

@@ -81,31 +82,34 @@ public function delete();
8182

8283
/**
8384
* @return $this
85+
* @throws RuntimeException
8486
*/
8587
public function transactionStart();
8688

8789
/**
8890
* @return $this
91+
* @throws RuntimeException
8992
*/
9093
public function transactionCommit();
9194

9295
/**
9396
* @return $this
97+
* @throws RuntimeException
9498
*/
9599
public function transactionRollback();
96100

97101
/**
98102
* @param int|callable $tries
99103
* @param callable|null $callback
100104
* @return mixed
101-
* @throws \Exception
105+
* @throws RuntimeException
102106
*/
103107
public function transaction($tries = 1, $callback = null);
104108

105109
/**
106110
* @param callable|null $callback
107111
* @return mixed
108-
* @throws \Exception
112+
* @throws RuntimeException
109113
*/
110114
public function dryRun($callback = null);
111115
}

src/Databases/MySQL.php

+48-32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PDO;
55
use PDOException;
6+
use RuntimeException;
67
use UnexpectedValueException;
78
use Kir\MySQL\Builder;
89
use 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

Comments
 (0)