Skip to content

Commit

Permalink
Split combined into separate getter setter for cleaner API.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark authored and dereuromark committed Nov 15, 2016
1 parent 585453b commit 97f008a
Show file tree
Hide file tree
Showing 19 changed files with 1,149 additions and 248 deletions.
147 changes: 116 additions & 31 deletions src/Database/Connection.php
Expand Up @@ -81,7 +81,7 @@ class Connection implements ConnectionInterface
/**
* Logger object instance.
*
* @var \Cake\Database\Log\QueryLogger
* @var \Cake\Database\Log\QueryLogger|null
*/
protected $_logger = null;

Expand All @@ -105,7 +105,7 @@ public function __construct($config)
if (!empty($config['driver'])) {
$driver = $config['driver'];
}
$this->driver($driver, $config);
$this->setDriver($driver, $config);

if (!empty($config['log'])) {
$this->logQueries($config['log']);
Expand Down Expand Up @@ -146,19 +146,14 @@ public function configName()
* Sets the driver instance. If a string is passed it will be treated
* as a class name and will be instantiated.
*
* If no params are passed it will return the current driver instance.
*
* @param \Cake\Database\Driver|string|null $driver The driver instance to use.
* @param array $config Either config for a new driver or null.
* @param \Cake\Database\Driver|string $driver The driver instance to use.
* @param array $config Config for a new driver.
* @throws \Cake\Database\Exception\MissingDriverException When a driver class is missing.
* @throws \Cake\Database\Exception\MissingExtensionException When a driver's PHP extension is missing.
* @return \Cake\Database\Driver
* @return $this
*/
public function driver($driver = null, $config = [])
public function setDriver($driver, $config = [])
{
if ($driver === null) {
return $this->_driver;
}
if (is_string($driver)) {
$className = App::className($driver, 'Database/Driver');
if (!$className || !class_exists($className)) {
Expand All @@ -170,7 +165,41 @@ public function driver($driver = null, $config = [])
throw new MissingExtensionException(['driver' => get_class($driver)]);
}

return $this->_driver = $driver;
$this->_driver = $driver;

return $this;
}

/**
* Gets the driver instance.
*
* @return \Cake\Database\Driver
*/
public function getDriver()
{
return $this->_driver;
}

/**
* Sets the driver instance. If a string is passed it will be treated
* as a class name and will be instantiated.
*
* If no params are passed it will return the current driver instance.
*
* @deprecated 3.4.0 Use setDriver()/getDriver() instead.
* @param \Cake\Database\Driver|string|null $driver The driver instance to use.
* @param array $config Either config for a new driver or null.
* @throws \Cake\Database\Exception\MissingDriverException When a driver class is missing.
* @throws \Cake\Database\Exception\MissingExtensionException When a driver's PHP extension is missing.
* @return \Cake\Database\Driver
*/
public function driver($driver = null, $config = [])
{
if ($driver !== null) {
$this->setDriver($driver, $config);
}

return $this->getDriver();
}

/**
Expand Down Expand Up @@ -259,7 +288,7 @@ public function execute($query, array $params = [], array $types = [])
*/
public function compileQuery(Query $query, ValueBinder $generator)
{
return $this->driver()->compileQuery($query, $generator)[1];
return $this->getDriver()->compileQuery($query, $generator)[1];
}

/**
Expand Down Expand Up @@ -303,17 +332,25 @@ public function newQuery()
}

/**
* Gets or sets a Schema\Collection object for this connection.
* Sets a Schema\Collection object for this connection.
*
* @param \Cake\Database\Schema\Collection|null $collection The schema collection object
* @return \Cake\Database\Schema\Collection
* @param \Cake\Database\Schema\Collection $collection The schema collection object
* @return $this
*/
public function schemaCollection(SchemaCollection $collection = null)
public function setSchemaCollection(SchemaCollection $collection)
{
if ($collection !== null) {
return $this->_schemaCollection = $collection;
}
$this->_schemaCollection = $collection;

return $this;
}

/**
* Gets a Schema\Collection object for this connection.
*
* @return \Cake\Database\Schema\Collection
*/
public function getSchemaCollection()
{
if ($this->_schemaCollection !== null) {
return $this->_schemaCollection;
}
Expand All @@ -325,6 +362,22 @@ public function schemaCollection(SchemaCollection $collection = null)
return $this->_schemaCollection = new SchemaCollection($this);
}

/**
* Gets or sets a Schema\Collection object for this connection.
*
* @deprecated 3.4.0 Use setSchemaCollection()/getSchemaCollection()
* @param \Cake\Database\Schema\Collection|null $collection The schema collection object
* @return \Cake\Database\Schema\Collection
*/
public function schemaCollection(SchemaCollection $collection = null)
{
if ($collection !== null) {
$this->setSchemaCollection($collection);
}

return $this->getSchemaCollection();
}

/**
* Executes an INSERT query on the specified table.
*
Expand Down Expand Up @@ -394,7 +447,7 @@ public function begin()
}

$this->_transactionLevel++;
if ($this->useSavePoints()) {
if ($this->isEnabledSavePoints()) {
$this->createSavePoint($this->_transactionLevel);
}
}
Expand All @@ -418,7 +471,7 @@ public function commit()

return $this->_driver->commitTransaction();
}
if ($this->useSavePoints()) {
if ($this->isEnabledSavePoints()) {
$this->releaseSavePoint($this->_transactionLevel);
}

Expand All @@ -438,7 +491,7 @@ public function rollback()
return false;
}

$useSavePoint = $this->useSavePoints();
$useSavePoint = $this->isEnabledSavePoints();
if ($this->_transactionLevel === 0 || !$useSavePoint) {
$this->_transactionLevel = 0;
$this->_transactionStarted = false;
Expand All @@ -457,6 +510,41 @@ public function rollback()
return true;
}

/**
* Enables/disables the usage of savepoints, enables only if driver the allows it.
*
* If you are trying to enable this feature, make sure you check the return value of this
* function to verify it was enabled successfully.
*
* ### Example:
*
* `$connection->useSavePoints(true)` Returns true if drivers supports save points, false otherwise
* `$connection->useSavePoints(false)` Disables usage of savepoints and returns false
*
* @param bool $enable Whether or not save points should be used.
* @return $this
*/
public function enableSavePoints($enable)
{
if ($enable === false) {
$this->_useSavePoints = false;
} else {
$this->_useSavePoints = $this->_driver->supportsSavePoints();
}

return $this;
}

/**
* Returns whether this connection is using savepoints for nested transactions
*
* @return bool true if enabled, false otherwise
*/
public function isEnabledSavePoints()
{
return $this->_useSavePoints;
}

/**
* Returns whether this connection is using savepoints for nested transactions
* If a boolean is passed as argument it will enable/disable the usage of savepoints
Expand All @@ -471,20 +559,17 @@ public function rollback()
* `$connection->useSavePoints(false)` Disables usage of savepoints and returns false
* `$connection->useSavePoints()` Returns current status
*
* @deprecated 3.4.0 Use enableSavePoints()/isEnabledSavePoints() instead.
* @param bool|null $enable Whether or not save points should be used.
* @return bool true if enabled, false otherwise
*/
public function useSavePoints($enable = null)
{
if ($enable === null) {
return $this->_useSavePoints;
}

if ($enable === false) {
return $this->_useSavePoints = false;
if ($enable !== null) {
$this->enableSavePoints($enable);
}

return $this->_useSavePoints = $this->_driver->supportsSavePoints();
return $this->isEnabledSavePoints();
}

/**
Expand Down Expand Up @@ -720,7 +805,7 @@ public function log($sql)
*/
protected function _newLogger(StatementInterface $statement)
{
$log = new LoggingStatement($statement, $this->driver());
$log = new LoggingStatement($statement, $this->getDriver());
$log->logger($this->logger());

return $log;
Expand Down
34 changes: 30 additions & 4 deletions src/Database/Driver.php
Expand Up @@ -296,23 +296,49 @@ public function isConnected()
return $this->_connection !== null;
}

/**
* Sets whether or not this driver should automatically quote identifiers
* in queries.
*
* @param bool $enable Whether to enable auto quoting
* @return $this
*/
public function enableAutoQuoting($enable)
{
$this->_autoQuoting = (bool)$enable;

return $this;
}

/**
* Returns whether or not this driver should automatically quote identifiers
* in queries
*
* @return bool
*/
public function isEnabledAutoQuoting()
{
return $this->_autoQuoting;
}

/**
* Returns whether or not this driver should automatically quote identifiers
* in queries
*
* If called with a boolean argument, it will toggle the auto quoting setting
* to the passed value
*
* @param bool|null $enable whether to enable auto quoting
* @deprecated 3.4.0 use enableAutoQuoting()/isEnabledAutoQuoting() instead.
* @param bool|null $enable Whether to enable auto quoting
* @return bool
*/
public function autoQuoting($enable = null)
{
if ($enable === null) {
return $this->_autoQuoting;
if ($enable !== null) {
$this->enableAutoQuoting($enable);
}

return $this->_autoQuoting = (bool)$enable;
return $this->isEnabledAutoQuoting();
}

/**
Expand Down
31 changes: 27 additions & 4 deletions src/Database/Expression/FunctionExpression.php
Expand Up @@ -73,21 +73,44 @@ public function __construct($name, $params = [], $types = [], $returnType = 'str
parent::__construct($params, $types, ',');
}

/**
* Sets the name of the SQL function to be invoke in this expression.
*
* @param string $name The name of the function
* @return $this
*/
public function setName($name)
{
$this->_name = $name;

return $this;
}

/**
* Gets the name of the SQL function to be invoke in this expression.
*
* @return string
*/
public function getName()
{
return $this->_name;
}

/**
* Sets the name of the SQL function to be invoke in this expression,
* if no value is passed it will return current name
*
* @deprecated 3.4.0 Use setName()/getName() instead.
* @param string|null $name The name of the function
* @return string|$this
*/
public function name($name = null)
{
if ($name === null) {
return $this->_name;
if ($name !== null) {
return $this->setName($name);
}
$this->_name = $name;

return $this;
return $this->getName();
}

/**
Expand Down

0 comments on commit 97f008a

Please sign in to comment.