diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 3d22980aa6e..7983bc891c1 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -123,9 +123,7 @@ public function __destruct() } /** - * Get the configuration data used to create the connection. - * - * @return array + * {@inheritDoc} */ public function config() { @@ -133,9 +131,7 @@ public function config() } /** - * Get the configuration name for this connection. - * - * @return string + * {@inheritDoc} */ public function configName() { @@ -533,13 +529,7 @@ public function enableForeignKeys() } /** - * Executes a callable function inside a transaction, if any exception occurs - * while executing the passed callable, the transaction will be rolled back - * If the result of the callable function is ``false``, the transaction will - * also be rolled back. Otherwise the transaction is committed after executing - * the callback. - * - * The callback will receive the connection instance as its first argument. + * {@inheritDoc} * * ### Example: * @@ -548,11 +538,6 @@ public function enableForeignKeys() * $connection->newQuery()->delete('users')->execute(); * }); * ``` - * - * @param callable $callback the code to be executed inside a transaction - * @return mixed result from the $callback function - * @throws \Exception Will re-throw any exception raised in $callback after - * rolling back the transaction. */ public function transactional(callable $callback) { @@ -574,6 +559,32 @@ public function transactional(callable $callback) return $result; } + /** + * {@inheritDoc} + * + * ### Example: + * + * ``` + * $connection->disableConstraints(function ($connection) { + * $connection->newQuery()->delete('users')->execute(); + * }); + * ``` + */ + public function disableConstraints(callable $callback) + { + $this->disableForeignKeys(); + + try { + $result = $callback($this); + } catch (\Exception $e) { + $this->enableForeignKeys(); + throw $e; + } + + $this->enableForeignKeys(); + return $result; + } + /** * Checks if a transaction is running. * @@ -619,21 +630,6 @@ public function quoteIdentifier($identifier) return $this->_driver->quoteIdentifier($identifier); } - /** - * Enables or disables query logging for this connection. - * - * @param bool $enable whether to turn logging on or disable it. - * Use null to read current value. - * @return bool - */ - public function logQueries($enable = null) - { - if ($enable === null) { - return $this->_logQueries; - } - $this->_logQueries = $enable; - } - /** * Enables or disables metadata caching for this connection * @@ -650,11 +646,18 @@ public function cacheMetadata($cache) } /** - * Sets the logger object instance. When called with no arguments - * it returns the currently setup logger instance. - * - * @param object $instance logger object instance - * @return object logger instance + * {@inheritDoc} + */ + public function logQueries($enable = null) + { + if ($enable === null) { + return $this->_logQueries; + } + $this->_logQueries = $enable; + } + + /** + * {@inheritDoc} */ public function logger($instance = null) { diff --git a/src/Datasource/ConnectionInterface.php b/src/Datasource/ConnectionInterface.php index 2a1cdf5b18f..624e40bf28f 100644 --- a/src/Datasource/ConnectionInterface.php +++ b/src/Datasource/ConnectionInterface.php @@ -20,5 +20,63 @@ */ interface ConnectionInterface { + /** + * Get the configuration name for this connection. + * + * @return string + */ + public function configName(); + /** + * Get the configuration data used to create the connection. + * + * @return array + */ + public function config(); + + /** + * Executes a callable function inside a transaction, if any exception occurs + * while executing the passed callable, the transaction will be rolled back + * If the result of the callable function is ``false``, the transaction will + * also be rolled back. Otherwise the transaction is committed after executing + * the callback. + * + * The callback will receive the connection instance as its first argument. + * + * @param callable $callable The callback to execute within a transaction. + * @return mixed The return value of the callback. + * @throws \Exception Will re-throw any exception raised in $callback after + * rolling back the transaction. + */ + public function transactional(callable $transaction); + + /** + * Run an operation with constraints disabled. + * + * Constraints should be re-enabled after the callback succeeds/fails. + * + * @param callable $callable The callback to execute within a transaction. + * @return mixed The return value of the callback. + * @throws \Exception Will re-throw any exception raised in $callback after + * rolling back the transaction. + */ + public function disableConstraints(callable $operation); + + /** + * Enables or disables query logging for this connection. + * + * @param bool $enable whether to turn logging on or disable it. + * Use null to read current value. + * @return bool + */ + public function logQueries($enable = null); + + /** + * Sets the logger object instance. When called with no arguments + * it returns the currently setup logger instance. + * + * @param object $instance logger object instance + * @return object logger instance + */ + public function logger($instance = null); } diff --git a/src/TestSuite/Fixture/FixtureManager.php b/src/TestSuite/Fixture/FixtureManager.php index 8c34e35672a..d281d80b8bf 100644 --- a/src/TestSuite/Fixture/FixtureManager.php +++ b/src/TestSuite/Fixture/FixtureManager.php @@ -284,9 +284,9 @@ protected function _runOperation($fixtures, $operation) $db->logQueries(false); } $db->transactional(function ($db) use ($fixtures, $operation) { - $db->disableForeignKeys(); - $operation($db, $fixtures); - $db->enableForeignKeys(); + $db->disableConstraints(function ($db) use ($fixtures, $operation) { + $operation($db, $fixtures); + }); }); if ($logQueries) { $db->logQueries(true);