Skip to content

Commit

Permalink
Added getHost() method
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Jan 2, 2016
1 parent fb8d745 commit ff1e545
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -138,6 +138,7 @@ The `DbWrapper\Connection\ConnectionInterface` provides information about your c
|--------------------------|---------------|-----------------------------------------------|
| `getCurrentSchema()` | `string|false`| Return current schema |
| `getResource()` | `mixed` | Return internal connection (pdo, mysqli...) |
| `getHost()` | `string` | Return server hostname or IP |


## Supported drivers
Expand Down
6 changes: 3 additions & 3 deletions src/Soluble/DbWrapper/Adapter/MysqliAdapter.php
Expand Up @@ -5,7 +5,7 @@
use Soluble\DbWrapper\Exception;
use mysqli;
use Soluble\DbWrapper\Result\Resultset;
use Soluble\DbWrapper\Connection\MysqlConnection;
use Soluble\DbWrapper\Connection\MysqliConnection;

class MysqliAdapter implements AdapterInterface
{
Expand All @@ -18,7 +18,7 @@ class MysqliAdapter implements AdapterInterface

/**
*
* @var MysqlConnection
* @var MysqliConnection
*/
protected $connection;

Expand All @@ -31,7 +31,7 @@ class MysqliAdapter implements AdapterInterface
public function __construct(mysqli $resource)
{
$this->resource = $resource;
$this->connection = new MysqlConnection($this, $resource);
$this->connection = new MysqliConnection($this, $resource);
}


Expand Down
6 changes: 3 additions & 3 deletions src/Soluble/DbWrapper/Adapter/PdoMysqlAdapter.php
Expand Up @@ -4,7 +4,7 @@

use Soluble\DbWrapper\Exception;
use Soluble\DbWrapper\Adapter\Pdo\GenericPdo;
use Soluble\DbWrapper\Connection\MysqlConnection;
use Soluble\DbWrapper\Connection\PdoMysqlConnection;
use PDO;

class PdoMysqlAdapter extends GenericPdo implements AdapterInterface
Expand All @@ -18,7 +18,7 @@ class PdoMysqlAdapter extends GenericPdo implements AdapterInterface

/**
*
* @var MysqlConnection
* @var PdoMysqlConnection
*/
protected $connection;

Expand All @@ -37,7 +37,7 @@ public function __construct(PDO $resource)
throw new Exception\InvalidArgumentException($msg);
}
$this->resource = $resource;
$this->connection = new MysqlConnection($this, $resource);
$this->connection = new PdoMysqlConnection($this, $resource);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Soluble/DbWrapper/Adapter/PdoSqliteAdapter.php
Expand Up @@ -21,7 +21,7 @@ class PdoSqliteAdapter extends GenericPdo implements AdapterInterface
* @var PdoSqliteConnection
*/
protected $connection;

/**
* Constructor
*
Expand Down
7 changes: 7 additions & 0 deletions src/Soluble/DbWrapper/Connection/ConnectionInterface.php
Expand Up @@ -19,4 +19,11 @@ public function getCurrentSchema();
* @return mixed
*/
public function getResource();

/**
* Return connection host name or IP
*
* @return string
*/
public function getHost();
}
@@ -1,25 +1,15 @@
<?php

namespace Soluble\DbWrapper\Connection;
namespace Soluble\DbWrapper\Connection\Mysql;

use Soluble\DbWrapper\Adapter\AdapterInterface;

class MysqlConnection implements ConnectionInterface
abstract class AbstractMysqlConnection
{

/**
*
* @var AdapterInterface
* @var \Soluble\DbWrapper\Adapter\AdapterInterface
*/
protected $adapter;


/**
*
* @var mixed
*/
protected $resource;

/**
*
* @param AdapterInterface $adapter
Expand All @@ -31,8 +21,11 @@ public function __construct(AdapterInterface $adapter, $resource)
$this->resource = $resource;
}


/**
* {@inheritdoc}
* Return current schema/database name
* @throws \Soluble\DbWrapper\Exception\RuntimeException
* @return string|false
*/
public function getCurrentSchema()
{
Expand All @@ -47,13 +40,4 @@ public function getCurrentSchema()
}
return $results[0]['current_schema'];
}

/**
* {@inheritdoc}
* @return \mysqli|\PDO
*/
public function getResource()
{
return $this->resource;
}
}
54 changes: 54 additions & 0 deletions src/Soluble/DbWrapper/Connection/MysqliConnection.php
@@ -0,0 +1,54 @@
<?php

