Removed the OCI8Connection::getExecuteMode() method

The existing relationship between the connection and its statement violate the ISP principle. Instead of having access only to the execution mode of the connection, statements have access to the entire connection API.

Having a method which is not defined in the driver connection interface makes it impossible to mark the method `final` (#3590).
parent 04db0efa
......@@ -63,6 +63,7 @@ Table columns are no longer indexed by column name. Use the `name` attribute of
- Class `Doctrine\DBAL\Driver\Mysqli\Driver` was made final.
- Class `Doctrine\DBAL\Driver\Mysqli\MysqliStatement` was made final.
- Class `Doctrine\DBAL\Driver\OCI8\Driver` was made final.
- Class `Doctrine\DBAL\Driver\OCI8\OCI8Connection` was made final.
- Class `Doctrine\DBAL\Driver\OCI8\OCI8Statement` was made final.
- Class `Doctrine\DBAL\Driver\PDOSqlsrv\Driver` was made final.
- Class `Doctrine\DBAL\Driver\PDOSqlsrv\Statement` was made final.
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\OCI8;
/**
* Encapsulates the execution mode that is shared between the connection and its statements.
*
* @internal This class is not covered by the backward compatibility promise
*/
final class ExecutionMode
{
/** @var bool */
private $isAutoCommitEnabled = true;
public function enableAutoCommit() : void
{
$this->isAutoCommitEnabled = true;
}
public function disableAutoCommit() : void
{
$this->isAutoCommitEnabled = false;
}
public function isAutoCommitEnabled() : bool
{
return $this->isAutoCommitEnabled;
}
}
......@@ -9,9 +9,7 @@ use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use UnexpectedValueException;
use const OCI_COMMIT_ON_SUCCESS;
use const OCI_DEFAULT;
use const OCI_NO_AUTO_COMMIT;
use function addcslashes;
use function oci_commit;
use function oci_connect;
......@@ -26,13 +24,13 @@ use function str_replace;
/**
* OCI8 implementation of the Connection interface.
*/
class OCI8Connection implements Connection, ServerInfoAwareConnection
final class OCI8Connection implements Connection, ServerInfoAwareConnection
{
/** @var resource */
protected $dbh;
/** @var int */
protected $executeMode = OCI_COMMIT_ON_SUCCESS;
/** @var ExecutionMode */
private $executionMode;
/**
* Creates a Connection to an Oracle Database using oci8 extension.
......@@ -56,6 +54,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
}
$this->dbh = $dbh;
$this->executionMode = new ExecutionMode();
}
/**
......@@ -90,7 +89,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
*/
public function prepare(string $sql) : DriverStatement
{
return new OCI8Statement($this->dbh, $sql, $this);
return new OCI8Statement($this->dbh, $sql, $this->executionMode);
}
/**
......@@ -143,20 +142,12 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
return $result;
}
/**
* Returns the current execution mode.
*/
public function getExecuteMode() : int
{
return $this->executeMode;
}
/**
* {@inheritdoc}
*/
public function beginTransaction() : void
{
$this->executeMode = OCI_NO_AUTO_COMMIT;
$this->executionMode->disableAutoCommit();
}
/**
......@@ -168,7 +159,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
}
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
$this->executionMode->enableAutoCommit();
}
/**
......@@ -180,6 +171,6 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
}
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
$this->executionMode->enableAutoCommit();
}
}
......@@ -15,9 +15,11 @@ use const OCI_ASSOC;
use const OCI_B_BIN;
use const OCI_B_BLOB;
use const OCI_BOTH;
use const OCI_COMMIT_ON_SUCCESS;
use const OCI_D_LOB;
use const OCI_FETCHSTATEMENT_BY_COLUMN;
use const OCI_FETCHSTATEMENT_BY_ROW;
use const OCI_NO_AUTO_COMMIT;
use const OCI_NUM;
use const OCI_RETURN_LOBS;
use const OCI_RETURN_NULLS;
......@@ -52,8 +54,8 @@ final class OCI8Statement implements IteratorAggregate, Statement
/** @var resource */
protected $_sth;
/** @var OCI8Connection */
protected $_conn;
/** @var ExecutionMode */
protected $executionMode;
/** @var int[] */
protected static $fetchModeMap = [
......@@ -93,7 +95,7 @@ final class OCI8Statement implements IteratorAggregate, Statement
*
* @throws OCI8Exception
*/
public function __construct($dbh, string $query, OCI8Connection $conn)
public function __construct($dbh, string $query, ExecutionMode $executionMode)
{
[$query, $paramMap] = (new ConvertPositionalToNamedPlaceholders())($query);
......@@ -103,7 +105,7 @@ final class OCI8Statement implements IteratorAggregate, Statement
$this->_sth = $stmt;
$this->_dbh = $dbh;
$this->_paramMap = $paramMap;
$this->_conn = $conn;
$this->executionMode = $executionMode;
}
/**
......@@ -208,7 +210,13 @@ final class OCI8Statement implements IteratorAggregate, Statement
}
}
$ret = @oci_execute($this->_sth, $this->_conn->getExecuteMode());
if ($this->executionMode->isAutoCommitEnabled()) {
$mode = OCI_COMMIT_ON_SUCCESS;
} else {
$mode = OCI_NO_AUTO_COMMIT;
}
$ret = @oci_execute($this->_sth, $mode);
if (! $ret) {
throw OCI8Exception::fromErrorInfo(oci_error($this->_sth));
}
......
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Driver\OCI8;
use Doctrine\DBAL\Driver\OCI8\ExecutionMode;
use PHPStan\Testing\TestCase;
final class ExecutionModeTest extends TestCase
{
/** @var ExecutionMode */
private $mode;
protected function setUp() : void
{
$this->mode = new ExecutionMode();
}
public function testDefaultAutoCommitStatus() : void
{
self::assertTrue($this->mode->isAutoCommitEnabled());
}
public function testChangeAutoCommitStatus() : void
{
$this->mode->disableAutoCommit();
self::assertFalse($this->mode->isAutoCommitEnabled());
$this->mode->enableAutoCommit();
self::assertTrue($this->mode->isAutoCommitEnabled());
}
}
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