Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Transport/HttpTransport.php
  • Loading branch information
FacedSID committed Jun 18, 2017
2 parents bd79523 + c4e03df commit cdb9046
Show file tree
Hide file tree
Showing 36 changed files with 481 additions and 475 deletions.
153 changes: 78 additions & 75 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,46 @@
use Tinderbox\Clickhouse\Transport\HttpTransport;

/**
* Client
* Client.
*/
class Client
{
/**
* Http transport which provides http requests to server
* Http transport which provides http requests to server.
*
* @var TransportInterface
*/
protected $transport;

/**
* Values Mapper to raw sql query
* Values Mapper to raw sql query.
*
* @var \Tinderbox\Clickhouse\Interfaces\QueryMapperInterface
*/
protected $mapper;

/**
* Server to perform requests
* Server to perform requests.
*
* @var \Tinderbox\Clickhouse\Server
*/
protected $server;

/**
* Cluster to perform requests
* Cluster to perform requests.
*
* In case of using cluster server will be chosen automatically or you may specify server by call using method
*
* @var \Tinderbox\Clickhouse\Cluster
*/
protected $cluster;

/**
* Client constructor.
*
* @param \Tinderbox\Clickhouse\Server|\Tinderbox\Clickhouse\Cluster $server
* @param \Tinderbox\Clickhouse\Interfaces\QueryMapperInterface|null $mapper
* @param \Tinderbox\Clickhouse\Interfaces\TransportInterface|null $transport
* @param \Tinderbox\Clickhouse\Interfaces\QueryMapperInterface|null $mapper
* @param \Tinderbox\Clickhouse\Interfaces\TransportInterface|null $transport
*
* @throws \Tinderbox\Clickhouse\Exceptions\ClientException
*/
Expand All @@ -72,33 +72,33 @@ public function __construct($server, QueryMapperInterface $mapper = null, Transp
throw ClientException::invalidServerProvided($server);
break;
}

$this->setTransport($transport);
$this->setMapper($mapper);
}

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

/**
* Returns values Mapper
* Returns values Mapper.
*
* @return \Tinderbox\Clickhouse\Interfaces\QueryMapperInterface
*/
public function getMapper(): QueryMapperInterface
{
return $this->mapper;
}

/**
* Sets transport
* Sets transport.
*
* @param \Tinderbox\Clickhouse\Interfaces\TransportInterface|null $transport
*/
Expand All @@ -110,9 +110,9 @@ protected function setTransport(TransportInterface $transport = null)
$this->transport = $transport;
}
}

/**
* Sets Mapper
* Sets Mapper.
*
* @param \Tinderbox\Clickhouse\Interfaces\QueryMapperInterface $mapper
*
Expand All @@ -123,26 +123,26 @@ public function setMapper(QueryMapperInterface $mapper = null): self
if (is_null($mapper)) {
return $this->setDefaultMapper();
}

$this->mapper = $mapper;

return $this;
}

/**
* Sets default mapper
* Sets default mapper.
*
* @return Client
*/
protected function setDefaultMapper(): self
{
$this->mapper = new UnnamedMapper();

return $this;
}

/**
* Sets server to perform requests
* Sets server to perform requests.
*
* @param \Tinderbox\Clickhouse\Server $server
*
Expand All @@ -151,42 +151,42 @@ protected function setDefaultMapper(): self
public function setServer(Server $server): self
{
$this->server = $server;

return $this;
}

/**
* Returns current server
* Returns current server.
*
* @return \Tinderbox\Clickhouse\Server
*/
public function getServer(): Server
{
return $this->server;
}

/**
* Sets cluster
* Sets cluster.
*
* If you have previously used alone server and then want to use cluster, server will be chosen from cluster
*
* @param \Tinderbox\Clickhouse\Cluster $cluster
* @param string|null $defaultServerHostname Default server to perform requests
* @param string|null $defaultServerHostname Default server to perform requests
*
* @return \Tinderbox\Clickhouse\Client
*/
public function setCluster(Cluster $cluster, string $defaultServerHostname = null): self
{
$this->server = null;
$this->cluster = $cluster;

$this->setServerByDefaultHostname($defaultServerHostname);

return $this;
}

