Skip to content

Commit 6c95593

Browse files
committed
implement PingableConnection, ServerInfoAwareConnection interfaces
1 parent 8209062 commit 6c95593

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/ClickHouseConnection.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
namespace FOD\DBALClickHouse;
1616

1717
use ClickHouseDB\Client as Smi2CHClient;
18+
use ClickHouseDB\Exception\TransportException;
1819
use Doctrine\DBAL\Driver\Connection;
20+
use Doctrine\DBAL\Driver\PingableConnection;
21+
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1922
use Doctrine\DBAL\ParameterType;
2023
use Doctrine\DBAL\Platforms\AbstractPlatform;
2124

2225
/**
2326
* ClickHouse implementation for the Connection interface.
2427
*/
25-
class ClickHouseConnection implements Connection
28+
class ClickHouseConnection implements Connection, PingableConnection, ServerInfoAwareConnection
2629
{
2730
/** @var Smi2CHClient */
2831
protected $smi2CHClient;
@@ -144,4 +147,32 @@ public function errorInfo()
144147
{
145148
throw new \LogicException('You need to implement ClickHouseConnection::errorInfo()');
146149
}
150+
151+
/**
152+
* {@inheritDoc}
153+
*/
154+
public function ping()
155+
{
156+
return $this->smi2CHClient->ping();
157+
}
158+
159+
/**
160+
* {@inheritDoc}
161+
*/
162+
public function getServerVersion()
163+
{
164+
try {
165+
return $this->smi2CHClient->getServerVersion();
166+
} catch (TransportException $exception) {
167+
return null;
168+
}
169+
}
170+
171+
/**
172+
* {@inheritDoc}
173+
*/
174+
public function requiresQueryForServerVersion()
175+
{
176+
return true;
177+
}
147178
}

tests/ConnectionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOD\DBALClickHouse\Tests;
1313

1414
use Doctrine\DBAL\DBALException;
15+
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1516
use FOD\DBALClickHouse\ClickHouseException;
1617
use FOD\DBALClickHouse\Connection;
1718
use PHPUnit\Framework\TestCase;
@@ -139,4 +140,20 @@ public function testIsRollbackOnly()
139140
$this->expectException(DBALException::class);
140141
$this->connection->isRollbackOnly();
141142
}
143+
144+
public function testPing()
145+
{
146+
$this->assertTrue($this->connection->ping());
147+
}
148+
149+
public function testGetServerVersion()
150+
{
151+
$conn = $this->connection->getWrappedConnection();
152+
if ($conn instanceof ServerInfoAwareConnection) {
153+
$this->assertRegExp('/(^[0-9]+.[0-9]+.[0-9]+.[0-9]$)/mi', $conn->getServerVersion());
154+
} else {
155+
$this->fail(sprintf('`%s` does not implement the `%s` interface', \get_class($conn),
156+
ServerInfoAwareConnection::class));
157+
}
158+
}
142159
}

0 commit comments

Comments
 (0)