Skip to content

Commit

Permalink
Add Exception support for MySQLi
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Nov 13, 2013
1 parent 0f65411 commit 4a5be97
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
56 changes: 55 additions & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
namespace Doctrine\DBAL\Driver\Mysqli;

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\DBALException;

/**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
Expand All @@ -31,7 +33,11 @@ class Driver implements DriverInterface
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{
return new MysqliConnection($params, $username, $password, $driverOptions);
try {
return new MysqliConnection($params, $username, $password, $driverOptions);
} catch (MysqliException $e) {
throw DBALException::driverException($this, $e);
}
}

/**
Expand Down Expand Up @@ -73,6 +79,54 @@ public function getDatabase(\Doctrine\DBAL\Connection $conn)
*/
public function convertExceptionCode(\Exception $exception)
{
if (strpos($exception->getMessage(), 'Table') === 0) {
if (strpos($exception->getMessage(), 'doesn\'t exist') !== false) {
return DBALException::ERROR_UNKNOWN_TABLE;
}

if (strpos($exception->getMessage(), 'already exists') !== false) {
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
}

if (strpos($exception->getMessage(), 'Unknown column') === 0) {
return DBALException::ERROR_BAD_FIELD_NAME;
}

if (strpos($exception->getMessage(), 'Cannot delete or update a parent row: a foreign key constraint fails') !== false) {
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}

if (strpos($exception->getMessage(), 'Duplicate entry') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}

if (strpos($exception->getMessage(), 'Column not found: 1054 Unknown column') !== false) {
return DBALException::ERROR_BAD_FIELD_NAME;
}

if (strpos($exception->getMessage(), 'in field list is ambiguous') !== falsE) {
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}

if (strpos($exception->getMessage(), 'You have an error in your SQL syntax; check the manual') !== false) {
return DBALException::ERROR_SYNTAX;
}

if (strpos($exception->getMessage(), 'Access denied for user') !== false) {
return DBALException::ERROR_ACCESS_DENIED;
}

if (strpos($exception->getMessage(), 'getaddrinfo failed: Name or service not known') !== false) {
return DBALException::ERROR_ACCESS_DENIED;
}

if (strpos($exception->getMessage(), ' cannot be null')) {
return DBALException::ERROR_NOT_NULL;
}

var_dump($exception->geTcode());
var_dump($exception->getMEssage());

This comment has been minimized.

Copy link
@kimhemsoe

kimhemsoe Nov 13, 2013

Member

@beberlei Think forgot to remove a few var_dump()'s here

This comment has been minimized.

Copy link
@deeky666

deeky666 Nov 13, 2013

Member

Already removed in commit 1dbd6d3

return 0;
}
}
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ public function __construct(array $params, $username, $password, array $driverOp
$socket = isset($params['unix_socket']) ? $params['unix_socket'] : ini_get('mysqli.default_socket');

$this->_conn = mysqli_init();

$previousHandler = set_error_handler(function () {
});

if ( ! $this->_conn->real_connect($params['host'], $username, $password, $params['dbname'], $port, $socket)) {
set_error_handler($previousHandler);

throw new MysqliException($this->_conn->connect_error, $this->_conn->connect_errno);
}

set_error_handler($previousHandler);

if (isset($params['charset'])) {
$this->_conn->set_charset($params['charset']);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

namespace Doctrine\DBAL\Driver\Mysqli;

use Doctrine\DBAL\DBALException;

/**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/
class MysqliException extends \Exception
class MysqliException extends DBALException
{
}
8 changes: 6 additions & 2 deletions tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,19 @@ public function testFetchAllWithTypes()

$row = array_change_key_case($row, \CASE_LOWER);
$this->assertEquals(1, $row['test_int']);
$this->assertEquals($datetimeString, $row['test_datetime']);
$this->assertEquals($datetimeString, $row['test_datetime']);
}

/**
* @group DBAL-209
* @expectedException \Doctrine\DBAL\DBALException
*/
public function testFetchAllWithMissingTypes()
{
if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver) {
$this->markTestSkipped('mysqli actually supports this');
}

$datetimeString = '2010-01-01 10:10:10';
$datetime = new \DateTime($datetimeString);
$sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function setUp()
{
parent::setUp();

$supportExceptions = array('pdo_sqlite', 'pdo_mysql', 'pdo_pgsql');
$supportExceptions = array('pdo_sqlite', 'pdo_mysql', 'pdo_pgsql', 'mysqli');
$params = $this->_conn->getParams();

if (!in_array($params['driver'], $supportExceptions)) {
Expand Down

0 comments on commit 4a5be97

Please sign in to comment.