Skip to content

Commit

Permalink
Removed the connection class.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKyle committed Jan 18, 2017
1 parent 7c778a6 commit 7c52c09
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 180 deletions.
18 changes: 2 additions & 16 deletions README.md
Expand Up @@ -5,15 +5,13 @@
[![Maintenance](https://img.shields.io/maintenance/yes/2017.svg)]()
[![Made With Love](https://img.shields.io/badge/Made%20With-Love-green.svg)]()

**Actual Version: 1.2.1**
**Actual Version: 1.3.0**


A DBAL at the most simplest of terms. Its a thin wrapper around PDO, while returning a connected PDO object.

We can connect to multiple database instances of either PGSQL or MYSQL (see below) and create open connections to each.

Note how ever that its your responsibility to close each of the connections when finished. See below for more details.

- Requires PHP 7
- Is Standalone

Expand All @@ -28,7 +26,7 @@ I wanted to understand PDO, and I still have a lot to learn about it. I could ha
But I thought I could build something super simple, super easy to get started with and something that
allowed me to understand exactly how PHP connects to a database.

While this isn't as fully flushed out as a regular DBAL, it is a good step in the process. You open a connection, get a db object back, do your work and then manually close the connection.
While this isn't as fully flushed out as a regular DBAL, it is a good step in the process. You open a connection, get a db object back, do your work and you move on with your life.

There is room for growth here and room for improvement and your feedback and help will help to shape Ice Cream components into a framework.

Expand Down Expand Up @@ -92,15 +90,3 @@ $con->db('pgsql')->exec( ... );
> connection strings to connect to the database in question.
>
> These names are also whats stored in the associated connections manager that manages all connections.
Once you are done with your database transactions you will have to manually close the connection your self:

```php
$con->manager()->closeConnection('mysql'); // or `pgsql`

// Or to close all connections:
$con->manager()->closeAllConnections();

// Both functions will either return true or false if the connections container is empty or the connection
// cannot be found.
```
4 changes: 2 additions & 2 deletions src/Connect.php
Expand Up @@ -95,13 +95,13 @@ protected function getDatabaseHandlers(array $config) {
* This method is only public for testing purposes to be able to mock the method
* and return fake PDO objects. You should enevr call this directly.
*
* @return Array ['driver_name' => \PDO, ...]
* @return Array ['driver_name' => \PDO, ...]
*/
public function getConnections() {
$connections = [];

foreach ($this->_databaseDrivers as $name => $driver) {
$connections[$name] = (new Connection($name . ':' . $driver->connectionString(), $driver->username(), $driver->password()))->connect();
$connections[$name] = new PDO($name . ':' . $driver->connectionString(), $driver->username(), $driver->password());
}

return $connections;
Expand Down
40 changes: 0 additions & 40 deletions src/Connections/Connection.php

This file was deleted.

37 changes: 0 additions & 37 deletions src/Connections/ConnectionManager.php
Expand Up @@ -95,43 +95,6 @@ public function setDefaultConnection(String $name) {
return false;
}

/**
* Close a connection in a list of connections.
*
* Can return false if the connection name cannot be found.
*
* If the connection is a default connection and it is closed the default connection will also be set to null,
* forcing you to set you to set a new connectio as a default connection manually.
*
* @param String name
* @return bool
*/
public function closeConnection(String $name) {
if (isset($this->_connections[$name])) {
$this->_connections[$name] = null;

if (isset($this->_defaultConnection[$name])) {
$this->_defaultConnection = null;
}

return true;
}

return false;
}

/**
* Closes all connections and sets default connection to null.
*/
public function closeAllConnections() {
forEach($this->_connections as $name => $pdoConnection) {
$this->_connections[$name] = null;
}

$this->_defaultConnection = null;
}


protected function storeAllContections(array $connections) {
foreach ($connections as $name => $connection) {
if (!$connection instanceof PDO) {
Expand Down
85 changes: 0 additions & 85 deletions tests/connections/ConnectionManagerTest.php
Expand Up @@ -61,52 +61,6 @@ public function testShouldReturnFalseSetDefaultConnection() {
$this->assertFalse($cm->setDefaultConnection('sdasdsa'));
}

public function testShouldReturnFalseForClosingAConnection() {
$connections = [
'mysql' => $this->_pdo,
'pgsql' => $this->_pdo,
];

$cm = new ConnectionManager($connections);

$this->assertFalse($cm->closeConnection('sdasdsa'));
}

public function testShouldReturnTrueForClosingAConnection() {
$connections = [
'mysql' => $this->_pdo,
'pgsql' => $this->_pdo,
];

$cm = new ConnectionManager($connections);

$this->assertTrue($cm->closeConnection('mysql'));
}

public function testShouldReturnNullForAClosedConnection() {
$connections = [
'mysql' => $this->_pdo,
'pgsql' => $this->_pdo,
];

$cm = new ConnectionManager($connections);
$cm->closeConnection('mysql');

$this->assertNull($cm->getConnection('mysql'));
}

public function testShouldReturnNullForAllClosedConnection() {
$connections = [
'mysql' => $this->_pdo,
'pgsql' => $this->_pdo,
];

$cm = new ConnectionManager($connections);
$cm->closeAllConnections();

$this->assertNull($cm->getConnection('mysql'));
}

public function testShouldReturnInstanceOfDefaultPdo() {
$connections = [
'mysql' => $this->_pdo,
Expand Down Expand Up @@ -141,43 +95,4 @@ public function testShoudNotSetNewDefaultConnection() {
$this->assertFalse($cm->setDefaultConnection('888'));
$this->assertInstanceOf(\PDO::class, $cm->getConnection());
}

public function testShouldCloseConnection() {
$connections = [
'mysql' => $this->_pdo,
'pgsql' => $this->_pdo,
];

$cm = new ConnectionManager($connections);

$this->assertTrue($cm->closeConnection('mysql'));
$this->assertNull($cm->getConnection('mysql'));
}

public function testShouldNotCloseConnection() {
$connections = [
'mysql' => $this->_pdo,
'pgsql' => $this->_pdo,
];

$cm = new ConnectionManager($connections);

$this->assertFalse($cm->closeConnection('adsdsad'));
$this->assertNotNull($cm->getConnection('mysql'));
}

public function testShouldCloseAllConnections() {
$connections = [
'mysql' => $this->_pdo,
'pgsql' => $this->_pdo,
];

$cm = new ConnectionManager($connections);

$cm->closeAllConnections();

$this->assertNull($cm->getConnection('mysql')); // Named
$this->assertNull($cm->getConnection('pgsql')); // Named
$this->assertNull($cm->getConnection()); // default
}
}

0 comments on commit 7c52c09

Please sign in to comment.