Commit a7105d0d authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #484 from deeky666/implement-oracle-exception-driver-converter

Implement ExceptionConverterDriver in Oracle drivers
parents f4e45ee1 25c4a086
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
namespace Doctrine\DBAL\Driver\OCI8; namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\OracleSchemaManager; use Doctrine\DBAL\Schema\OracleSchemaManager;
...@@ -29,13 +31,14 @@ use Doctrine\DBAL\Schema\OracleSchemaManager; ...@@ -29,13 +31,14 @@ use Doctrine\DBAL\Schema\OracleSchemaManager;
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @since 2.0 * @since 2.0
*/ */
class Driver implements \Doctrine\DBAL\Driver class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
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 OCI8Connection( return new OCI8Connection(
$username, $username,
$password, $password,
...@@ -44,6 +47,9 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -44,6 +47,9 @@ class Driver implements \Doctrine\DBAL\Driver
isset($params['sessionMode']) ? $params['sessionMode'] : OCI_DEFAULT, isset($params['sessionMode']) ? $params['sessionMode'] : OCI_DEFAULT,
isset($params['persistent']) ? $params['persistent'] : false isset($params['persistent']) ? $params['persistent'] : false
); );
} catch (OCI8Exception $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
...@@ -130,6 +136,39 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -130,6 +136,39 @@ class Driver implements \Doctrine\DBAL\Driver
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
switch ($exception->getCode()) {
case '1':
case '2299':
case '38911':
return DBALException::ERROR_DUPLICATE_KEY;
case '904':
return DBALException::ERROR_BAD_FIELD_NAME;
case '918':
case '960':
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
case '923':
return DBALException::ERROR_SYNTAX;
case '942':
return DBALException::ERROR_UNKNOWN_TABLE;
case '955':
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
case '1017':
case '12545':
return DBALException::ERROR_ACCESS_DENIED;
case '1400':
return DBALException::ERROR_NOT_NULL;
case '2292':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
return 0; return 0;
} }
} }
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
namespace Doctrine\DBAL\Driver\PDOOracle; namespace Doctrine\DBAL\Driver\PDOOracle;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\OracleSchemaManager; use Doctrine\DBAL\Schema\OracleSchemaManager;
...@@ -32,19 +34,23 @@ use Doctrine\DBAL\Schema\OracleSchemaManager; ...@@ -32,19 +34,23 @@ use Doctrine\DBAL\Schema\OracleSchemaManager;
* which leads us to the recommendation to use the "oci8" driver to connect * which leads us to the recommendation to use the "oci8" driver to connect
* to Oracle instead. * to Oracle instead.
*/ */
class Driver implements \Doctrine\DBAL\Driver class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
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 PDOConnection( return new PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch (\PDOException $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
...@@ -136,6 +142,46 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -136,6 +142,46 @@ class Driver implements \Doctrine\DBAL\Driver
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
$errorCode = $exception->getCode();
// Use driver-specific error code instead of SQLSTATE for PDO exceptions if available.
if ($exception instanceof \PDOException && null !== $exception->errorInfo[1]) {
$errorCode = $exception->errorInfo[1];
}
switch ($errorCode) {
case '1':
case '2299':
case '38911':
return DBALException::ERROR_DUPLICATE_KEY;
case '904':
return DBALException::ERROR_BAD_FIELD_NAME;
case '918':
case '960':
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
case '923':
return DBALException::ERROR_SYNTAX;
case '942':
return DBALException::ERROR_UNKNOWN_TABLE;
case '955':
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
case '1017':
case '12545':
return DBALException::ERROR_ACCESS_DENIED;
case '1400':
return DBALException::ERROR_NOT_NULL;
case '2292':
return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
}
return 0; return 0;
} }
} }
...@@ -234,14 +234,11 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -234,14 +234,11 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$table = $schema->createTable("no_connection"); $table = $schema->createTable("no_connection");
$table->addColumn('id', 'integer'); $table->addColumn('id', 'integer');
try { $this->setExpectedException('Doctrine\DBAL\Exception\AccessDeniedException', null, $exceptionCode);
foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) { foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
$conn->executeQuery($sql); $conn->executeQuery($sql);
} }
$this->fail("Did not trigger DBALException with code " . $exceptionCode);
} catch (\Doctrine\DBAL\DBALException $e) {
$this->assertEquals($exceptionCode, $e->getCode(), "Got exception " . $e->getMessage());
}
} }
public function getConnectionParams() public function getConnectionParams()
......
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