Commit f4635a9b authored by Benjamin Eberlei's avatar Benjamin Eberlei

Fully implement MySQL support for Exception handling

parent 89592347
......@@ -28,7 +28,6 @@ class DBALException extends \Exception
const ERROR_NOT_NULL = 5;
const ERROR_BAD_FIELD_NAME = 6;
const ERROR_NON_UNIQUE_FIELD_NAME = 7;
const ERROR_NOT_UNIQUE = 8;
const ERROR_SYNTAX = 9;
const ERROR_UNABLE_TO_OPEN = 10;
const ERROR_WRITE_READONLY = 11;
......
......@@ -21,6 +21,7 @@ namespace Doctrine\DBAL\Driver\PDOMySql;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use PDOException;
/**
* PDO MySql driver.
......@@ -34,12 +35,16 @@ class Driver implements \Doctrine\DBAL\Driver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{
try {
$conn = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params),
$username,
$password,
$driverOptions
);
} catch (PDOException $e) {
throw DBALException::driverException($this, $e);
}
return $conn;
}
......@@ -116,17 +121,44 @@ class Driver implements \Doctrine\DBAL\Driver
public function convertExceptionCode(\Exception $exception)
{
switch ($exception->getCode()) {
case 23000:
case '42S02':
return DBALException::ERROR_UNKNOWN_TABLE;
case '42S01':
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
default:
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;
}
case '42S02':
return DBALException::ERROR_UNKNOWN_TABLE;
case '42S01':
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
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;
}
}
return 0;
......
......@@ -134,7 +134,7 @@ class Driver implements \Doctrine\DBAL\Driver
}
if (strpos($exception->getMessage(), 'is not unique') !== false) {
return DBALException::ERROR_NOT_UNIQUE;
return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'no such table:') !== false) {
......
......@@ -87,7 +87,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
}
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NOT_NULL);
$this->_conn->insert("notnull_table", array('id' => 1));
$this->_conn->insert("notnull_table", array('id' => 1, 'value' => null));
}
public function testBadFieldNameException()
......@@ -137,7 +137,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
}
$this->_conn->insert("unique_field_table", array('id' => 5));
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NOT_UNIQUE);
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_DUPLICATE_KEY);
$this->_conn->insert("unique_field_table", array('id' => 5));
}
......
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