Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
}
},
"require": {
"php": "^7.1|^8.0",
"guzzlehttp/guzzle": "^6.0|^7.0"
"php": "^8.0",
"guzzlehttp/guzzle": "^7.0"
},
"bin": [
"bin/ccat_linux",
"bin/ccat_darwin"
],
"require-dev": {
"mockery/mockery": "^0.9|^1.4",
"phpunit/phpunit": "^7.0|^8.0|^9.0",
"php-coveralls/php-coveralls": "^2.2",
"phpunit/phpcov": "^5.0|^6.0|^7.0|^8.0"
"mockery/mockery": "^1.6",
"phpunit/phpunit": "^9.0",
"php-coveralls/php-coveralls": "^2.5",
"phpunit/phpcov": "^8.0",
"ext-zlib": "*"
},
"scripts": {
"test": "phpunit --coverage-text --colors=never"
Expand Down
27 changes: 15 additions & 12 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
colors="true" verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<html outputDirectory="coverage"/>
</report>
</coverage>
<testsuites>
<testsuite name="Tinderbox Clickhouse Client Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="coverage"/>
</logging>
<logging/>
<php>
<const name="CLICKHOUSE_SERVER_HOST" value="127.0.0.1"/>
<const name="CLICKHOUSE_SERVER_PORT" value="8123"/>
Expand Down
122 changes: 34 additions & 88 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Tinderbox\Clickhouse\Common\File;
use Tinderbox\Clickhouse\Common\Format;
use Tinderbox\Clickhouse\Exceptions\ClusterException;
use Tinderbox\Clickhouse\Exceptions\ServerProviderException;
use Tinderbox\Clickhouse\Interfaces\FileInterface;
use Tinderbox\Clickhouse\Interfaces\TransportInterface;
use Tinderbox\Clickhouse\Query\Result;
Expand All @@ -15,63 +17,45 @@
class Client
{
/**
* Http transport which provides http requests to server.
*
* @var TransportInterface
*/
protected $transport;

/**
* Server provider.
*
* @var ServerProvider
* Cluster name.
*/
protected $serverProvider;
protected ?string $clusterName = null;

/**
* Cluster name.
*
* @var string
* Http transport which provides http requests to server.
*/
protected $clusterName;
protected ?TransportInterface $transport = null;

/**
* Server hostname.
*
* @var string
*/
protected $serverHostname;
protected null|string|\Closure $serverHostname = null;

/**
* Client constructor.
*
* @param \Tinderbox\Clickhouse\ServerProvider $serverProvider
* @param \Tinderbox\Clickhouse\Interfaces\TransportInterface|null $transport
*/
public function __construct(
ServerProvider $serverProvider,
TransportInterface $transport = null
/**
* Server Provider.
*/
protected ServerProvider $serverProvider,
?TransportInterface $transport = null
) {
$this->serverProvider = $serverProvider;
$this->setTransport($transport);
}

/**
* Creates default http transport.
*
* @return HttpTransport
*/
protected function createTransport()
protected function createTransport(): HttpTransport
{
return new HttpTransport();
}

/**
* Sets transport.
*
* @param \Tinderbox\Clickhouse\Interfaces\TransportInterface|null $transport
*/
protected function setTransport(TransportInterface $transport = null)
protected function setTransport(?TransportInterface $transport = null): void
{
if (is_null($transport)) {
$this->transport = $this->createTransport();
Expand All @@ -82,8 +66,6 @@ protected function setTransport(TransportInterface $transport = null)

/**
* Returns transport.
*
* @return TransportInterface
*/
protected function getTransport(): TransportInterface
{
Expand All @@ -92,12 +74,8 @@ protected function getTransport(): TransportInterface

/**
* Client will use servers from specified cluster.
*
* @param string|null $cluster
*
* @return $this
*/
public function onCluster(?string $cluster)
public function onCluster(?string $cluster): self
{
$this->clusterName = $cluster;
$this->serverHostname = null;
Expand All @@ -107,8 +85,6 @@ public function onCluster(?string $cluster)

/**
* Returns current cluster name.
*
* @return null|string
*/
protected function getClusterName(): ?string
{
Expand All @@ -117,12 +93,8 @@ protected function getClusterName(): ?string

/**
* Client will use specified server.
*
* @param string $serverHostname
*
* @return $this
*/
public function using(string $serverHostname)
public function using(string $serverHostname): self
{
$this->serverHostname = $serverHostname;

Expand All @@ -131,10 +103,8 @@ public function using(string $serverHostname)

/**
* Client will return random server on each query.
*
* @return $this
*/
public function usingRandomServer()
public function usingRandomServer(): self
{
$this->serverHostname = function () {
if ($this->isOnCluster()) {
Expand All @@ -149,12 +119,8 @@ public function usingRandomServer()

/**
* Client will use server with tag as server for queries.
*
* @var string
*
* @return $this
*/
public function usingServerWithTag(string $tag)
public function usingServerWithTag(string $tag): self
{
$this->serverHostname = function () use ($tag) {
if ($this->isOnCluster()) {
Expand All @@ -180,7 +146,8 @@ protected function isOnCluster(): bool
/**
* Returns server to perform request.
*
* @return Server
* @throws ServerProviderException
* @throws ClusterException
*/
public function getServer(): Server
{
Expand Down Expand Up @@ -223,11 +190,10 @@ public function getServer(): Server
*
* $client->select('select * from table where column = ?', [1]);
*
* @param string $query
* @param FileInterface[] $files
* @param array $settings
*
* @return \Tinderbox\Clickhouse\Query\Result
* @throws ServerProviderException
* @throws ClusterException
*/
public function readOne(string $query, array $files = [], array $settings = []): Result
{
Expand All @@ -241,10 +207,10 @@ public function readOne(string $query, array $files = [], array $settings = []):
/**
* Performs batch of select queries.
*
* @param array $queries
* @param int $concurrency Max concurrency requests
* @param int $concurrency Max concurrency requests
*
* @return array
* @throws ServerProviderException
* @throws ClusterException
*/
public function read(array $queries, int $concurrency = 5): array
{
Expand All @@ -260,11 +226,8 @@ public function read(array $queries, int $concurrency = 5): array
/**
* Performs insert or simple statement query.
*
* @param string $query
* @param array $files
* @param array $settings
*
* @return bool
* @throws ServerProviderException
* @throws ClusterException
*/
public function writeOne(string $query, array $files = [], array $settings = []): bool
{
Expand All @@ -279,11 +242,6 @@ public function writeOne(string $query, array $files = [], array $settings = [])

/**
* Performs batch of insert or simple statement queries.
*
* @param array $queries
* @param int $concurrency
*
* @return array
*/
public function write(array $queries, int $concurrency = 5): array
{
Expand All @@ -299,23 +257,19 @@ public function write(array $queries, int $concurrency = 5): array
/**
* Performs async insert queries using local csv or tsv files.
*
* @param string $table
* @param array $columns
* @param array $files
* @param string|null $format
* @param array $settings
* @param int $concurrency Max concurrency requests
* @param int $concurrency Max concurrency requests
*
* @return array
* @throws ServerProviderException
* @throws ClusterException
*/
public function writeFiles(
string $table,
array $columns,
array $files,
string $format = Format::TSV,
?string $format = Format::TSV,
array $settings = [],
int $concurrency = 5
) {
): array {
$sql = 'INSERT INTO '.$table.' ('.implode(', ', $columns).') FORMAT '.strtoupper($format);

foreach ($files as $i => $file) {
Expand All @@ -331,13 +285,6 @@ public function writeFiles(

/**
* Creates query instance from specified arguments.
*
* @param \Tinderbox\Clickhouse\Server $server
* @param string $sql
* @param array $files
* @param array $settings
*
* @return \Tinderbox\Clickhouse\Query
*/
protected function createQuery(
Server $server,
Expand All @@ -351,9 +298,8 @@ protected function createQuery(
/**
* Parses query array and returns query instance.
*
* @param array $query
*
* @return \Tinderbox\Clickhouse\Query
* @throws ServerProviderException
* @throws ClusterException
*/
protected function guessQuery(array $query): Query
{
Expand Down
Loading