Skip to content

Commit

Permalink
added settings support in http query
Browse files Browse the repository at this point in the history
  • Loading branch information
ay committed Dec 10, 2018
1 parent 92ca8f8 commit 91b11fa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
52 changes: 51 additions & 1 deletion src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,47 @@

namespace Tinderbox\Clickhouse;

/**
* Query instance
*/
class Query
{

/**
* SQL Query.
*
* @var string
*/
protected $query;

/**
* Files attached to query.
*
* @var array
*/
protected $files = [];

/**
* Server to process query.
*
* @var \Tinderbox\Clickhouse\Server
*/
protected $server;

/**
* Query settings
*
* @var array
*/
protected $settings = [];

/**
* Query constructor.
*
* @param \Tinderbox\Clickhouse\Server $server
* @param string $query
* @param array $files
* @param array $settings
*/
public function __construct(Server $server, string $query, array $files = [], array $settings = [])
{
$this->server = $server;
Expand All @@ -21,21 +51,41 @@ public function __construct(Server $server, string $query, array $files = [], ar
$this->settings = $settings;
}

/**
* Returns SQL query.
*
* @return string
*/
public function getQuery(): string
{
return $this->query;
}

/**
* Returns files attached to query.
*
* @return array
*/
public function getFiles(): array
{
return $this->files;
}

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

/**
* Returns settings.
*
* @return array
*/
public function getSettings(): array
{
return $this->settings;
Expand Down
13 changes: 8 additions & 5 deletions src/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function write(array $queries, int $concurrency = 5) : array

$uri = $this->buildRequestUri($query->getServer(), [
'query' => $query->getQuery()
]);
], $query->getSettings());

$stream = $file->open();
$openedStreams[] = $stream;
Expand All @@ -118,7 +118,7 @@ public function write(array $queries, int $concurrency = 5) : array
} else {
$headers = $this->getHeaders();

$uri = $this->buildRequestUri($query->getServer());
$uri = $this->buildRequestUri($query->getServer(), [], $query->getSettings());

$request = new Request('POST', $uri, $headers, gzencode($query->getQuery()));

Expand Down Expand Up @@ -203,7 +203,7 @@ public function read(array $queries, int $concurrency = 5) : array

$body = new MultipartStream($multipart);

$uri = $this->buildRequestUri($query->getServer(), $params);
$uri = $this->buildRequestUri($query->getServer(), $params, $query->getSettings());

yield $index => new Request('POST', $uri, [], $body);
}
Expand Down Expand Up @@ -349,11 +349,12 @@ protected function assembleResult(Query $query, ResponseInterface $response): Re
* Builds uri with necessary params.
*
* @param \Tinderbox\Clickhouse\Server $server
* @param array $query
* @param array $query
* @param array $settings
*
* @return string
*/
protected function buildRequestUri(Server $server, array $query = []): string
protected function buildRequestUri(Server $server, array $query = [], array $settings = []): string
{
$uri = $server->getOptions()->getProtocol() . '://' . $server->getHost() . ':' . $server->getPort();

Expand All @@ -369,6 +370,8 @@ protected function buildRequestUri(Server $server, array $query = []): string
$query['password'] = $server->getPassword();
}

$query = array_merge($query, $settings);

return $uri . '?' . http_build_query($query);
}
}

0 comments on commit 91b11fa

Please sign in to comment.