diff --git a/src/Client/ClickHouseClient.php b/src/Client/ClickHouseClient.php index 1e691bb..af2461a 100644 --- a/src/Client/ClickHouseClient.php +++ b/src/Client/ClickHouseClient.php @@ -75,6 +75,38 @@ public function selectWithParams( SettingsProvider $settings = new EmptySettingsProvider(), ): Output; + /** + * @param Format $outputFormat + * + * @throws ClientExceptionInterface + * @throws ServerError + * + * @template O of Output + */ + public function selectStream( + string $query, + Format $outputFormat, + SettingsProvider $settings = new EmptySettingsProvider(), + ): StreamInterface; + + /** + * @param array $params + * @param Format $outputFormat + * + * @throws ClientExceptionInterface + * @throws ServerError + * @throws UnsupportedParamType + * @throws UnsupportedParamValue + * + * @template O of Output + */ + public function selectStreamWithParams( + string $query, + array $params, + Format $outputFormat, + SettingsProvider $settings = new EmptySettingsProvider(), + ): StreamInterface; + /** * @param array> $values * @param list|array|null $columns diff --git a/src/Client/PsrClickHouseClient.php b/src/Client/PsrClickHouseClient.php index a4f768d..08aa348 100644 --- a/src/Client/PsrClickHouseClient.php +++ b/src/Client/PsrClickHouseClient.php @@ -109,6 +109,40 @@ public function selectWithParams( return $outputFormat::output($response->getBody()->__toString()); } + public function selectStream( + string $query, + Format $outputFormat, + SettingsProvider $settings = new EmptySettingsProvider(), + ): StreamInterface { + try { + return $this->selectStreamWithParams($query, params: [], outputFormat: $outputFormat, settings: $settings); + } catch (UnsupportedParamValue | UnsupportedParamType) { + absurd(); + } + } + + public function selectStreamWithParams( + string $query, + array $params, + Format $outputFormat, + SettingsProvider $settings = new EmptySettingsProvider(), + ): StreamInterface { + $formatClause = $outputFormat::toSql(); + + $sql = $this->sqlFactory->createWithParameters($query, $params); + + $response = $this->executeRequest( + <<getBody(); + } + public function insert( Table|string $table, array $values, diff --git a/tests/Client/SelectTest.php b/tests/Client/SelectTest.php index b17c04c..d95e48f 100644 --- a/tests/Client/SelectTest.php +++ b/tests/Client/SelectTest.php @@ -164,6 +164,24 @@ public function testNull(): void self::assertTrue(true); } + public function testSelectStream(): void + { + $stream = self::$client->selectStream('SELECT 1 AS data', new TabSeparated()); + + self::assertSame("1\n", $stream->__toString()); + } + + public function testSelectStreamWithParams(): void + { + $stream = self::$client->selectStreamWithParams( + 'SELECT {p1:UInt8} AS data', + ['p1' => 3], + new TabSeparated(), + ); + + self::assertSame("3\n", $stream->__toString()); + } + public function testSettingsArePassed(): void { self::expectException(ServerError::class);