Skip to content

Commit

Permalink
Extract methods into the ConnectionInterface.
Browse files Browse the repository at this point in the history
This builds a very minimal interface for connections. I've tried to
focus it on only what is needed for fixture interoperability and
ConnectionManager functionality. Trying to abstract anything else seemed
foolish at this point.

I added a new `disableConstraints()` method to Connection. This method
works like `transactional()` and aims to hide the implementation of
if/how foreign keys/constraints are managed in a connection.

Refs #6288
  • Loading branch information
markstory committed Jul 11, 2015
1 parent d7471e5 commit 969ea24
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 41 deletions.
79 changes: 41 additions & 38 deletions src/Database/Connection.php
Expand Up @@ -123,19 +123,15 @@ public function __destruct()
}

/**
* Get the configuration data used to create the connection.
*
* @return array
* {@inheritDoc}
*/
public function config()
{
return $this->_config;
}

/**
* Get the configuration name for this connection.
*
* @return string
* {@inheritDoc}
*/
public function configName()
{
Expand Down Expand Up @@ -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:
*
Expand All @@ -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)
{
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
*
Expand All @@ -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)
{
Expand Down
58 changes: 58 additions & 0 deletions src/Datasource/ConnectionInterface.php
Expand Up @@ -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);
}
6 changes: 3 additions & 3 deletions src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -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);
Expand Down

0 comments on commit 969ea24

Please sign in to comment.