Skip to content

Commit

Permalink
Update in order to produce a v1
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Jan 2, 2016
1 parent f1bafc1 commit dbcbc13
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 43 deletions.
72 changes: 54 additions & 18 deletions README.md
Expand Up @@ -11,16 +11,26 @@

## Introduction

Extra minimalist PHP database wrapper.
Minimalist modern PHP database wrapper.

## Features

- Currently only abstract pdo_mysql / mysqli drivers with minimal functions.
- Small, fast and modern database abstraction layer.
- Provides mysqli, pdo_mysql, pdo_sqlite driver implementations.
- Thoroughly tested and documented.
- Adhere to soluble standards.

## Requirements

- PHP engine 5.4+, 7.0+ or HHVM >= 3.2.
- PHP extensions pfo, pdo_mysql and mysqli.

## Motivations

Initially the reason behind the development of `soluble/dbwrapper` was to get
a reliable, modern and lightweight library to abstract the `PDO_mysql` and `mysqli` driver interfaces.

*If you are looking for a more complete library with extra drivers and a SQL abstraction,
take a look at the excellent [`zendframework/zend-db`](https://github.com/zendframework/zend-db) package.*

## Installation

Expand Down Expand Up @@ -53,11 +63,12 @@ $conn = new \PDO("mysql:host=$hostname", $username, $password, [
]);

try {
$adapter = DbWrapperAdapterFactory::createFromConnection($conn);
$adapter = DbWrapperAdapterFactory::createAdapterFromResource($conn);
} catch (DbWrapper\Exception\UnsupportedDriverException $e) {
// ...
} catch (DbWrapper\Exception\InvalidArgumentException $e) {
// ...
}

```

Create an adapter from an existing Mysqli connection
Expand All @@ -70,18 +81,37 @@ use Soluble\DbWrapper;
$conn = new \mysqli($hostname,$username,$password,$database);
$conn->set_charset($charset);

try {
$adapter = DbWrapper\AdapterFactory::createFromConnection($conn);
} catch (DbWrapper\Exception\InvalidArgumentException $e) {
// ...
}
$adapter = DbWrapper\AdapterFactory::createAdapterFromResource($conn);

```

### Querying database

Execute

```php
<?php
$results = $adapter->query("select * from my_table");
foreach($results as $result) {
echo $result['my_column'];
}
```

### API methods

Once a `DbWrapper\Adapter\AdapterInterface is intitalized, you have access to the following methods
## API methods

### AdapterFactory

The `DbWrapper\AdapterFactory` allows to instanciate an Adapter from en existing connection link or resource.

| Methods | Return | Comment |
|-----------------------------------------------|--------------------|-------------------------------------|
| static `createAdapterFromResource($resource)` | `AdapterInterface` | Create an adapter from existing resource |


### AdapterInterface

Once a `DbWrapper\Adapter\AdapterInterface` is intitalized, you have access to the following methods

| Methods | Return | Description |
|---------------------------------|---------------|-----------------------------------------------|
Expand All @@ -91,6 +121,15 @@ Once a `DbWrapper\Adapter\AdapterInterface is intitalized, you have access to th
| `getCurrentSchema()` | `string|false`| Return current schema |
| `getResource()` | `mixed` | Return internal connection (pdo, mysqli...) |

### Resultset

The `DbWrapper\Result\Resultset` is can be easily iterated through a simple foreach loop.
Additionnaly you can call the following methods :

| Methods | Return | Description |
|---------------------------------|---------------|-----------------------------------------------|
| `count()` | `int` | Count the number of results |


## Supported drivers

Expand All @@ -99,21 +138,18 @@ Currently only pdo_mysql and mysqli drivers are supported.
| Drivers | DbWrapper\Adapter\AdapterInterface implementations |
|--------------------|------------------------------------------------------|
| pdo_mysql | `Soluble\DbWrapper\Adapter\PdoMysqlAdapter` |
| pdo_sqlite | `Soluble\DbWrapper\Adapter\PdoSqliteAdapter` |
| mysqli | `Soluble\DbWrapper\Adapter\MysqliAdapter` |

You can easily add new drivers by implementing the `DbWrapper\Adapter\AdapterInterface`.

## Contributing

Contribution are welcome see [contribution guide](./CONTRIBUTING.md)
Contribution and pull request are more than welcome, see the [contribution guide](./CONTRIBUTING.md)

## Coding standards

* [PSR 4 Autoloader](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md)
* [PSR 2 Coding Style Guide](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
* [PSR 1 Coding Standards](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md)
* [PSR 0 Autoloading standards](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)





2 changes: 1 addition & 1 deletion phpunit.xml.dist
Expand Up @@ -24,7 +24,7 @@

<testsuites>
<testsuite name="soluble/dbwrapper">
<directory>./test/src/Soluble/DbWrapper</directory>
<directory>./test/src/SolubleTest/DbWrapper</directory>
</testsuite>
</testsuites>

Expand Down
41 changes: 41 additions & 0 deletions src/Soluble/DbWrapper/Adapter/PdoSqliteAdapter.php
@@ -0,0 +1,41 @@
<?php

namespace Soluble\DbWrapper\Adapter;

use Soluble\DbWrapper\Exception;
use Soluble\DbWrapper\Adapter\Pdo\GenericPdo;
use PDO;

class PdoSqliteAdapter extends GenericPdo implements AdapterInterface
{

/**
*
* @var \PDO
*/
protected $resource;


/**
* Constructor
*
* @throws Exception\InvalidArgumentException
* @param \PDO $connection
*/
public function __construct(PDO $connection)
{
if ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME) != 'sqlite') {
$msg = __CLASS__ . " requires pdo connection to be 'sqlite'";
throw new Exception\InvalidArgumentException($msg);
}
$this->resource = $connection;
}

/**
* {@inheritdoc}
*/
public function getCurrentSchema()
{
throw new \Exception('nope');
}
}
53 changes: 37 additions & 16 deletions src/Soluble/DbWrapper/AdapterFactory.php
Expand Up @@ -6,28 +6,49 @@ class AdapterFactory
{

/**
* Get an adapter from an existing connection
* Create adapter from an existing connection resource
*
* @param \PDO|\mysqli $connection database connection object
* @param mixed $resource database connection object (mysqli, pdo_mysql,...)
* @throws Exception\InvalidArgumentException
* @throws Exception\UnsupportedDriverException
* @return Adapter\AdapterInterface
*/
public static function createAdapterFromConnection($connection)
public static function createAdapterFromResource($resource)
{
if ($connection instanceof \PDO) {
switch ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
case 'mysql':
$adapter = new Adapter\PdoMysqlAdapter($connection);
break;
default:
$msg = "Currently only support 'pdo_mysql' connection";
throw new Exception\InvalidArgumentException($msg);
}
} elseif ($connection instanceof \mysqli) {
$adapter = new Adapter\MysqliAdapter($connection);
if (is_scalar($resource) || is_array($resource)) {
throw new Exception\InvalidArgumentException("Resource param must be a valid 'resource' link (mysqli, pdo)");
} if ($resource instanceof \PDO) {
$adapter = self::getAdapterFromPdo($resource);
} elseif (extension_loaded('mysqli') && $resource instanceof \mysqli) {
$adapter = new Adapter\MysqliAdapter($resource);
} elseif (is_object($resource)) {
$class = get_class($resource);
$msg = "Resource connection '$class' is either unsupported or php extension not enabled.";
throw new Exception\UnsupportedDriverException($msg);
} else {
$msg = "Currently only support 'pdo' or 'mysqli' connections";
throw new Exception\InvalidArgumentException($msg);
throw new Exception\InvalidArgumentException("Resource must be a valid connection link, like PDO or mysqli");
}
return $adapter;
}


/**
* Get an adapter from an existing connection resource
*
* @param \PDO $resource database connection object
* @throws Exception\UnsupportedDriverException
* @return Adapter\AdapterInterface
*/
protected static function getAdapterFromPdo(\PDO $resource) {

$driver = $resource->getAttribute(\PDO::ATTR_DRIVER_NAME);
switch ($driver) {
case 'mysql':
$adapter = new Adapter\PdoMysqlAdapter($resource);
break;
default:
$msg = "Driver 'PDO_$driver' is not currently supported.";
throw new Exception\UnsupportedDriverException($msg);
}
return $adapter;
}
Expand Down
@@ -0,0 +1,7 @@
<?php

namespace Soluble\DbWrapper\Exception;

class UnsupportedDriverException extends \DomainException implements ExceptionInterface
{
}
2 changes: 1 addition & 1 deletion test/bootstrap.php
Expand Up @@ -11,5 +11,5 @@
require_once(dirname(__FILE__) . '/SolubleTestFactories.php');

$loader = require __DIR__ . '/../vendor/autoload.php';
$loader->add('Soluble', array($baseDir . '/src/', $baseDir . '/test/'));
$loader->add('SolubleTest', array($baseDir . '/src/', $baseDir . '/test/'));
$loader->register();
@@ -1,14 +1,16 @@
<?php

namespace Soluble\DbWrapper;
namespace SolubleTest\DbWrapper;

use Soluble\DbWrapper\AdapterFactory;

class AdapterFactoryTest extends \PHPUnit_Framework_TestCase
{

public function testCreateAdapterFromMysqliConnection()
{
$conn = \SolubleTestFactories::getDbConnection('mysqli');
$adapter = AdapterFactory::createAdapterFromConnection($conn);
$adapter = AdapterFactory::createAdapterFromResource($conn);
$this->assertInstanceOf('\Soluble\DbWrapper\Adapter\AdapterInterface', $adapter);
$this->assertInstanceOf('\Soluble\DbWrapper\Adapter\MysqliAdapter', $adapter);
}
Expand All @@ -17,15 +19,15 @@ public function testCreateAdapterFromMysqliConnection()
public function testCreateAdapterFromPDOMysqlConnection()
{
$conn = \SolubleTestFactories::getDbConnection('pdo:mysql');
$adapter = AdapterFactory::createAdapterFromConnection($conn);
$adapter = AdapterFactory::createAdapterFromResource($conn);
$this->assertInstanceOf('\Soluble\DbWrapper\Adapter\AdapterInterface', $adapter);
$this->assertInstanceOf('\Soluble\DbWrapper\Adapter\PdoMysqlAdapter', $adapter);
}

public function testCreateAdapterThrowsException()
{
$this->setExpectedException('Soluble\DbWrapper\Exception\InvalidArgumentException');
$this->setExpectedException('Soluble\DbWrapper\Exception\UnsupportedDriverException');
$connection = new \PDO('sqlite::memory:');
$adapter = AdapterFactory::createAdapterFromConnection($connection);
$adapter = AdapterFactory::createAdapterFromResource($connection);
}
}
@@ -1,6 +1,8 @@
<?php

namespace Soluble\DbWrapper\Adapter;
namespace SolubleTest\DbWrapper\Adapter;

use Soluble\DbWrapper\Adapter\MysqliAdapter;

class MysqliAdapterTest extends \PHPUnit_Framework_TestCase
{
Expand Down
@@ -1,6 +1,8 @@
<?php

namespace Soluble\DbWrapper\Adapter;
namespace SolubleTest\DbWrapper\Adapter;

use Soluble\DbWrapper\Adapter\PdoMysqlAdapter;

class PdoMysqlAdapterTest extends \PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit dbcbc13

Please sign in to comment.