Skip to content

Commit 1cab084

Browse files
committed
docs(readme): update client setup examples
1 parent 6c28d2c commit 1cab084

1 file changed

Lines changed: 101 additions & 22 deletions

File tree

README.md

Lines changed: 101 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ That said everything is as much transparent as possible and so object-oriented A
1212
Naming used here is the same as in ClickHouse docs.
1313

1414
- Works with any HTTP Client implementation ([PSR-18 compliant](https://www.php-fig.org/psr/psr-18/))
15-
- All [ClickHouse Formats](https://clickhouse.yandex/docs/en/interfaces/formats/) support
15+
- All [ClickHouse Formats](https://clickhouse.com/docs/en/interfaces/formats/) support
1616
- Logging ([PSR-3 compliant](https://www.php-fig.org/psr/psr-3/))
1717
- SQL Factory for [parameters "binding"](#parameters-binding)
1818
- [Native query parameters](#native-query-parameters) support
@@ -28,41 +28,76 @@ Naming used here is the same as in ClickHouse docs.
2828
- [Insert](#insert)
2929
- [Async API](#async-api)
3030
- [Select](#select-1)
31+
- [Parameters "binding"](#parameters-binding)
3132
- [Native Query Parameters](#native-query-parameters)
33+
- [Custom Query Parameter Value Conversion](#custom-query-parameter-value-conversion)
34+
- [Expression](#expression)
3235
- [Snippets](#snippets)
3336

3437
## Setup
3538

3639
```sh
37-
composer require simpod/clickhouse-client
40+
composer require simpod/clickhouse-client symfony/http-client nyholm/psr7
3841
```
3942

40-
1. Read about ClickHouse [Http Interface](https://clickhouse.com/docs/en/interfaces/http/). _It's short and useful for concept understanding._
41-
2. Create a new instance of ClickHouse client and pass PSR factories.
42-
1. Symfony HttpClient is recommended (performance, less bugs, maintenance)
43-
2. The plot twist is there's no endpoint/credentials etc. config in this library, provide it via client
43+
1. Read about ClickHouse [HTTP Interface](https://clickhouse.com/docs/en/interfaces/http/). _It's short and useful for concept understanding._
44+
2. Create a new instance of ClickHouse client and pass a PSR-18 HTTP client plus PSR-17 factories.
45+
1. Symfony HttpClient is recommended for the PSR-18 client.
46+
2. The plot twist is there's no endpoint/credentials etc. config in this library, provide it via client.
4447
3. See tests
4548

4649
```php
4750
<?php
4851

49-
use Http\Client\Curl\Client;
5052
use Nyholm\Psr7\Factory\Psr17Factory;
5153
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
5254
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
5355
use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry;
56+
use Symfony\Component\HttpClient\HttpClient;
57+
use Symfony\Component\HttpClient\Psr18Client;
5458

5559
$psr17Factory = new Psr17Factory;
5660

5761
$clickHouseClient = new PsrClickHouseClient(
58-
new Client(),
62+
new Psr18Client(
63+
HttpClient::create([
64+
'base_uri' => 'http://localhost:8123',
65+
'headers' => [
66+
'X-ClickHouse-User' => 'default',
67+
'X-ClickHouse-Key' => '',
68+
],
69+
'query' => ['database' => 'default'],
70+
]),
71+
),
5972
new RequestFactory(
6073
new ParamValueConverterRegistry(),
6174
$psr17Factory,
75+
$psr17Factory,
6276
$psr17Factory
6377
),
64-
new LoggerChain(),
65-
[],
78+
);
79+
```
80+
81+
### Logging
82+
83+
Pass a `SqlLogger` implementation as the third `PsrClickHouseClient` constructor argument. The library provides a PSR-3 adapter:
84+
85+
```php
86+
<?php
87+
88+
use Psr\Http\Client\ClientInterface;
89+
use Psr\Log\LoggerInterface;
90+
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
91+
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
92+
use SimPod\ClickHouseClient\Logger\PsrLogger;
93+
94+
/** @var ClientInterface $psr18Client */
95+
/** @var RequestFactory $requestFactory */
96+
/** @var LoggerInterface $logger */
97+
$clickHouseClient = new PsrClickHouseClient(
98+
$psr18Client,
99+
$requestFactory,
100+
new PsrLogger($logger),
66101
);
67102
```
68103

@@ -87,11 +122,11 @@ framework:
87122
88123
### PSR Factories who?
89124
90-
_The library does not implement it's own HTTP.
125+
_The library does not implement its own HTTP.
91126
That has already been done via [PSR-7, PSR-17 and PSR-18](https://www.php-fig.org/psr/).
92-
This library respects it and allows you to plug your own implementation (eg. HTTPPlug or Guzzle)._
127+
This library respects it and allows you to plug your own implementation (eg. Symfony HttpClient or Guzzle)._
93128
94-
_Recommended are `composer require nyholm/psr7` for PSR-17 and `composer require php-http/curl-client` for Curl PSR-18 implementation (used in example above)._
129+
_Recommended are `composer require nyholm/psr7` for PSR-17 and `composer require symfony/http-client` for PSR-18 (used in the example above)._
95130

96131
## Sync API
97132

@@ -108,13 +143,14 @@ Appends `FORMAT` to the query and returns response in selected output format:
108143
use SimPod\ClickHouseClient\Client\ClickHouseClient;
109144
use SimPod\ClickHouseClient\Format\JsonEachRow;
110145
use SimPod\ClickHouseClient\Output;
146+
use SimPod\ClickHouseClient\Settings\ArraySettingsProvider;
111147
112148
/** @var ClickHouseClient $client */
113149
/** @var Output\JsonEachRow $output */
114150
$output = $client->select(
115151
'SELECT * FROM table',
116152
new JsonEachRow(),
117-
['force_primary_key' => 1]
153+
new ArraySettingsProvider(['force_primary_key' => 1])
118154
);
119155
```
120156

@@ -130,14 +166,15 @@ Same as `ClickHouseClient::select()` except it also allows [parameter binding](#
130166
use SimPod\ClickHouseClient\Client\ClickHouseClient;
131167
use SimPod\ClickHouseClient\Format\JsonEachRow;
132168
use SimPod\ClickHouseClient\Output;
169+
use SimPod\ClickHouseClient\Settings\ArraySettingsProvider;
133170
134171
/** @var ClickHouseClient $client */
135172
/** @var Output\JsonEachRow $output */
136173
$output = $client->selectWithParams(
137-
'SELECT * FROM :table',
138-
['table' => 'table_name'],
174+
'SELECT * FROM table WHERE name = :name',
175+
['name' => 'Alice'],
139176
new JsonEachRow(),
140-
['force_primary_key' => 1]
177+
new ArraySettingsProvider(['force_primary_key' => 1])
141178
);
142179
```
143180

@@ -156,7 +193,7 @@ $client->insert('table', $data, $columnNames);
156193

157194
If `$columnNames` is provided and is key->value array column names are generated based on it and values are passed as parameters:
158195

159-
`$client->insert( 'table', [[1,2]], ['a' => 'Int8, 'b' => 'String'] );` generates `INSERT INTO table (a,b) VALUES ({p1:Int8},{p2:String})` and values are passed along the query.
196+
`$client->insert( 'table', [[1,2]], ['a' => 'Int8', 'b' => 'String'] );` generates `INSERT INTO table (a,b) VALUES ({p1:Int8},{p2:String})` and values are passed along the query.
160197

161198
If `$columnNames` is provided column names are generated based on it:
162199

@@ -178,6 +215,40 @@ If not provided they're not passed either:
178215

179216
### Select
180217

218+
`ClickHouseAsyncClient::select()` returns an Amp `Future`:
219+
220+
```php
221+
<?php
222+
223+
use Amp\Http\Client\HttpClientBuilder;
224+
use Amp\Http\Client\Psr7\PsrAdapter;
225+
use Nyholm\Psr7\Factory\Psr17Factory;
226+
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
227+
use SimPod\ClickHouseClient\Client\PsrClickHouseAsyncClient;
228+
use SimPod\ClickHouseClient\Format\JsonEachRow;
229+
use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry;
230+
231+
$psr17Factory = new Psr17Factory();
232+
233+
$client = new PsrClickHouseAsyncClient(
234+
HttpClientBuilder::buildDefault(),
235+
new RequestFactory(
236+
new ParamValueConverterRegistry(),
237+
$psr17Factory,
238+
$psr17Factory,
239+
$psr17Factory,
240+
'http://localhost:8123?database=default',
241+
),
242+
new PsrAdapter($psr17Factory, $psr17Factory),
243+
[
244+
'X-ClickHouse-User' => 'default',
245+
'X-ClickHouse-Key' => '',
246+
],
247+
);
248+
249+
$output = $client->select('SELECT 1 AS data', new JsonEachRow())->await();
250+
```
251+
181252
## Parameters "binding"
182253

183254
```php
@@ -197,6 +268,9 @@ This produces `SELECT 'value'` and it can be passed to `ClickHouseClient::select
197268

198269
Supported types are:
199270
- scalars
271+
- null
272+
- arrays, including `IN (:list)` values and tuple arrays
273+
- backed enums
200274
- DateTimeInterface
201275
- [Expression](#expression)
202276
- objects implementing `__toString()`
@@ -210,17 +284,19 @@ Supported types are:
210284
<?php
211285
212286
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
287+
use SimPod\ClickHouseClient\Format\JsonEachRow;
213288
214289
$client = new PsrClickHouseClient(...);
215290
216291
$output = $client->selectWithParams(
217292
'SELECT {p1:String}',
218-
['param' => 'value']
293+
['p1' => 'value'],
294+
new JsonEachRow(),
219295
);
220296
```
221297

222298
All types are supported (except `AggregateFunction`, `SimpleAggregateFunction` and `Nothing` by design).
223-
You can also pass `DateTimeInterface` into `Date*` types or native array into `Array`, `Tuple`, `Native` and `Geo` types
299+
You can also pass `DateTimeInterface` into `Date*` types or native array into `Array`, `Tuple`, `Nested` and Geo types (`Point`, `Polygon`, `Geometry` etc.).
224300

225301
### Custom Query Parameter Value Conversion
226302

@@ -235,15 +311,18 @@ use SimPod\ClickHouseClient\Client\Http\RequestFactory;
235311
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
236312
use SimPod\ClickHouseClient\Exception\UnsupportedParamValue;
237313
use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry;
314+
use DateTimeInterface;
238315
239316
$paramValueConverterRegistry = new ParamValueConverterRegistry([
240-
'datetime' => static fn (mixed $v) => $v instanceof DateTimeInterface ? $v->format('c') : throw UnsupportedParamValue::type($value)
317+
'datetime' => static fn (mixed $v) => $v instanceof DateTimeInterface
318+
? $v->format('c')
319+
: throw UnsupportedParamValue::type($v),
241320
]);
242321
243322
$client = new PsrClickHouseClient(..., new RequestFactory($paramValueConverterRegistry, ...));
244323
```
245324

246-
Be aware that the library can not ensure that passed values have a certain type. They are passed as-is and closures must accept `mixed` values.
325+
Be aware that the library cannot ensure that passed values have a certain type. They are passed as-is and closures must accept `mixed` values.
247326

248327
Throw an exception of type `UnsupportedParamValue` if your converter does not support the passed value type.
249328

0 commit comments

Comments
 (0)