Commit 2dd76c4a authored by dazz's avatar dazz Committed by Benjamin Eberlei

[DBAL-407] Implement and detect not unique field value exception for sqlite

parent d47e926e
...@@ -28,6 +28,7 @@ class DBALException extends \Exception ...@@ -28,6 +28,7 @@ class DBALException extends \Exception
const ERROR_NOT_NULL = 5; const ERROR_NOT_NULL = 5;
const ERROR_BAD_FIELD_NAME = 6; const ERROR_BAD_FIELD_NAME = 6;
const ERROR_NON_UNIQUE_FIELD_NAME = 7; const ERROR_NON_UNIQUE_FIELD_NAME = 7;
const ERROR_NOT_UNIQUE = 8;
/** /**
* @param string $method * @param string $method
......
...@@ -124,9 +124,14 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -124,9 +124,14 @@ class Driver implements \Doctrine\DBAL\Driver
if (strpos($exception->getMessage(), 'must be unique') !== false) { if (strpos($exception->getMessage(), 'must be unique') !== false) {
return DBALException::ERROR_DUPLICATE_KEY; return DBALException::ERROR_DUPLICATE_KEY;
} }
if (strpos($exception->getMessage(), 'may not be NULL') !== false) { if (strpos($exception->getMessage(), 'may not be NULL') !== false) {
return DBALException::ERROR_NOT_NULL; return DBALException::ERROR_NOT_NULL;
} }
if (strpos($exception->getMessage(), 'is not unique') !== false) {
return DBALException::ERROR_NOT_UNIQUE;
}
case 'HY000': case 'HY000':
if (strpos($exception->getMessage(), 'no such table:') !== false) { if (strpos($exception->getMessage(), 'no such table:') !== false) {
return DBALException::ERROR_UNKNOWN_TABLE; return DBALException::ERROR_UNKNOWN_TABLE;
......
...@@ -115,7 +115,6 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -115,7 +115,6 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$table2 = $schema->createTable("ambiguous_list_table_2"); $table2 = $schema->createTable("ambiguous_list_table_2");
$table2->addColumn('id', 'integer'); $table2->addColumn('id', 'integer');
foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) { foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) {
$this->_conn->executeQuery($sql); $this->_conn->executeQuery($sql);
} }
...@@ -123,7 +122,23 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -123,7 +122,23 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$sql = 'SELECT id FROM ambiguous_list_table, ambiguous_list_table_2'; $sql = 'SELECT id FROM ambiguous_list_table, ambiguous_list_table_2';
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NON_UNIQUE_FIELD_NAME); $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NON_UNIQUE_FIELD_NAME);
$this->_conn->executeQuery($sql); $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_NOT_UNIQUE);
$this->_conn->insert("unique_field_table", array('id' => 5));
} }
protected function onNotSuccessfulTest(\Exception $e) protected function onNotSuccessfulTest(\Exception $e)
......
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