Skip to content

Commit

Permalink
Merge pull request #5 from larapulse/feature/PLD-3_add_options_to_PDO…
Browse files Browse the repository at this point in the history
…Manager

PLD-3 Add custom options attribute to ConnectionManager
  • Loading branch information
SergeyPodgornyy authored Dec 12, 2017
2 parents 9fe6370 + e9db0f0 commit 82868ce
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
11 changes: 11 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to `database` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 2017-12-12

### Added
- Ability to add custom options while connection using `ConnectionManager`

### Removed
- Useless `private` method `setConnection` in `ConnectionManager`

### Fixed
- Code readability

## 2017-11-22

### Deprecated
Expand Down
4 changes: 2 additions & 2 deletions src/BulkSql/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ final public function execute()

// On first query check disable indeces
if ($this->isIndexesDisabled() && $this->affectedCount === 0) {
$this->getDbConnection()->exec('ALTER TABLE ' . $this->getTable() . ' DISABLE KEYS');
$this->getDbConnection()->exec("ALTER TABLE {$this->getTable()} DISABLE KEYS");
}

$query = $this->buildQuery();
Expand Down Expand Up @@ -182,7 +182,7 @@ final public function finish()

// Re-enable indexes
if ($this->isIndexesDisabled()) {
$this->getDbConnection()->exec('ALTER TABLE ' . $this->getTable() . ' ENABLE KEYS');
$this->getDbConnection()->exec("ALTER TABLE {$this->getTable()} ENABLE KEYS");
}

$this->isFinished = true;
Expand Down
21 changes: 6 additions & 15 deletions src/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,31 @@ final class ConnectionManager
*
* @var TransactionPDO
*/
private $masterConnection = null;
private $masterConnection;

/**
* Slave database connection
*
* @var TransactionPDO|null
*/
private $slaveConnection = null;
private $slaveConnection;

/**
* PDOManager constructor
*
* @param string $name Unique name for connection
* @param array $config Configuration settings for database connection
*/
public function __construct(string $name, array $config)
{
$this->setConnection($name, $config);
}

/**
* Initialize connection to Database for master and slave (if applicable)
*
* @param string $name Unique name for connection
* @param array $config Configuration settings for database connection
* @param array $options Some specific options
*/
private function setConnection(string $name, array $config)
public function __construct(string $name, array $config, array $options = [])
{
$masterConf = $config['master'] ?? $config;
$slaveConf = $config['slave'] ?? [];

$this->masterConnection = Engine::setConnection($name.'_master', $masterConf);
$this->masterConnection = Engine::setConnection($name.'_master', $masterConf, $options);
$this->slaveConnection = $slaveConf
? Engine::setConnection($name.'_slave', $slaveConf)
? Engine::setConnection($name.'_slave', $slaveConf, $options)
: null;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Driver/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ final class Engine
/**
* Set connection with database
*
* @param string $name Unique name for connection
* @param array $config Configuration settings for database connection
* @param array $options Some specific options
* @param string $name Unique name for connection
* @param array $config Configuration settings for database connection
* @param array $options Some specific options
*
* @throws \League\Database\Exceptions\InvalidArgumentException
* @return bool|TransactionPDO
*/
public static function setConnection(string $name, array $config, array $options = [])
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/TransactionPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TransactionPDO extends PDO
/**
* @var array Database drivers that support SAVEPOINT * statements.
*/
protected static $supportedDrivers = ["pgsql", "mysql"];
protected static $supportedDrivers = ['pgsql', 'mysql'];

/**
* @var int Current transaction depth
Expand All @@ -24,7 +24,7 @@ class TransactionPDO extends PDO
*/
protected function hasSavepoint() : bool
{
return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME), self::$supportedDrivers);
return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME), self::$supportedDrivers, true);
}

/**
Expand Down
18 changes: 7 additions & 11 deletions src/Utils/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ private static function getConnection($connection = null)
*/
public static function begin($connection = null) : bool
{
return self::getConnection($connection)
? self::getConnection($connection)->beginTransaction()
: false;
return self::getConnection($connection) && self::getConnection($connection)->beginTransaction();
}

/**
Expand All @@ -72,36 +70,34 @@ public static function begin($connection = null) : bool
*/
public static function commit($connection = null) : bool
{
return self::getConnection($connection)
? self::getConnection($connection)->commit()
: false;
return self::getConnection($connection) && self::getConnection($connection)->commit();
}

/**
* Rollback transaction on connection
*
* @param string|TransactionPDO $connection
*
* @throws \PDOException
* @return bool
*/
public static function rollback($connection = null) : bool
{
return self::getConnection($connection)
? self::getConnection($connection)->rollBack()
: false;
return self::getConnection($connection) && self::getConnection($connection)->rollBack();
}

/**
* Perform some callback while transaction execution
*
* @param Closure $callback
* @param null $connection
* @param Closure $callback
* @param string|TransactionPDO $connection
*
* @throws LogicException
* @throws TransactionException
*/
public static function perform(Closure $callback, $connection = null)
{
/** @var TransactionPDO|bool $connection */
$connection = self::getConnection($connection);

if ($connection === false) {
Expand Down

0 comments on commit 82868ce

Please sign in to comment.