Commit e85e7130 authored by dazz's avatar dazz Committed by Benjamin Eberlei

[DBAL-407] Introduce failing test for duplicate key exception code

parent d79ce18e
...@@ -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,8 @@ namespace Doctrine\DBAL; ...@@ -21,6 +21,8 @@ namespace Doctrine\DBAL;
class DBALException extends \Exception class DBALException extends \Exception
{ {
const ERROR_DUPLICATE_KEY = 1;
/** /**
* @param string $method * @param string $method
* *
...@@ -74,13 +76,14 @@ class DBALException extends \Exception ...@@ -74,13 +76,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) {
......
...@@ -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;
}
} }
...@@ -67,4 +67,12 @@ class Driver implements DriverInterface ...@@ -67,4 +67,12 @@ class Driver implements DriverInterface
return $params['dbname']; return $params['dbname'];
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
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;
}
} }
...@@ -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;
}
} }
...@@ -108,4 +108,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -108,4 +108,12 @@ class Driver implements \Doctrine\DBAL\Driver
} }
return $conn->query('SELECT DATABASE()')->fetchColumn(); return $conn->query('SELECT DATABASE()')->fetchColumn();
} }
/**
* {@inheritdoc}
*/
public function convertExceptionCode(\Exception $exception)
{
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;
}
} }
...@@ -99,5 +99,13 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -99,5 +99,13 @@ 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)
{
return 0;
}
} }
...@@ -112,4 +112,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -112,4 +112,12 @@ 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)
{
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());
} }
} }
<?php
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\DBALException;
require_once __DIR__ . '/../../TestInit.php';
class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function testDuplicateKeyException()
{
/* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
$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));
}
}
\ 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