diff --git a/src/Query.php b/src/Query.php index 8f7e602..24ce1ee 100644 --- a/src/Query.php +++ b/src/Query.php @@ -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; @@ -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; diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index b634985..d30155a 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -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; @@ -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())); @@ -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); } @@ -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(); @@ -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); } }