Skip to content

Commit d1eb7aa

Browse files
committed
Throw more concrete exceptiontypes. E.g. for dead-locks
1 parent 3ecdfbc commit d1eb7aa

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

src/Databases/MySQL.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22
namespace Kir\MySQL\Databases;
33

4+
use PDO;
5+
use PDOException;
6+
use UnexpectedValueException;
7+
use Kir\MySQL\Builder\RunnableSelect;
48
use Kir\MySQL\Builder;
59
use Kir\MySQL\Builder\Exception;
610
use Kir\MySQL\Builder\QueryStatement;
711
use Kir\MySQL\Database;
12+
use Kir\MySQL\Databases\MySQL\MySQLExceptionInterpreter;
813
use Kir\MySQL\QueryLogger\QueryLoggers;
914
use Kir\MySQL\Tools\AliasRegistry;
10-
use PDO;
11-
use UnexpectedValueException;
12-
use Kir\MySQL\Builder\RunnableSelect;
1315

1416
/**
1517
*/
@@ -32,6 +34,7 @@ public function __construct(PDO $pdo) {
3234
$this->pdo = $pdo;
3335
$this->aliasRegistry = new AliasRegistry();
3436
$this->queryLoggers = new QueryLoggers();
37+
$this->exceptionInterpreter = new MySQLExceptionInterpreter($pdo);
3538
}
3639

3740
/**
@@ -84,7 +87,11 @@ public function prepare($query) {
8487
public function exec($query, array $params = array()) {
8588
$stmt = $this->pdo->prepare($query);
8689
$timer = microtime(true);
87-
$stmt->execute($params);
90+
try {
91+
$stmt->execute($params);
92+
} catch (PDOException $e) {
93+
$this->exceptionInterpreter->throwMoreConcreteException($e);
94+
}
8895
$this->queryLoggers->log($query, microtime(true) - $timer);
8996
$result = $stmt->rowCount();
9097
$stmt->closeCursor();
@@ -216,12 +223,12 @@ public function transactionStart() {
216223

217224
/**
218225
* @return $this
219-
* @throws Exception
226+
* @throws \Exception
220227
*/
221228
public function transactionCommit() {
222229
$this->transactionLevel--;
223230
if($this->transactionLevel < 0) {
224-
throw new Exception("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction");
231+
throw new \Exception("Transaction-Nesting-Problem: Trying to invoke commit on a already closed transaction");
225232
}
226233
if((int) $this->transactionLevel === 0) {
227234
$this->pdo->commit();
@@ -231,12 +238,12 @@ public function transactionCommit() {
231238

232239
/**
233240
* @return $this
234-
* @throws Exception
241+
* @throws \Exception
235242
*/
236243
public function transactionRollback() {
237244
$this->transactionLevel--;
238245
if($this->transactionLevel < 0) {
239-
throw new Exception("Transaction-Nesting-Problem: Trying to invoke rollback on a already closed transaction");
246+
throw new \Exception("Transaction-Nesting-Problem: Trying to invoke rollback on a already closed transaction");
240247
}
241248
if((int) $this->transactionLevel === 0) {
242249
$this->pdo->rollBack();
@@ -249,6 +256,7 @@ public function transactionRollback() {
249256
* @param callable|null $callback
250257
* @return mixed
251258
* @throws \Exception
259+
* @throws null
252260
*/
253261
public function transaction($tries = 1, $callback = null) {
254262
if(is_callable($tries)) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Kir\MySQL\Databases\MySQL;
3+
4+
use Kir\MySQL\Exceptions\SqlDeadLockException;
5+
use PDO;
6+
use PDOException;
7+
8+
class MySQLExceptionInterpreter {
9+
/** @var PDO */
10+
private $pdo;
11+
12+
/**
13+
* @param PDO $pdo
14+
*/
15+
public function __construct(PDO $pdo) {
16+
$this->pdo = $pdo;
17+
}
18+
19+
/**
20+
* @param PDOException $exception
21+
* @throw PDOException
22+
*/
23+
public function throwMoreConcreteException(PDOException $exception) {
24+
$errorInfo = $this->pdo->errorInfo();
25+
$code = $errorInfo[1];
26+
$message = $errorInfo[2];
27+
if($code === 1213) {
28+
throw new SqlDeadLockException($message, $code, $exception);
29+
}
30+
throw $exception;
31+
}
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace Kir\MySQL\Exceptions;
3+
4+
class SqlDeadLockException extends SqlException {
5+
}

src/Exceptions/SqlException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Kir\MySQL\Exceptions;
3+
4+
use PDOException;
5+
6+
class SqlException extends PDOException {
7+
}

0 commit comments

Comments
 (0)