-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganized, covered, and documented
- Loading branch information
1 parent
8c4873e
commit 3100f1e
Showing
6 changed files
with
240 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# minphp/Db | ||
|
||
[![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) | ||
|
||
Database Connection Library. | ||
|
||
Efficiently manages a connection, preventing a new one from being established if a matching connection already exists. | ||
|
||
## Installation | ||
|
||
Install via composer: | ||
|
||
```sh | ||
composer require minphp/db:dev-master | ||
``` | ||
|
||
## Basic Usage | ||
|
||
```php | ||
use minphp\Db\PdoConnection; | ||
|
||
$dbInfo = array( | ||
'driver' => 'mysql', | ||
'host' => 'localhost', | ||
'database' => 'databasename', | ||
'user' => 'user', | ||
'pass' => 'pass' | ||
); | ||
|
||
$connection = new PdoConnection($dbInfo); | ||
$connection->query('SELECT * FROM table WHERE id=?', 1); | ||
``` | ||
|
||
### Explicitly Connecting | ||
|
||
By default, PdoConnection will only connect to the database when a connection is required. To explicitly connect to the database use `connect()`: | ||
|
||
```php | ||
use minphp\Db\PdoConnection; | ||
|
||
$dbInfo = array( | ||
'driver' => 'mysql', | ||
'host' => 'localhost', | ||
'database' => 'databasename', | ||
'user' => 'user', | ||
'pass' => 'pass' | ||
); | ||
|
||
$connection = new PdoConnection($dbInfo); | ||
$connection->connect(); | ||
// Connection now ready and waiting | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace minphp\Db; | ||
|
||
class SqliteConnection extends PdoConnection | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function makeDsn(array $db) | ||
{ | ||
return 'sqlite:' . $db['database']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
namespace minphp\Db\Tests\Integration; | ||
|
||
use minphp\Db\PdoConnection; | ||
use minphp\Db\SqliteConnection; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* @coversDefaultClass minphp\Db\PdoConnection | ||
*/ | ||
class PdoConnectionTest extends PHPUnit_Framework_TestCase | ||
{ | ||
private function getDbInfo() | ||
{ | ||
return array( | ||
'database' => ':memory:' | ||
); | ||
} | ||
|
||
/** | ||
* @covers ::connect | ||
* @covers ::__construct | ||
* @covers ::setConnection | ||
* @covers ::getConnection | ||
* @covers \minphp\Db\SqliteConnection::makeDsn | ||
* @covers ::makeConnection | ||
* @covers ::query | ||
* @covers ::prepare | ||
*/ | ||
public function testConnect() | ||
{ | ||
$connection = new SqliteConnection($this->getDbInfo()); | ||
$this->assertInstanceOf('\PDO', $connection->connect()); | ||
} | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::reuseConnection | ||
* @covers ::getConnection | ||
* @covers ::setConnection | ||
* @covers ::connect | ||
* @covers \minphp\Db\SqliteConnection::makeDsn | ||
* @covers ::makeConnection | ||
*/ | ||
public function testReuseConnection() | ||
{ | ||
$dbInfo = $this->getDbInfo(); | ||
|
||
$connectionA = new SqliteConnection($dbInfo); | ||
$pdoA = $connectionA->connect(); | ||
|
||
$connectionB = new SqliteConnection($dbInfo); | ||
$pdoB = $connectionB->reuseConnection(true)->connect(); | ||
|
||
$connectionC = new SqliteConnection($dbInfo); | ||
$pdoC = $connectionC->reuseConnection(false)->connect(); | ||
|
||
$this->assertSame($pdoA, $pdoB); | ||
$this->assertNotSame($pdoA, $pdoC); | ||
} | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::connect | ||
* @covers ::getConnection | ||
* @covers ::makeDsn | ||
* @covers ::makeConnection | ||
* @expectedException \RuntimeException | ||
*/ | ||
public function testConnectException() | ||
{ | ||
$dbInfo = array( | ||
'driver' => 'invalid', | ||
'host' => 'invalid', | ||
'database' => 'invalid', | ||
'charset_query' => "SET NAMES 'utf8'" | ||
); | ||
|
||
$connection = new PdoConnection($dbInfo); | ||
$connection->connect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.