Commit 46863217 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge branch 'DBAL-407'

parents d79ce18e 4a5be979
...@@ -5,3 +5,4 @@ dist/ ...@@ -5,3 +5,4 @@ dist/
download/ download/
lib/Doctrine/Common/ lib/Doctrine/Common/
vendor/ vendor/
*.phpunit.xml
...@@ -645,7 +645,7 @@ class Connection implements DriverConnection ...@@ -645,7 +645,7 @@ class Connection implements DriverConnection
try { try {
$stmt = new Statement($statement, $this); $stmt = new Statement($statement, $this);
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw DBALException::driverExceptionDuringQuery($ex, $statement); throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
} }
$stmt->setFetchMode($this->defaultFetchMode); $stmt->setFetchMode($this->defaultFetchMode);
...@@ -698,7 +698,7 @@ class Connection implements DriverConnection ...@@ -698,7 +698,7 @@ class Connection implements DriverConnection
$stmt = $this->_conn->query($query); $stmt = $this->_conn->query($query);
} }
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw DBALException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types)); throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
} }
$stmt->setFetchMode($this->defaultFetchMode); $stmt->setFetchMode($this->defaultFetchMode);
...@@ -807,7 +807,7 @@ class Connection implements DriverConnection ...@@ -807,7 +807,7 @@ class Connection implements DriverConnection
break; break;
} }
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw DBALException::driverExceptionDuringQuery($ex, $args[0]); throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]);
} }
$statement->setFetchMode($this->defaultFetchMode); $statement->setFetchMode($this->defaultFetchMode);
...@@ -860,7 +860,7 @@ class Connection implements DriverConnection ...@@ -860,7 +860,7 @@ class Connection implements DriverConnection
$result = $this->_conn->exec($query); $result = $this->_conn->exec($query);
} }
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw DBALException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types)); throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
} }
if ($logger) { if ($logger) {
...@@ -891,7 +891,7 @@ class Connection implements DriverConnection ...@@ -891,7 +891,7 @@ class Connection implements DriverConnection
try { try {
$result = $this->_conn->exec($statement); $result = $this->_conn->exec($statement);
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw DBALException::driverExceptionDuringQuery($ex, $statement); throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
} }
if ($logger) { if ($logger) {
......
...@@ -21,6 +21,18 @@ namespace Doctrine\DBAL; ...@@ -21,6 +21,18 @@ namespace Doctrine\DBAL;
class DBALException extends \Exception class DBALException extends \Exception
{ {
const ERROR_DUPLICATE_KEY = 1;
const ERROR_UNKNOWN_TABLE = 2;
const ERROR_TABLE_ALREADY_EXISTS = 3;
const ERROR_FOREIGN_KEY_CONSTRAINT = 4;
const ERROR_NOT_NULL = 5;
const ERROR_BAD_FIELD_NAME = 6;
const ERROR_NON_UNIQUE_FIELD_NAME = 7;
const ERROR_SYNTAX = 9;
const ERROR_UNABLE_TO_OPEN = 10;
const ERROR_WRITE_READONLY = 11;
const ERROR_ACCESS_DENIED = 12;
/** /**
* @param string $method * @param string $method
* *
...@@ -74,13 +86,14 @@ class DBALException extends \Exception ...@@ -74,13 +86,14 @@ class DBALException extends \Exception
} }
/** /**
* @param \Doctrine\DBAL\Driver $driver
* @param \Exception $driverEx * @param \Exception $driverEx
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* *
* @return \Doctrine\DBAL\DBALException * @return \Doctrine\DBAL\DBALException
*/ */
public static function driverExceptionDuringQuery(\Exception $driverEx, $sql, array $params = array()) public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = array())
{ {
$msg = "An exception occurred while executing '".$sql."'"; $msg = "An exception occurred while executing '".$sql."'";
if ($params) { if ($params) {
...@@ -88,7 +101,20 @@ class DBALException extends \Exception ...@@ -88,7 +101,20 @@ class DBALException extends \Exception
} }
$msg .= ":\n\n".$driverEx->getMessage(); $msg .= ":\n\n".$driverEx->getMessage();
return new self($msg, 0, $driverEx); return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx);
}
/**
* @param \Doctrine\DBAL\Driver $driver
* @param \Exception $driverEx
*
* @return \Doctrine\DBAL\DBALException
*/
public static function driverException(Driver $driver, \Exception $driverEx)
{
$msg = "An exception occured in driver: " . $driverEx->getMessage();
return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx);
} }
/** /**
......
...@@ -72,4 +72,12 @@ interface Driver ...@@ -72,4 +72,12 @@ interface Driver
* @return string The name of the database. * @return string The name of the database.
*/ */
public function getDatabase(Connection $conn); public function getDatabase(Connection $conn);
/**
* @param \Exception $exception
*
* @return int
*/
public function convertExceptionCode(\Exception $exception);
} }
...@@ -99,4 +99,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -99,4 +99,12 @@ class Driver implements \Doctrine\DBAL\Driver
return $params['dbname']; return $params['dbname'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
...@@ -91,4 +91,12 @@ class DB2Driver implements Driver ...@@ -91,4 +91,12 @@ class DB2Driver implements Driver
return $params['dbname']; return $params['dbname'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver as DriverInterface; use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\DBALException;
/** /**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com> * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
...@@ -31,7 +33,11 @@ class Driver implements DriverInterface ...@@ -31,7 +33,11 @@ class Driver implements DriverInterface
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{ {
try {
return new MysqliConnection($params, $username, $password, $driverOptions); return new MysqliConnection($params, $username, $password, $driverOptions);
} catch (MysqliException $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
...@@ -67,4 +73,60 @@ class Driver implements DriverInterface ...@@ -67,4 +73,60 @@ class Driver implements DriverInterface
return $params['dbname']; return $params['dbname'];
} }
/**
* {@inheritdoc}
*/
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 ...@@ -45,10 +45,18 @@ class MysqliConnection implements Connection
$socket = isset($params['unix_socket']) ? $params['unix_socket'] : ini_get('mysqli.default_socket'); $socket = isset($params['unix_socket']) ? $params['unix_socket'] : ini_get('mysqli.default_socket');
$this->_conn = mysqli_init(); $this->_conn = mysqli_init();
$previousHandler = set_error_handler(function () {
});
if ( ! $this->_conn->real_connect($params['host'], $username, $password, $params['dbname'], $port, $socket)) { 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); throw new MysqliException($this->_conn->connect_error, $this->_conn->connect_errno);
} }
set_error_handler($previousHandler);
if (isset($params['charset'])) { if (isset($params['charset'])) {
$this->_conn->set_charset($params['charset']); $this->_conn->set_charset($params['charset']);
} }
......
...@@ -19,9 +19,11 @@ ...@@ -19,9 +19,11 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\DBALException;
/** /**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com> * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/ */
class MysqliException extends \Exception class MysqliException extends DBALException
{ {
} }
...@@ -113,4 +113,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -113,4 +113,12 @@ class Driver implements \Doctrine\DBAL\Driver
return $params['user']; return $params['user'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
...@@ -105,4 +105,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -105,4 +105,12 @@ class Driver implements \Doctrine\DBAL\Driver
return $params['dbname']; return $params['dbname'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
namespace Doctrine\DBAL\Driver\PDOMySql; namespace Doctrine\DBAL\Driver\PDOMySql;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use PDOException;
/** /**
* PDO MySql driver. * PDO MySql driver.
...@@ -33,12 +35,16 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -33,12 +35,16 @@ class Driver implements \Doctrine\DBAL\Driver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{ {
try {
$conn = new \Doctrine\DBAL\Driver\PDOConnection( $conn = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch (PDOException $e) {
throw DBALException::driverException($this, $e);
}
return $conn; return $conn;
} }
...@@ -108,4 +114,53 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -108,4 +114,53 @@ class Driver implements \Doctrine\DBAL\Driver
} }
return $conn->query('SELECT DATABASE()')->fetchColumn(); return $conn->query('SELECT DATABASE()')->fetchColumn();
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
switch ($exception->getCode()) {
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;
}
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;
}
} }
...@@ -113,4 +113,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -113,4 +113,12 @@ class Driver implements \Doctrine\DBAL\Driver
return $params['user']; return $params['user'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
namespace Doctrine\DBAL\Driver\PDOPgSql; namespace Doctrine\DBAL\Driver\PDOPgSql;
use Doctrine\DBAL\Platforms; use Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use PDOException;
/** /**
* Driver that connects through pdo_pgsql. * Driver that connects through pdo_pgsql.
...@@ -33,12 +35,16 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -33,12 +35,16 @@ class Driver implements \Doctrine\DBAL\Driver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{ {
try {
return new \Doctrine\DBAL\Driver\PDOConnection( return new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch(PDOException $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
...@@ -99,5 +105,53 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -99,5 +105,53 @@ class Driver implements \Doctrine\DBAL\Driver
? $params['dbname'] ? $params['dbname']
: $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); : $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn();
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
if (strpos($exception->getMessage(), 'duplicate key value violates unique constraint') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}
if ($exception->getCode() === "42P01") {
return DBALException::ERROR_UNKNOWN_TABLE;
}
if ($exception->getCode() === "42P07") {
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
if ($exception->getCode() === "23503") {
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
if ($exception->getCode() === "23502") {
return DBALException::ERROR_NOT_NULL;
}
if ($exception->getCode() === "42703") {
return DBALException::ERROR_BAD_FIELD_NAME;
}
if ($exception->getCode() === "42702") {
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if ($exception->getCode() === "42601") {
return DBALException::ERROR_SYNTAX;
}
if (stripos($exception->getMessage(), 'password authentication failed for user') !== false) {
return DBALException::ERROR_ACCESS_DENIED;
}
if (stripos($exception->getMessage(), 'Name or service not known') !== false) {
return DBALException::ERROR_ACCESS_DENIED;
}
return 0;
}
} }
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
namespace Doctrine\DBAL\Driver\PDOSqlite; namespace Doctrine\DBAL\Driver\PDOSqlite;
use Doctrine\DBAL\DBALException;
use PDOException;
/** /**
* The PDO Sqlite driver. * The PDO Sqlite driver.
* *
...@@ -46,12 +49,16 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -46,12 +49,16 @@ class Driver implements \Doctrine\DBAL\Driver
unset($driverOptions['userDefinedFunctions']); unset($driverOptions['userDefinedFunctions']);
} }
try {
$pdo = new \Doctrine\DBAL\Driver\PDOConnection( $pdo = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch (PDOException $ex) {
throw DBALException::driverException($this, $ex);
}
foreach ($this->_userDefinedFunctions as $fn => $data) { foreach ($this->_userDefinedFunctions as $fn => $data) {
$pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']); $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
...@@ -112,4 +119,52 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -112,4 +119,52 @@ class Driver implements \Doctrine\DBAL\Driver
return isset($params['path']) ? $params['path'] : null; return isset($params['path']) ? $params['path'] : null;
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
if (strpos($exception->getMessage(), 'must be unique') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'may not be NULL') !== false) {
return DBALException::ERROR_NOT_NULL;
}
if (strpos($exception->getMessage(), 'is not unique') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'no such table:') !== false) {
return DBALException::ERROR_UNKNOWN_TABLE;
}
if (strpos($exception->getMessage(), 'already exists') !== false) {
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
if (strpos($exception->getMessage(), 'has no column named') !== false) {
return DBALException::ERROR_BAD_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'syntax error') !== false) {
return DBALException::ERROR_SYNTAX;
}
if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
return DBALException::ERROR_WRITE_READONLY;
}
if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
return DBALException::ERROR_UNABLE_TO_OPEN;
}
return 0;
}
} }
...@@ -102,4 +102,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -102,4 +102,12 @@ class Driver implements \Doctrine\DBAL\Driver
return $params['dbname']; return $params['dbname'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
...@@ -83,4 +83,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -83,4 +83,12 @@ class Driver implements \Doctrine\DBAL\Driver
$params = $conn->getParams(); $params = $conn->getParams();
return $params['dbname']; return $params['dbname'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
...@@ -164,7 +164,12 @@ class Statement implements \IteratorAggregate, DriverStatement ...@@ -164,7 +164,12 @@ class Statement implements \IteratorAggregate, DriverStatement
try { try {
$stmt = $this->stmt->execute($params); $stmt = $this->stmt->execute($params);
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw DBALException::driverExceptionDuringQuery($ex, $this->sql, $this->conn->resolveParams($this->params, $this->types)); throw DBALException::driverExceptionDuringQuery(
$this->conn->getDriver(),
$ex,
$this->sql,
$this->conn->resolveParams($this->params, $this->types)
);
} }
if ($logger) { if ($logger) {
......
...@@ -8,7 +8,8 @@ class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase ...@@ -8,7 +8,8 @@ class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase
{ {
public function testDriverExceptionDuringQueryAcceptsBinaryData() public function testDriverExceptionDuringQueryAcceptsBinaryData()
{ {
$e = DBALException::driverExceptionDuringQuery(new \Exception, '', array('ABC', chr(128))); $driver = $this->getMock('\Doctrine\DBAL\Driver');
$e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128)));
$this->assertContains('with params ["ABC", "\x80"]', $e->getMessage()); $this->assertContains('with params ["ABC", "\x80"]', $e->getMessage());
} }
} }
...@@ -212,6 +212,10 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -212,6 +212,10 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
*/ */
public function testFetchAllWithMissingTypes() 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'; $datetimeString = '2010-01-01 10:10:10';
$datetime = new \DateTime($datetimeString); $datetime = new \DateTime($datetimeString);
$sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
......
<?php
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\DBALException;
class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function setUp()
{
parent::setUp();
$supportExceptions = array('pdo_sqlite', 'pdo_mysql', 'pdo_pgsql', 'mysqli');
$params = $this->_conn->getParams();
if (!in_array($params['driver'], $supportExceptions)) {
$this->markTestSkipped('Driver does not support special exception handling.');
}
}
public function testDuplicateKeyException()
{
$table = new \Doctrine\DBAL\Schema\Table("duplicatekey_table");
$table->addColumn('id', 'integer', array());
$table->setPrimaryKey(array('id'));
foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
$this->_conn->executeQuery($sql);
}
$this->_conn->insert("duplicatekey_table", array('id' => 1));
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_DUPLICATE_KEY);
$this->_conn->insert("duplicatekey_table", array('id' => 1));
}
public function testUnknownTableException()
{
$sql = "SELECT * FROM unknown_table";
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_UNKNOWN_TABLE);
$this->_conn->executeQuery($sql);
}
public function testTableAlreadyExistsException()
{
$table = new \Doctrine\DBAL\Schema\Table("alreadyexist_table");
$table->addColumn('id', 'integer', array());
$table->setPrimaryKey(array('id'));
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_TABLE_ALREADY_EXISTS);
foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
$this->_conn->executeQuery($sql);
}
foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
$this->_conn->executeQuery($sql);
}
}
public function testForeignKeyContraintException()
{
if ( ! $this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped("Only fails on platforms with foreign key constraints.");
}
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("constraint_error_table");
$table->addColumn('id', 'integer', array());
$table->setPrimaryKey(array('id'));
$owningTable = $schema->createTable("owning_table");
$owningTable->addColumn('id', 'integer', array());
$owningTable->addColumn('constraint_id', 'integer', array());
$owningTable->setPrimaryKey(array('id'));
$owningTable->addForeignKeyConstraint($table, array('constraint_id'), array('id'));
foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
$this->_conn->executeQuery($sql);
}
$this->_conn->insert("constraint_error_table", array('id' => 1));
$this->_conn->insert("owning_table", array('id' => 1, 'constraint_id' => 1));
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_FOREIGN_KEY_CONSTRAINT);
$this->_conn->delete('constraint_error_table', array('id' => 1));
}
public function testNotNullException()
{
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("notnull_table");
$table->addColumn('id', 'integer', array());
$table->addColumn('value', 'integer', array('notnull' => true));
$table->setPrimaryKey(array('id'));
foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
$this->_conn->executeQuery($sql);
}
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NOT_NULL);
$this->_conn->insert("notnull_table", array('id' => 1, 'value' => null));
}
public function testBadFieldNameException()
{
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("bad_fieldname_table");
$table->addColumn('id', 'integer', array());
foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
$this->_conn->executeQuery($sql);
}
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_BAD_FIELD_NAME);
$this->_conn->insert("bad_fieldname_table", array('name' => 5));
}
public function testNonUniqueFieldNameException()
{
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("ambiguous_list_table");
$table->addColumn('id', 'integer');
$table2 = $schema->createTable("ambiguous_list_table_2");
$table2->addColumn('id', 'integer');
foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
$this->_conn->executeQuery($sql);
}
$sql = 'SELECT id FROM ambiguous_list_table, ambiguous_list_table_2';
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NON_UNIQUE_FIELD_NAME);
$this->_conn->executeQuery($sql);
}
public function testNotUniqueException()
{
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("unique_field_table");
$table->addColumn('id', 'integer');
$table->addUniqueIndex(array('id'));
foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
$this->_conn->executeQuery($sql);
}
$this->_conn->insert("unique_field_table", array('id' => 5));
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_DUPLICATE_KEY);
$this->_conn->insert("unique_field_table", array('id' => 5));
}
public function testSyntaxErrorException()
{
$table = new \Doctrine\DBAL\Schema\Table("syntax_error_table");
$table->addColumn('id', 'integer', array());
$table->setPrimaryKey(array('id'));
foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
$this->_conn->executeQuery($sql);
}
$sql = 'SELECT id FRO syntax_error_table';
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_SYNTAX);
$this->_conn->executeQuery($sql);
}
/**
* @dataProvider getSqLiteOpenConnection
*/
public function testConnectionExceptionSqLite($mode, $exceptionCode)
{
if ($this->_conn->getDatabasePlatform()->getName() != 'sqlite') {
$this->markTestSkipped("Only fails this way on sqlite");
}
$filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection.db');
if (file_exists($filename)) {
unlink($filename);
}
touch($filename);
chmod($filename, $mode);
$params = array(
'driver' => 'pdo_sqlite',
'path' => $filename,
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("no_connection");
$table->addColumn('id', 'integer');
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode);
foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
$conn->executeQuery($sql);
}
}
public function getSqLiteOpenConnection()
{
return array(
array(0000, DBALException::ERROR_UNABLE_TO_OPEN),
array(0444, DBALException::ERROR_WRITE_READONLY),
);
}
/**
* @dataProvider getConnectionParams
*/
public function testConnectionException($params, $exceptionCode)
{
if ($this->_conn->getDatabasePlatform()->getName() == 'sqlite') {
$this->markTestSkipped("Only skipped if platform is not sqlite");
}
$defaultParams = $this->_conn->getParams();
$params = array_merge($defaultParams, $params);
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("no_connection");
$table->addColumn('id', 'integer');
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode);
foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
$conn->executeQuery($sql);
}
}
public function getConnectionParams()
{
return array(
array(array('user' => 'not_existing'), DBALException::ERROR_ACCESS_DENIED),
array(array('password' => 'really_not'), DBALException::ERROR_ACCESS_DENIED),
array(array('host' => 'localnope'), DBALException::ERROR_ACCESS_DENIED),
);
}
}
...@@ -69,4 +69,9 @@ class DriverMock implements \Doctrine\DBAL\Driver ...@@ -69,4 +69,9 @@ class DriverMock implements \Doctrine\DBAL\Driver
{ {
return; return;
} }
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
} }
\ No newline at end of file
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