Skip to content

Commit

Permalink
implement PingableConnection, ServerInfoAwareConnection interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
argayash committed Jul 15, 2018
1 parent 8209062 commit 6c95593
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/ClickHouseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
namespace FOD\DBALClickHouse;

use ClickHouseDB\Client as Smi2CHClient;
use ClickHouseDB\Exception\TransportException;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* ClickHouse implementation for the Connection interface.
*/
class ClickHouseConnection implements Connection
class ClickHouseConnection implements Connection, PingableConnection, ServerInfoAwareConnection
{
/** @var Smi2CHClient */
protected $smi2CHClient;
Expand Down Expand Up @@ -144,4 +147,32 @@ public function errorInfo()
{
throw new \LogicException('You need to implement ClickHouseConnection::errorInfo()');
}

/**
* {@inheritDoc}
*/
public function ping()
{
return $this->smi2CHClient->ping();
}

/**
* {@inheritDoc}
*/
public function getServerVersion()
{
try {
return $this->smi2CHClient->getServerVersion();
} catch (TransportException $exception) {
return null;
}
}

/**
* {@inheritDoc}
*/
public function requiresQueryForServerVersion()
{
return true;
}
}
17 changes: 17 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FOD\DBALClickHouse\Tests;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use FOD\DBALClickHouse\ClickHouseException;
use FOD\DBALClickHouse\Connection;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -139,4 +140,20 @@ public function testIsRollbackOnly()
$this->expectException(DBALException::class);
$this->connection->isRollbackOnly();
}

public function testPing()
{
$this->assertTrue($this->connection->ping());
}

public function testGetServerVersion()
{
$conn = $this->connection->getWrappedConnection();
if ($conn instanceof ServerInfoAwareConnection) {
$this->assertRegExp('/(^[0-9]+.[0-9]+.[0-9]+.[0-9]$)/mi', $conn->getServerVersion());
} else {
$this->fail(sprintf('`%s` does not implement the `%s` interface', \get_class($conn),
ServerInfoAwareConnection::class));
}
}
}

0 comments on commit 6c95593

Please sign in to comment.