Remove 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 92ba48ce
<?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;
}
}
...@@ -23,7 +23,6 @@ use function preg_match; ...@@ -23,7 +23,6 @@ use function preg_match;
use function sprintf; use function sprintf;
use function str_replace; use function str_replace;
use const OCI_COMMIT_ON_SUCCESS;
use const OCI_NO_AUTO_COMMIT; use const OCI_NO_AUTO_COMMIT;
/** /**
...@@ -36,8 +35,8 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -36,8 +35,8 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
/** @var resource */ /** @var resource */
protected $dbh; protected $dbh;
/** @var int */ /** @var ExecutionMode */
protected $executeMode = OCI_COMMIT_ON_SUCCESS; private $executionMode;
/** /**
* Creates a Connection to an Oracle Database using oci8 extension. * Creates a Connection to an Oracle Database using oci8 extension.
...@@ -68,6 +67,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -68,6 +67,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
} }
$this->dbh = $dbh; $this->dbh = $dbh;
$this->executionMode = new ExecutionMode();
} }
/** /**
...@@ -107,7 +107,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -107,7 +107,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
public function prepare(string $sql): DriverStatement public function prepare(string $sql): DriverStatement
{ {
return new Statement($this->dbh, $sql, $this); return new Statement($this->dbh, $sql, $this->executionMode);
} }
public function query(string $sql): ResultInterface public function query(string $sql): ResultInterface
...@@ -154,22 +154,12 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -154,22 +154,12 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
return (int) $result; return (int) $result;
} }
/**
* Returns the current execution mode.
*
* @return int
*/
public function getExecuteMode()
{
return $this->executeMode;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function beginTransaction() public function beginTransaction()
{ {
$this->executeMode = OCI_NO_AUTO_COMMIT; $this->executionMode->disableAutoCommit();
return true; return true;
} }
...@@ -183,7 +173,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -183,7 +173,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
} }
$this->executeMode = OCI_COMMIT_ON_SUCCESS; $this->executionMode->enableAutoCommit();
return true; return true;
} }
...@@ -197,7 +187,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection ...@@ -197,7 +187,7 @@ class OCI8Connection implements ConnectionInterface, ServerInfoAwareConnection
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
} }
$this->executeMode = OCI_COMMIT_ON_SUCCESS; $this->executionMode->enableAutoCommit();
return true; return true;
} }
......
...@@ -24,7 +24,9 @@ use function substr; ...@@ -24,7 +24,9 @@ use function substr;
use const OCI_B_BIN; use const OCI_B_BIN;
use const OCI_B_BLOB; use const OCI_B_BLOB;
use const OCI_COMMIT_ON_SUCCESS;
use const OCI_D_LOB; use const OCI_D_LOB;
use const OCI_NO_AUTO_COMMIT;
use const OCI_TEMP_BLOB; use const OCI_TEMP_BLOB;
use const PREG_OFFSET_CAPTURE; use const PREG_OFFSET_CAPTURE;
use const SQLT_CHR; use const SQLT_CHR;
...@@ -42,8 +44,8 @@ class OCI8Statement implements StatementInterface ...@@ -42,8 +44,8 @@ class OCI8Statement implements StatementInterface
/** @var resource */ /** @var resource */
protected $_sth; protected $_sth;
/** @var OCI8Connection */ /** @var ExecutionMode */
protected $_conn; private $executionMode;
/** @var string[] */ /** @var string[] */
protected $_paramMap = []; protected $_paramMap = [];
...@@ -63,7 +65,7 @@ class OCI8Statement implements StatementInterface ...@@ -63,7 +65,7 @@ class OCI8Statement implements StatementInterface
* @param resource $dbh The connection handle. * @param resource $dbh The connection handle.
* @param string $query The SQL query. * @param string $query The SQL query.
*/ */
public function __construct($dbh, $query, OCI8Connection $conn) public function __construct($dbh, $query, ExecutionMode $executionMode)
{ {
[$query, $paramMap] = self::convertPositionalToNamedPlaceholders($query); [$query, $paramMap] = self::convertPositionalToNamedPlaceholders($query);
...@@ -73,7 +75,7 @@ class OCI8Statement implements StatementInterface ...@@ -73,7 +75,7 @@ class OCI8Statement implements StatementInterface
$this->_sth = $stmt; $this->_sth = $stmt;
$this->_dbh = $dbh; $this->_dbh = $dbh;
$this->_paramMap = $paramMap; $this->_paramMap = $paramMap;
$this->_conn = $conn; $this->executionMode = $executionMode;
} }
/** /**
...@@ -299,7 +301,13 @@ class OCI8Statement implements StatementInterface ...@@ -299,7 +301,13 @@ class OCI8Statement implements StatementInterface
} }
} }
$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) { if (! $ret) {
throw OCI8Exception::fromErrorInfo(oci_error($this->_sth)); throw OCI8Exception::fromErrorInfo(oci_error($this->_sth));
} }
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\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