Skip to content

Commit

Permalink
try fix ping() for windows users
Browse files Browse the repository at this point in the history
  • Loading branch information
isublimity committed Apr 23, 2022
1 parent 35c69ad commit 690272d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ PHP ClickHouse wrapper - Changelog
======================


### 2022-04-20 [Release 1.4.4]
* Fix ping() for windows users
* ping(true) throw TransportException if can`t connect/ping

### 2022-04-20 [Release 1.4.3]
* Fix: prevent enable_http_compression parameter from being overridden #164
* For correct work with utf-8 . I am working on server with PHP 5.6.40 Update CurlerRequest.php #158
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ composer require smi2/phpclickhouse

In php
```php

// vendor autoload
$db = new ClickHouseDB\Client(['config_array']);
$db->ping();

if (!$db->ping()) echo 'Error connect';
```

Last stable version for
Expand All @@ -64,7 +66,7 @@ $db->database('default');
$db->setTimeout(1.5); // 1500 ms
$db->setTimeout(10); // 10 seconds
$db->setConnectTimeOut(5); // 5 seconds

$db->ping(true); // if can`t connect throw exception
```

Show tables:
Expand Down
9 changes: 7 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ClickHouseDB;

use ClickHouseDB\Exception\QueryException;
use ClickHouseDB\Exception\TransportException;
use ClickHouseDB\Query\Degeneration;
use ClickHouseDB\Query\Degeneration\Bindings;
use ClickHouseDB\Query\Degeneration\Conditions;
Expand Down Expand Up @@ -735,11 +736,15 @@ public function tableSize(string $tableName)
/**
* Ping server
*
* @param bool $throwException
* @return bool
* @throws TransportException
*/
public function ping()
public function ping(bool $throwException=false): bool
{
return $this->transport()->ping();
$result=$this->transport()->ping();
if ($throwException && !$result) throw new TransportException('Can`t ping server');
return $result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public function ping(): bool
$request->url($this->getUri())->verbose(false)->GET()->connectTimeOut($this->getConnectTimeOut());
$this->_curler->execOne($request);

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

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ClickHouseDB\Client;
use ClickHouseDB\Exception\DatabaseException;
use ClickHouseDB\Exception\QueryException;
use ClickHouseDB\Exception\TransportException;
use ClickHouseDB\Query\WhereInFile;
use ClickHouseDB\Query\WriteToFile;
use ClickHouseDB\Quote\FormatLine;
Expand Down Expand Up @@ -803,6 +804,10 @@ public function testExceptionConnects()

$db = new Client($config);
$this->assertFalse($db->ping());

$this->expectException(TransportException::class);
$db->ping(true);

}

public function testSettings()
Expand Down

0 comments on commit 690272d

Please sign in to comment.