namespace Soluble\DbWrapper\Connection;

use Soluble\DbWrapper\Adapter\MysqliAdapter;
use Soluble\DbWrapper\Connection\Mysql\AbstractMysqlConnection;
use mysqli;

class MysqliConnection extends AbstractMysqlConnection implements ConnectionInterface
{

/**
*
* @var MysqliAdapter
*/
protected $adapter;


/**
*
* @var mysqli
*/
protected $resource;

/**
*
* @param MysqliAdapter $adapter
* @param mysqli $resource
*/
public function __construct(MysqliAdapter $adapter, $resource)
{
$this->adapter = $adapter;
$this->resource = $resource;
}


/**
* {@inheritdoc}
* @return \mysqli
*/
public function getResource()
{
return $this->resource;
}

/**
* {@inheritdoc}
*/
public function getHost()
{
$infos = explode(' ', trim($this->resource->host_info));
return strtolower($infos[0]);
}
}
54 changes: 54 additions & 0 deletions src/Soluble/DbWrapper/Connection/PdoMysqlConnection.php
@@ -0,0 +1,54 @@
<?php

namespace Soluble\DbWrapper\Connection;

use Soluble\DbWrapper\Adapter\PdoMysqlAdapter;
use Soluble\DbWrapper\Connection\Mysql\AbstractMysqlConnection;
use PDO;

class PdoMysqlConnection extends AbstractMysqlConnection implements ConnectionInterface
{

/**
*
* @var PdoMysqlAdapter
*/
protected $adapter;


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

/**
*
* @param PdoMysqlAdapter $adapter
* @param PDO $resource
*/
public function __construct(PdoMysqlAdapter $adapter, PDO $resource)
{
$this->adapter = $adapter;
$this->resource = $resource;
}


/**
* {@inheritdoc}
* @return PDO
*/
public function getResource()
{
return $this->resource;
}

/**
* {@inheritdoc}
*/
public function getHost()
{
$infos = explode(' ', trim($this->resource->getAttribute(PDO::ATTR_CONNECTION_STATUS)));
return strtolower($infos[0]);
}
}
10 changes: 9 additions & 1 deletion src/Soluble/DbWrapper/Connection/PdoSqliteConnection.php
Expand Up @@ -37,7 +37,7 @@ public function __construct(AdapterInterface $adapter, PDO $resource)
*/
public function getCurrentSchema()
{
return 'main';
return "main";
}

/**
Expand All @@ -48,4 +48,12 @@ public function getResource()
{
return $this->resource;
}

/**
* {@inheritdoc}
*/
public function getHost()
{
return "localhost";
}
}
Expand Up @@ -5,7 +5,7 @@
use Soluble\DbWrapper\Connection;
use Soluble\DbWrapper\Adapter\MysqliAdapter;

class MysqlConnectionMysqliTest extends \PHPUnit_Framework_TestCase
class MysqliConnectionTest extends \PHPUnit_Framework_TestCase
{

/**
Expand Down Expand Up @@ -44,4 +44,11 @@ public function testGetResource()
$conn = $this->connection->getResource();
$this->assertInstanceOf('mysqli', $conn);
}

public function testGetHost()
{
$params = \SolubleTestFactories::getDbConfiguration('mysqli');
$host = strtolower($params['hostname']);
$this->assertEquals($host, $this->connection->getHost());
}
}
Expand Up @@ -5,7 +5,7 @@
use Soluble\DbWrapper\Connection;
use Soluble\DbWrapper\Adapter\PdoMysqlAdapter;

class MysqlConnectionPdoTest extends MysqlConnectionMysqliTest
class PdoMysqlConnectionTest extends MysqliConnectionTest
{

/**
Expand All @@ -31,4 +31,11 @@ public function testGetResource()
$conn = $this->connection->getResource();
$this->assertInstanceOf('PDO', $conn);
}

public function testGetHost()
{
$params = \SolubleTestFactories::getDbConfiguration('pdo:mysql');
$host = strtolower($params['hostname']);
$this->assertEquals($host, $this->connection->getHost());
}
}
Expand Up @@ -36,4 +36,9 @@ public function testGetResource()
$conn = $this->connection->getResource();
$this->assertInstanceOf('PDO', $conn);
}

public function testGetHost()
{
$this->assertEquals('localhost', $this->connection->getHost());
}
}

0 comments on commit ff1e545

Please sign in to comment.