You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
44
47
3. See tests
45
48
46
49
```php
47
50
<?php
48
51
49
-
use Http\Client\Curl\Client;
50
52
use Nyholm\Psr7\Factory\Psr17Factory;
51
53
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
52
54
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
53
55
use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry;
56
+
use Symfony\Component\HttpClient\HttpClient;
57
+
use Symfony\Component\HttpClient\Psr18Client;
54
58
55
59
$psr17Factory = new Psr17Factory;
56
60
57
61
$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
+
),
59
72
new RequestFactory(
60
73
new ParamValueConverterRegistry(),
61
74
$psr17Factory,
75
+
$psr17Factory,
62
76
$psr17Factory
63
77
),
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),
66
101
);
67
102
```
68
103
@@ -87,11 +122,11 @@ framework:
87
122
88
123
### PSR Factories who?
89
124
90
-
_The library does not implement it's own HTTP.
125
+
_The library does not implement its own HTTP.
91
126
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)._
93
128
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)._
95
130
96
131
## Sync API
97
132
@@ -108,13 +143,14 @@ Appends `FORMAT` to the query and returns response in selected output format:
108
143
use SimPod\ClickHouseClient\Client\ClickHouseClient;
109
144
use SimPod\ClickHouseClient\Format\JsonEachRow;
110
145
use SimPod\ClickHouseClient\Output;
146
+
use SimPod\ClickHouseClient\Settings\ArraySettingsProvider;
111
147
112
148
/** @var ClickHouseClient $client */
113
149
/** @var Output\JsonEachRow $output */
114
150
$output = $client->select(
115
151
'SELECT * FROM table',
116
152
new JsonEachRow(),
117
-
['force_primary_key' => 1]
153
+
new ArraySettingsProvider(['force_primary_key' => 1])
118
154
);
119
155
```
120
156
@@ -130,14 +166,15 @@ Same as `ClickHouseClient::select()` except it also allows [parameter binding](#
130
166
use SimPod\ClickHouseClient\Client\ClickHouseClient;
131
167
use SimPod\ClickHouseClient\Format\JsonEachRow;
132
168
use SimPod\ClickHouseClient\Output;
169
+
use SimPod\ClickHouseClient\Settings\ArraySettingsProvider;
133
170
134
171
/** @var ClickHouseClient $client */
135
172
/** @var Output\JsonEachRow $output */
136
173
$output = $client->selectWithParams(
137
-
'SELECT * FROM :table',
138
-
['table' => 'table_name'],
174
+
'SELECT * FROM table WHERE name = :name',
175
+
['name' => 'Alice'],
139
176
new JsonEachRow(),
140
-
['force_primary_key' => 1]
177
+
new ArraySettingsProvider(['force_primary_key' => 1])
If `$columnNames` is provided and is key->value array column names are generated based on it and values are passed as parameters:
158
195
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.
160
197
161
198
If `$columnNames` is provided column names are generated based on it:
162
199
@@ -178,6 +215,40 @@ If not provided they're not passed either:
178
215
179
216
### Select
180
217
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
+
181
252
## Parameters "binding"
182
253
183
254
```php
@@ -197,6 +268,9 @@ This produces `SELECT 'value'` and it can be passed to `ClickHouseClient::select
197
268
198
269
Supported types are:
199
270
- scalars
271
+
- null
272
+
- arrays, including `IN (:list)` values and tuple arrays
273
+
- backed enums
200
274
- DateTimeInterface
201
275
- [Expression](#expression)
202
276
- objects implementing `__toString()`
@@ -210,17 +284,19 @@ Supported types are:
210
284
<?php
211
285
212
286
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
287
+
use SimPod\ClickHouseClient\Format\JsonEachRow;
213
288
214
289
$client = new PsrClickHouseClient(...);
215
290
216
291
$output = $client->selectWithParams(
217
292
'SELECT {p1:String}',
218
-
['param' => 'value']
293
+
['p1' => 'value'],
294
+
new JsonEachRow(),
219
295
);
220
296
```
221
297
222
298
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.).
224
300
225
301
### Custom Query Parameter Value Conversion
226
302
@@ -235,15 +311,18 @@ use SimPod\ClickHouseClient\Client\Http\RequestFactory;
235
311
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
236
312
use SimPod\ClickHouseClient\Exception\UnsupportedParamValue;
237
313
use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry;
314
+
use DateTimeInterface;
238
315
239
316
$paramValueConverterRegistry = new ParamValueConverterRegistry([
0 commit comments