Skip to content

Commit 3100f1e

Browse files
committed
reorganized, covered, and documented
1 parent 8c4873e commit 3100f1e

File tree

6 files changed

+240
-74
lines changed

6 files changed

+240
-74
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# minphp/Db
2+
3+
[![Build Status](https://travis-ci.org/phillipsdata/minphp-db.svg?banch=master)](https://travis-ci.org/phillipsdata/minphp-db) [![Coverage Status](https://coveralls.io/repos/phillipsdata/minphp-db/badge.svg)](https://coveralls.io/r/phillipsdata/minphp-db)
4+
5+
Database Connection Library.
6+
7+
Efficiently manages a connection, preventing a new one from being established if a matching connection already exists.
8+
9+
## Installation
10+
11+
Install via composer:
12+
13+
```sh
14+
composer require minphp/db:dev-master
15+
```
16+
17+
## Basic Usage
18+
19+
```php
20+
use minphp\Db\PdoConnection;
21+
22+
$dbInfo = array(
23+
'driver' => 'mysql',
24+
'host' => 'localhost',
25+
'database' => 'databasename',
26+
'user' => 'user',
27+
'pass' => 'pass'
28+
);
29+
30+
$connection = new PdoConnection($dbInfo);
31+
$connection->query('SELECT * FROM table WHERE id=?', 1);
32+
```
33+
34+
### Explicitly Connecting
35+
36+
By default, PdoConnection will only connect to the database when a connection is required. To explicitly connect to the database use `connect()`:
37+
38+
```php
39+
use minphp\Db\PdoConnection;
40+
41+
$dbInfo = array(
42+
'driver' => 'mysql',
43+
'host' => 'localhost',
44+
'database' => 'databasename',
45+
'user' => 'user',
46+
'pass' => 'pass'
47+
);
48+
49+
$connection = new PdoConnection($dbInfo);
50+
$connection->connect();
51+
// Connection now ready and waiting
52+
```

src/PdoConnection.php

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use PDO;
55
use PDOStatement;
6-
use PDOException;
76
use RuntimeException;
87
use InvalidArgumentException;
98

@@ -63,14 +62,22 @@ class PdoConnection
6362
* the given database info, or the default configured info set in the database
6463
* config file if no info is given
6564
*
66-
* @param array $dbInfo Database information for this connection
65+
* @param array $dbInfo Database information for this connection:
66+
* - driver DB Driver
67+
* - host DB Host
68+
* - database DB name
69+
* - user DB User
70+
* - pass DB Password
71+
* - port DB Port
72+
* - options Array of PDO connection options
6773
*/
68-
public function __construct(array $dbInfo = null) {
74+
public function __construct(array $dbInfo)
75+
{
6976
$this->dbInfo = $dbInfo;
7077
}
7178

7279
/**
73-
* Attemps to initialize a connection to the database if one does not already exist.
80+
* Attemps to initialize a connection to the database if one does not already exist
7481
*
7582
* @return PDO The PDO connection
7683
*/
@@ -80,16 +87,49 @@ public function connect()
8087
if ($connection instanceof PDO) {
8188
return $connection;
8289
}
83-
return $this->makeConnection($this->dbInfo);
90+
91+
// Attempt to reuse an existing connection if one exists that matches this connection
92+
if ($this->reuseConnection
93+
&& ($key = array_search($this->dbInfo, self::$dbInfos)) !== false
94+
) {
95+
$connection = self::$connections[$key];
96+
$this->setConnection($connection);
97+
return $connection;
98+
}
99+
100+
$dsn = $this->makeDsn($this->dbInfo);
101+
$username = isset($this->dbInfo['user'])
102+
? $this->dbInfo['user']
103+
: null;
104+
$password = isset($this->dbInfo['pass'])
105+
? $this->dbInfo['pass']
106+
: null;
107+
$options = (array) (
108+
isset($this->dbInfo['options'])
109+
? $this->dbInfo['options']
110+
: null
111+
)
112+
+ $this->options;
113+
114+
$connection = $this->makeConnection($dsn, $username, $password, $options);
115+
116+
self::$connections[] = $connection;
117+
self::$dbInfos[] = $this->dbInfo;
118+
$this->setConnection($connection);
119+
120+
return $connection;
84121
}
85122

86123
/**
124+
* Set whether or not to reuse an existing connection
87125
*
88126
* @param boolean $enable True to reuse an existing matching connection if available
127+
* @return PdoConnection
89128
*/
90129
public function reuseConnection($enable)
91130
{
92131
$this->reuseConnection = $enable;
132+
return $this;
93133
}
94134

95135
/**
@@ -120,10 +160,12 @@ public function lastInsertId($name = null)
120160
*
121161
* @param long $attribute The attribute to set
122162
* @param int $value The value to assign to the attribute
163+
* @return PdoConnection
123164
*/
124165
public function setAttribute($attribute, $value)
125166
{
126167
$this->connect()->setAttribute($attribute, $value);
168+
return $this;
127169
}
128170

129171
/**
@@ -223,20 +265,12 @@ public function getConnection()
223265
* Set the PDO connection to use
224266
*
225267
* @param PDO $connection
268+
* @return PdoConnection
226269
*/
227270
public function setConnection(PDO $connection)
228271
{
229272
$this->connection = $connection;
230-
}
231-
232-
/**
233-
* Return all registered connections available
234-
*
235-
* @return array
236-
*/
237-
public function getRegisteredConnections()
238-
{
239-
return $this->connections;
273+
return $this;
240274
}
241275

242276
/**
@@ -275,71 +309,23 @@ public function makeDsn(array $db)
275309
);
276310
}
277311

278-
return $db['driver'] . ":dbname=" . $db['database'] . ";host=" . $db['host']
279-
. (
280-
isset($db['port'])
281-
? ";port=" . $db['port']
282-
: ""
283-
);
312+
return isset($db['port'])
313+
? $db['driver'] . ':host=' . $db['host'] . ';dbname=' . $db['database'] . ';port=' . $db['port']
314+
: $db['driver'] . ':host=' . $db['host'] . ';dbname=' . $db['database'];
284315
}
285316

286317
/**
287318
* Establish a new PDO connection using the given array of information. If
288319
* a connection already exists, no new connection will be created.
289320
*
290-
* @param array $dbInfo Database information for this connection
321+
* @param string $dsn
322+
* @param string $username
323+
* @param string $password
324+
* @param array $options
291325
* @return \PDO The connection
292-
* @throws RuntimeException Throw when PDOException is encountered
293326
*/
294-
private function makeConnection(array $dbInfo)
327+
private function makeConnection($dsn, $username, $password, $options)
295328
{
296-
// Attempt to reuse an existing connection if one exists that matches this connection
297-
if ($this->reuseConnection
298-
&& ($key = array_search($dbInfo, self::$dbInfos)) !== false
299-
) {
300-
$this->setConnection(self::$connections[$key]);
301-
return $this->getConnection();
302-
}
303-
304-
// Override any default settings with those provided
305-
$options = (array) (
306-
isset($dbInfo['options'])
307-
? $dbInfo['options']
308-
: null
309-
)
310-
+ $this->options;
311-
312-
try {
313-
$this->setConnection(new PDO(
314-
$this->makeDsn($dbInfo),
315-
(
316-
isset($dbInfo['user'])
317-
? $dbInfo['user']
318-
: null
319-
),
320-
(
321-
isset($dbInfo['pass'])
322-
? $dbInfo['pass']
323-
: null
324-
),
325-
$options
326-
));
327-
$connection = $this->getConnection();
328-
329-
// Record the connection
330-
self::$connections[] = $connection;
331-
self::$dbInfos[] = $dbInfo;
332-
333-
// Run a character set query to override the database server's default character set
334-
if (!empty($dbInfo['charset_query'])) {
335-
$this->query($dbInfo['charset_query']);
336-
}
337-
return $connection;
338-
339-
} catch (PDOException $e) {
340-
throw new RuntimeException($e->getMessage());
341-
}
342-
343-
throw new RuntimeException('Connection could not be established.');
329+
return new PDO($dsn, $username, $password, $options);
344330
}
345331
}

src/SqliteConnection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace minphp\Db;
3+
4+
class SqliteConnection extends PdoConnection
5+
{
6+
/**
7+
* {@inheritdoc}
8+
*/
9+
public function makeDsn(array $db)
10+
{
11+
return 'sqlite:' . $db['database'];
12+
}
13+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
namespace minphp\Db\Tests\Integration;
3+
4+
use minphp\Db\PdoConnection;
5+
use minphp\Db\SqliteConnection;
6+
use PHPUnit_Framework_TestCase;
7+
8+
/**
9+
* @coversDefaultClass minphp\Db\PdoConnection
10+
*/
11+
class PdoConnectionTest extends PHPUnit_Framework_TestCase
12+
{
13+
private function getDbInfo()
14+
{
15+
return array(
16+
'database' => ':memory:'
17+
);
18+
}
19+
20+
/**
21+
* @covers ::connect
22+
* @covers ::__construct
23+
* @covers ::setConnection
24+
* @covers ::getConnection
25+
* @covers \minphp\Db\SqliteConnection::makeDsn
26+
* @covers ::makeConnection
27+
* @covers ::query
28+
* @covers ::prepare
29+
*/
30+
public function testConnect()
31+
{
32+
$connection = new SqliteConnection($this->getDbInfo());
33+
$this->assertInstanceOf('\PDO', $connection->connect());
34+
}
35+
36+
/**
37+
* @covers ::__construct
38+
* @covers ::reuseConnection
39+
* @covers ::getConnection
40+
* @covers ::setConnection
41+
* @covers ::connect
42+
* @covers \minphp\Db\SqliteConnection::makeDsn
43+
* @covers ::makeConnection
44+
*/
45+
public function testReuseConnection()
46+
{
47+
$dbInfo = $this->getDbInfo();
48+
49+
$connectionA = new SqliteConnection($dbInfo);
50+
$pdoA = $connectionA->connect();
51+
52+
$connectionB = new SqliteConnection($dbInfo);
53+
$pdoB = $connectionB->reuseConnection(true)->connect();
54+
55+
$connectionC = new SqliteConnection($dbInfo);
56+
$pdoC = $connectionC->reuseConnection(false)->connect();
57+
58+
$this->assertSame($pdoA, $pdoB);
59+
$this->assertNotSame($pdoA, $pdoC);
60+
}
61+
62+
/**
63+
* @covers ::__construct
64+
* @covers ::connect
65+
* @covers ::getConnection
66+
* @covers ::makeDsn
67+
* @covers ::makeConnection
68+
* @expectedException \RuntimeException
69+
*/
70+
public function testConnectException()
71+
{
72+
$dbInfo = array(
73+
'driver' => 'invalid',
74+
'host' => 'invalid',
75+
'database' => 'invalid',
76+
'charset_query' => "SET NAMES 'utf8'"
77+
);
78+
79+
$connection = new PdoConnection($dbInfo);
80+
$connection->connect();
81+
}
82+
}

tests/Unit/PdoConnectionTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class PdoConnectionTest extends PHPUnit_Framework_TestCase
1111
{
1212
public function setUp()
1313
{
14-
$this->connection = new PdoConnection();
14+
$this->connection = new PdoConnection(array());
1515
}
1616

1717
/**
@@ -211,7 +211,7 @@ public function testMakeDsn()
211211
'host' => 'localhost',
212212
'port' => '8889'
213213
);
214-
$expected = 'mysql:dbname=database;host=localhost;port=8889';
214+
$expected = 'mysql:host=localhost;dbname=database;port=8889';
215215

216216
$this->assertEquals($expected, $this->connection->makeDsn($info));
217217
}
@@ -225,4 +225,13 @@ public function testMakeDsnException()
225225
{
226226
$this->connection->makeDsn(array());
227227
}
228+
229+
/**
230+
* @covers ::reuseConnection
231+
* @covers ::__construct
232+
*/
233+
public function testReuseConnection()
234+
{
235+
$this->assertSame($this->connection->reuseConnection(true), $this->connection);
236+
}
228237
}

0 commit comments

Comments
 (0)