Add OCI8\Exception\Error

parent a77c0074
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\OCI8\Exception;
use Doctrine\DBAL\Driver\OCI8\OCI8Exception;
use function assert;
use function oci_error;
/**
* @internal
*
* @psalm-immutable
*/
final class ConnectionFailed extends OCI8Exception
{
public static function new(): self
{
$error = oci_error();
assert($error !== false);
return new self($error['message'], null, $error['code']);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\OCI8\Exception;
use Doctrine\DBAL\Driver\OCI8\OCI8Exception;
use function assert;
use function oci_error;
/**
* @internal
*
* @psalm-immutable
*/
final class Error extends OCI8Exception
{
/**
* @param resource $resource
*/
public static function new($resource): self
{
$error = oci_error($resource);
assert($error !== false);
return new self($error['message'], null, $error['code']);
}
}
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Doctrine\DBAL\Driver\OCI8; namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\OCI8\Exception\ConnectionFailed;
use Doctrine\DBAL\Driver\OCI8\Exception\Error;
use Doctrine\DBAL\Driver\OCI8\Exception\SequenceDoesNotExist; use Doctrine\DBAL\Driver\OCI8\Exception\SequenceDoesNotExist;
use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
...@@ -15,7 +17,6 @@ use function is_float; ...@@ -15,7 +17,6 @@ use function is_float;
use function is_int; use function is_int;
use function oci_commit; use function oci_commit;
use function oci_connect; use function oci_connect;
use function oci_error;
use function oci_pconnect; use function oci_pconnect;
use function oci_rollback; use function oci_rollback;
use function oci_server_version; use function oci_server_version;
...@@ -63,7 +64,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -63,7 +64,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
: @oci_connect($username, $password, $db, $charset, $sessionMode); : @oci_connect($username, $password, $db, $charset, $sessionMode);
if ($dbh === false) { if ($dbh === false) {
throw OCI8Exception::fromErrorInfo(oci_error()); throw ConnectionFailed::new();
} }
$this->dbh = $dbh; $this->dbh = $dbh;
...@@ -81,7 +82,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -81,7 +82,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
$version = oci_server_version($this->dbh); $version = oci_server_version($this->dbh);
if ($version === false) { if ($version === false) {
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); throw Error::new($this->dbh);
} }
if (preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches) === 0) { if (preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches) === 0) {
...@@ -162,7 +163,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -162,7 +163,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
public function commit() public function commit()
{ {
if (! oci_commit($this->dbh)) { if (! oci_commit($this->dbh)) {
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); throw Error::new($this->dbh);
} }
$this->executionMode->enableAutoCommit(); $this->executionMode->enableAutoCommit();
...@@ -176,7 +177,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -176,7 +177,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
public function rollBack() public function rollBack()
{ {
if (! oci_rollback($this->dbh)) { if (! oci_rollback($this->dbh)) {
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); throw Error::new($this->dbh);
} }
$this->executionMode->enableAutoCommit(); $this->executionMode->enableAutoCommit();
......
...@@ -11,17 +11,4 @@ use Doctrine\DBAL\Driver\AbstractDriverException; ...@@ -11,17 +11,4 @@ use Doctrine\DBAL\Driver\AbstractDriverException;
*/ */
class OCI8Exception extends AbstractDriverException class OCI8Exception extends AbstractDriverException
{ {
/**
* @param mixed[]|false $error
*
* @return OCI8Exception
*/
public static function fromErrorInfo($error)
{
if ($error === false) {
return new self('Database error occurred but no error information was retrieved from the driver.');
}
return new self($error['message'], null, $error['code']);
}
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\DBAL\Driver\OCI8; namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\Driver\OCI8\Exception\Error;
use Doctrine\DBAL\Driver\OCI8\Exception\UnknownParameterIndex; use Doctrine\DBAL\Driver\OCI8\Exception\UnknownParameterIndex;
use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface;
...@@ -11,7 +12,6 @@ use function assert; ...@@ -11,7 +12,6 @@ use function assert;
use function is_int; use function is_int;
use function is_resource; use function is_resource;
use function oci_bind_by_name; use function oci_bind_by_name;
use function oci_error;
use function oci_execute; use function oci_execute;
use function oci_new_descriptor; use function oci_new_descriptor;
use function oci_parse; use function oci_parse;
...@@ -156,7 +156,7 @@ class OCI8Statement implements StatementInterface ...@@ -156,7 +156,7 @@ class OCI8Statement implements StatementInterface
$ret = @oci_execute($this->_sth, $mode); $ret = @oci_execute($this->_sth, $mode);
if (! $ret) { if (! $ret) {
throw OCI8Exception::fromErrorInfo(oci_error($this->_sth)); throw Error::new($this->_sth);
} }
return new Result($this->_sth); return new Result($this->_sth);
......
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