Commit 4a5be979 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Add Exception support for MySQLi

parent 0f654115
......@@ -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>
......@@ -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);
}
}
/**
......@@ -73,6 +79,54 @@ class Driver implements DriverInterface
*/
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());
return 0;
}
}
......@@ -45,10 +45,18 @@ class MysqliConnection implements Connection
$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']);
}
......
......@@ -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
{
}
......@@ -203,15 +203,19 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$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 = ?";
......
......@@ -9,7 +9,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
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)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment