Skip to content

Commit 690272d

Browse files
committed
try fix ping() for windows users
1 parent 35c69ad commit 690272d

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ PHP ClickHouse wrapper - Changelog
55
======================
66

77

8+
### 2022-04-20 [Release 1.4.4]
9+
* Fix ping() for windows users
10+
* ping(true) throw TransportException if can`t connect/ping
11+
812
### 2022-04-20 [Release 1.4.3]
913
* Fix: prevent enable_http_compression parameter from being overridden #164
1014
* For correct work with utf-8 . I am working on server with PHP 5.6.40 Update CurlerRequest.php #158

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ composer require smi2/phpclickhouse
3737

3838
In php
3939
```php
40+
4041
// vendor autoload
4142
$db = new ClickHouseDB\Client(['config_array']);
42-
$db->ping();
43+
44+
if (!$db->ping()) echo 'Error connect';
4345
```
4446

4547
Last stable version for
@@ -64,7 +66,7 @@ $db->database('default');
6466
$db->setTimeout(1.5); // 1500 ms
6567
$db->setTimeout(10); // 10 seconds
6668
$db->setConnectTimeOut(5); // 5 seconds
67-
69+
$db->ping(true); // if can`t connect throw exception
6870
```
6971

7072
Show tables:

src/Client.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ClickHouseDB;
66

77
use ClickHouseDB\Exception\QueryException;
8+
use ClickHouseDB\Exception\TransportException;
89
use ClickHouseDB\Query\Degeneration;
910
use ClickHouseDB\Query\Degeneration\Bindings;
1011
use ClickHouseDB\Query\Degeneration\Conditions;
@@ -735,11 +736,15 @@ public function tableSize(string $tableName)
735736
/**
736737
* Ping server
737738
*
739+
* @param bool $throwException
738740
* @return bool
741+
* @throws TransportException
739742
*/
740-
public function ping()
743+
public function ping(bool $throwException=false): bool
741744
{
742-
return $this->transport()->ping();
745+
$result=$this->transport()->ping();
746+
if ($throwException && !$result) throw new TransportException('Can`t ping server');
747+
return $result;
743748
}
744749

745750
/**

src/Transport/Http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ public function ping(): bool
575575
$request->url($this->getUri())->verbose(false)->GET()->connectTimeOut($this->getConnectTimeOut());
576576
$this->_curler->execOne($request);
577577

578-
return $request->response()->body() === 'Ok.' . PHP_EOL;
578+
return trim($request->response()->body()) === 'Ok.';
579579
}
580580

581581
/**

tests/ClientTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ClickHouseDB\Client;
66
use ClickHouseDB\Exception\DatabaseException;
77
use ClickHouseDB\Exception\QueryException;
8+
use ClickHouseDB\Exception\TransportException;
89
use ClickHouseDB\Query\WhereInFile;
910
use ClickHouseDB\Query\WriteToFile;
1011
use ClickHouseDB\Quote\FormatLine;
@@ -803,6 +804,10 @@ public function testExceptionConnects()
803804

804805
$db = new Client($config);
805806
$this->assertFalse($db->ping());
807+
808+
$this->expectException(TransportException::class);
809+
$db->ping(true);
810+
806811
}
807812

808813
public function testSettings()

0 commit comments

Comments
 (0)