Skip to content

Commit 82868ce

Browse files
Merge pull request #5 from larapulse/feature/PLD-3_add_options_to_PDOManager
PLD-3 Add custom options attribute to ConnectionManager
2 parents 9fe6370 + e9db0f0 commit 82868ce

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
lines changed

docs/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to `database` will be documented in this file.
44

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

7+
## 2017-12-12
8+
9+
### Added
10+
- Ability to add custom options while connection using `ConnectionManager`
11+
12+
### Removed
13+
- Useless `private` method `setConnection` in `ConnectionManager`
14+
15+
### Fixed
16+
- Code readability
17+
718
## 2017-11-22
819

920
### Deprecated

src/BulkSql/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ final public function execute()
151151

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

157157
$query = $this->buildQuery();
@@ -182,7 +182,7 @@ final public function finish()
182182

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

188188
$this->isFinished = true;

src/ConnectionManager.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,31 @@ final class ConnectionManager
1212
*
1313
* @var TransactionPDO
1414
*/
15-
private $masterConnection = null;
15+
private $masterConnection;
1616

1717
/**
1818
* Slave database connection
1919
*
2020
* @var TransactionPDO|null
2121
*/
22-
private $slaveConnection = null;
22+
private $slaveConnection;
2323

2424
/**
2525
* PDOManager constructor
26-
*
27-
* @param string $name Unique name for connection
28-
* @param array $config Configuration settings for database connection
29-
*/
30-
public function __construct(string $name, array $config)
31-
{
32-
$this->setConnection($name, $config);
33-
}
34-
35-
/**
3626
* Initialize connection to Database for master and slave (if applicable)
3727
*
3828
* @param string $name Unique name for connection
3929
* @param array $config Configuration settings for database connection
30+
* @param array $options Some specific options
4031
*/
41-
private function setConnection(string $name, array $config)
32+
public function __construct(string $name, array $config, array $options = [])
4233
{
4334
$masterConf = $config['master'] ?? $config;
4435
$slaveConf = $config['slave'] ?? [];
4536

46-
$this->masterConnection = Engine::setConnection($name.'_master', $masterConf);
37+
$this->masterConnection = Engine::setConnection($name.'_master', $masterConf, $options);
4738
$this->slaveConnection = $slaveConf
48-
? Engine::setConnection($name.'_slave', $slaveConf)
39+
? Engine::setConnection($name.'_slave', $slaveConf, $options)
4940
: null;
5041
}
5142

src/Driver/Engine.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ final class Engine
3232
/**
3333
* Set connection with database
3434
*
35-
* @param string $name Unique name for connection
36-
* @param array $config Configuration settings for database connection
37-
* @param array $options Some specific options
35+
* @param string $name Unique name for connection
36+
* @param array $config Configuration settings for database connection
37+
* @param array $options Some specific options
3838
*
39+
* @throws \League\Database\Exceptions\InvalidArgumentException
3940
* @return bool|TransactionPDO
4041
*/
4142
public static function setConnection(string $name, array $config, array $options = [])

src/Driver/TransactionPDO.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TransactionPDO extends PDO
1010
/**
1111
* @var array Database drivers that support SAVEPOINT * statements.
1212
*/
13-
protected static $supportedDrivers = ["pgsql", "mysql"];
13+
protected static $supportedDrivers = ['pgsql', 'mysql'];
1414

1515
/**
1616
* @var int Current transaction depth
@@ -24,7 +24,7 @@ class TransactionPDO extends PDO
2424
*/
2525
protected function hasSavepoint() : bool
2626
{
27-
return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME), self::$supportedDrivers);
27+
return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME), self::$supportedDrivers, true);
2828
}
2929

3030
/**

src/Utils/Transaction.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ private static function getConnection($connection = null)
5858
*/
5959
public static function begin($connection = null) : bool
6060
{
61-
return self::getConnection($connection)
62-
? self::getConnection($connection)->beginTransaction()
63-
: false;
61+
return self::getConnection($connection) && self::getConnection($connection)->beginTransaction();
6462
}
6563

6664
/**
@@ -72,36 +70,34 @@ public static function begin($connection = null) : bool
7270
*/
7371
public static function commit($connection = null) : bool
7472
{
75-
return self::getConnection($connection)
76-
? self::getConnection($connection)->commit()
77-
: false;
73+
return self::getConnection($connection) && self::getConnection($connection)->commit();
7874
}
7975

8076
/**
8177
* Rollback transaction on connection
8278
*
8379
* @param string|TransactionPDO $connection
8480
*
81+
* @throws \PDOException
8582
* @return bool
8683
*/
8784
public static function rollback($connection = null) : bool
8885
{
89-
return self::getConnection($connection)
90-
? self::getConnection($connection)->rollBack()
91-
: false;
86+
return self::getConnection($connection) && self::getConnection($connection)->rollBack();
9287
}
9388

9489
/**
9590
* Perform some callback while transaction execution
9691
*
97-
* @param Closure $callback
98-
* @param null $connection
92+
* @param Closure $callback
93+
* @param string|TransactionPDO $connection
9994
*
10095
* @throws LogicException
10196
* @throws TransactionException
10297
*/
10398
public static function perform(Closure $callback, $connection = null)
10499
{
100+
/** @var TransactionPDO|bool $connection */
105101
$connection = self::getConnection($connection);
106102

107103
if ($connection === false) {

0 commit comments

Comments
 (0)