/**
* Gets server from cluster and uses him to perform requests
* Gets server from cluster and uses him to perform requests.
*
* If no hostname provided, will use first server in cluster
*
Expand All @@ -197,46 +197,47 @@ protected function setServerByDefaultHostname(string $hostname = null)
if (is_null($hostname)) {
$servers = $this->getCluster()->getServers();
$hostnames = array_keys($servers);

$hostname = $hostnames[0];
}

$this->using($hostname);
}

/**
* Switches between servers in cluster
* Switches between servers in cluster.
*
* If no cluster provided throws exception
*
* @param string $hostname
*
* @return \Tinderbox\Clickhouse\Client
* @throws \Tinderbox\Clickhouse\Exceptions\ClientException
*
* @return \Tinderbox\Clickhouse\Client
*/
public function using(string $hostname): self
{
if (is_null($this->getCluster())) {
throw ClientException::clusterIsNotProvided();
}

$this->setServer($this->getCluster()->getServerByHostname($hostname));

return $this;
}

/**
* Return cluster
* Return cluster.
*
* @return null|\Tinderbox\Clickhouse\Cluster
*/
public function getCluster(): ?Cluster
{
return $this->cluster;
}

/**
* Removes cluster to use alone server
* Removes cluster to use alone server.
*
* @return \Tinderbox\Clickhouse\Client
*/
Expand All @@ -246,19 +247,19 @@ public function removeCluster(): self

return $this;
}

/**
* Returns transport
* Returns transport.
*
* @return TransportInterface
*/
protected function getTransport() : TransportInterface
{
return $this->transport;
}

/**
* Performs select query
* Performs select query.
*
* Example:
*
Expand All @@ -273,12 +274,12 @@ protected function getTransport() : TransportInterface
public function select(string $query, array $bindings = [], $tables = null): Result
{
$query = $this->prepareQuery($query, $bindings).' FORMAT JSON';

return $this->getTransport()->get($this->getServer(), $query, $tables);
}

/**
* Performs async select queries
* Performs async select queries.
*
* Example:
*
Expand All @@ -298,12 +299,12 @@ public function selectAsync(array $queries, int $concurrency = 5): array
foreach ($queries as $i => $query) {
$queries[$i] = [$this->prepareQuery($query[0], $query[1] ?? []).' FORMAT JSON', $query[2] ?? null];
}

return $this->getTransport()->getAsync($this->getServer(), $queries, $concurrency);
}

/**
* Performs insert query
* Performs insert query.
*
* Example:
*
Expand All @@ -317,12 +318,12 @@ public function selectAsync(array $queries, int $concurrency = 5): array
public function insert(string $query, array $bindings = []): bool
{
$query = $this->prepareQuery($query, $bindings);

return $this->getTransport()->send($this->getServer(), $query);
}

/**
* Performs async insert queries using local csv or tsv files
* Performs async insert queries using local csv or tsv files.
*
* Example:
*
Expand All @@ -333,33 +334,35 @@ public function insert(string $query, array $bindings = []): bool
* 'file4.csv',
* ]);
*
* @param string $table
* @param array $columns
* @param array $files
* @param string $table
* @param array $columns
* @param array $files
* @param string|null $format
* @param int $concurrency Max concurrency requests
* @param int $concurrency Max concurrency requests
*
* @return array
* @throws \Tinderbox\Clickhouse\Exceptions\ClientException
*
* @return array
*/
public function insertFiles(string $table, array $columns, array $files, string $format = null, int $concurrency = 5) {
public function insertFiles(string $table, array $columns, array $files, string $format = null, int $concurrency = 5)
{
if (is_null($format)) {
$format = Format::CSV;
}

$query = 'INSERT INTO '.$table.' ('.implode(', ', $columns).') FORMAT '.strtoupper($format);

foreach ($files as $file) {
if (!is_file($file)) {
throw ClientException::insertFileNotFound($file);
}
}

return $this->getTransport()->sendAsyncFilesWithQuery($this->getServer(), $query, $files, $concurrency);
}

/**
* Executes query
* Executes query.
*
* Alias for method insert
*
Expand All @@ -372,9 +375,9 @@ public function statement(string $query, array $bindings = []): bool
{
return $this->insert($query, $bindings);
}

/**
* Prepares query to execution
* Prepares query to execution.
*
* @param string $query
* @param array $bindings
Expand All @@ -385,4 +388,4 @@ protected function prepareQuery(string $query, array $bindings)
{
return $this->getMapper()->bind($query, $bindings);
}
}
}
Loading

0 comments on commit cdb9046

Please sign in to comment.