Skip to content

Commit 22bf253

Browse files
authored
feat: pass settings via interface to the client (#301)
1 parent 9835f47 commit 22bf253

14 files changed

+192
-59
lines changed

infection.json.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
"text": "infection-log.txt"
1818
},
1919
"minMsi": 90,
20-
"minCoveredMsi": 94
20+
"minCoveredMsi": 93
2121
}

src/Client/ClickHouseAsyncClient.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,32 @@
77
use GuzzleHttp\Promise\PromiseInterface;
88
use SimPod\ClickHouseClient\Format\Format;
99
use SimPod\ClickHouseClient\Output\Output;
10+
use SimPod\ClickHouseClient\Settings\EmptySettingsProvider;
11+
use SimPod\ClickHouseClient\Settings\SettingsProvider;
1012

11-
/** @see Output hack for IDE to preserve `use` */
1213
interface ClickHouseAsyncClient
1314
{
1415
/**
1516
* @param Format<O> $outputFormat
16-
* @param array<string, float|int|string> $settings
1717
*
1818
* @template O of Output
1919
*/
20-
public function select(string $query, Format $outputFormat, array $settings = []): PromiseInterface;
20+
public function select(
21+
string $query,
22+
Format $outputFormat,
23+
SettingsProvider $settings = new EmptySettingsProvider(),
24+
): PromiseInterface;
2125

2226
/**
2327
* @param array<string, mixed> $params
2428
* @param Format<O> $outputFormat
25-
* @param array<string, float|int|string> $settings
2629
*
2730
* @template O of Output
2831
*/
2932
public function selectWithParams(
3033
string $query,
3134
array $params,
3235
Format $outputFormat,
33-
array $settings = [],
36+
SettingsProvider $settings = new EmptySettingsProvider(),
3437
): PromiseInterface;
3538
}

src/Client/ClickHouseClient.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,33 @@
1313
use SimPod\ClickHouseClient\Format\Format;
1414
use SimPod\ClickHouseClient\Output\Output;
1515
use SimPod\ClickHouseClient\Schema\Table;
16+
use SimPod\ClickHouseClient\Settings\EmptySettingsProvider;
17+
use SimPod\ClickHouseClient\Settings\SettingsProvider;
1618

19+
/** @phpstan-import-type Settings from SettingsProvider */
1720
interface ClickHouseClient
1821
{
1922
/**
20-
* @param array<string, float|int|string> $settings
21-
*
2223
* @throws ClientExceptionInterface
2324
* @throws ServerError
2425
*/
25-
public function executeQuery(string $query, array $settings = []): void;
26+
public function executeQuery(string $query, SettingsProvider $settings = new EmptySettingsProvider()): void;
2627

2728
/**
2829
* @param array<string, mixed> $params
29-
* @param array<string, float|int|string> $settings
3030
*
3131
* @throws ClientExceptionInterface
3232
* @throws ServerError
3333
* @throws UnsupportedParamType
3434
* @throws UnsupportedParamValue
3535
*/
36-
public function executeQueryWithParams(string $query, array $params, array $settings = []): void;
36+
public function executeQueryWithParams(
37+
string $query,
38+
array $params,
39+
SettingsProvider $settings = new EmptySettingsProvider(),
40+
): void;
3741

3842
/**
39-
* @param array<string, float|int|string> $settings
4043
* @param Format<O> $outputFormat
4144
*
4245
* @return O
@@ -46,10 +49,13 @@ public function executeQueryWithParams(string $query, array $params, array $sett
4649
*
4750
* @template O of Output
4851
*/
49-
public function select(string $query, Format $outputFormat, array $settings = []): Output;
52+
public function select(
53+
string $query,
54+
Format $outputFormat,
55+
SettingsProvider $settings = new EmptySettingsProvider(),
56+
): Output;
5057

5158
/**
52-
* @param array<string, float|int|string> $settings
5359
* @param array<string, mixed> $params
5460
* @param Format<O> $outputFormat
5561
*
@@ -62,23 +68,31 @@ public function select(string $query, Format $outputFormat, array $settings = []
6268
*
6369
* @template O of Output
6470
*/
65-
public function selectWithParams(string $query, array $params, Format $outputFormat, array $settings = []): Output;
71+
public function selectWithParams(
72+
string $query,
73+
array $params,
74+
Format $outputFormat,
75+
SettingsProvider $settings = new EmptySettingsProvider(),
76+
): Output;
6677

6778
/**
6879
* @param array<array<mixed>> $values
6980
* @param list<string>|array<string, string>|null $columns
70-
* @param array<string, float|int|string> $settings
7181
*
7282
* @throws CannotInsert
7383
* @throws ClientExceptionInterface
7484
* @throws ServerError
7585
* @throws UnsupportedParamType
7686
* @throws UnsupportedParamValue
7787
*/
78-
public function insert(Table|string $table, array $values, array|null $columns = null, array $settings = []): void;
88+
public function insert(
89+
Table|string $table,
90+
array $values,
91+
array|null $columns = null,
92+
SettingsProvider $settings = new EmptySettingsProvider(),
93+
): void;
7994

8095
/**
81-
* @param array<string, float|int|string> $settings
8296
* @param Format<O> $inputFormat
8397
*
8498
* @throws ClientExceptionInterface
@@ -90,11 +104,10 @@ public function insertWithFormat(
90104
Table|string $table,
91105
Format $inputFormat,
92106
string $data,
93-
array $settings = [],
107+
SettingsProvider $settings = new EmptySettingsProvider(),
94108
): void;
95109

96110
/**
97-
* @param array<string, float|int|string> $settings
98111
* @param list<string> $columns
99112
* @param Format<Output<mixed>> $inputFormat
100113
*
@@ -107,6 +120,6 @@ public function insertPayload(
107120
Format $inputFormat,
108121
StreamInterface $payload,
109122
array $columns = [],
110-
array $settings = [],
123+
SettingsProvider $settings = new EmptySettingsProvider(),
111124
): void;
112125
}

src/Client/Http/RequestSettings.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44

55
namespace SimPod\ClickHouseClient\Client\Http;
66

7+
use SimPod\ClickHouseClient\Settings\SettingsProvider;
8+
9+
/** @phpstan-import-type Settings from SettingsProvider */
710
final readonly class RequestSettings
811
{
9-
/** @var array<string, float|int|string> */
12+
/** @phpstan-var Settings */
1013
public array $settings;
1114

12-
/**
13-
* @param array<string, float|int|string> $defaultSettings
14-
* @param array<string, float|int|string> $querySettings
15-
*/
1615
public function __construct(
17-
array $defaultSettings,
18-
array $querySettings,
16+
SettingsProvider $defaultSettings,
17+
SettingsProvider $querySettings,
1918
) {
20-
$this->settings = $querySettings + $defaultSettings;
19+
$this->settings = $querySettings->get() + $defaultSettings->get();
2120
}
2221
}

src/Client/PsrClickHouseAsyncClient.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use SimPod\ClickHouseClient\Format\Format;
1717
use SimPod\ClickHouseClient\Logger\SqlLogger;
1818
use SimPod\ClickHouseClient\Output\Output;
19+
use SimPod\ClickHouseClient\Settings\EmptySettingsProvider;
20+
use SimPod\ClickHouseClient\Settings\SettingsProvider;
1921
use SimPod\ClickHouseClient\Sql\SqlFactory;
2022
use SimPod\ClickHouseClient\Sql\ValueFormatter;
2123

@@ -25,12 +27,11 @@ class PsrClickHouseAsyncClient implements ClickHouseAsyncClient
2527
{
2628
private SqlFactory $sqlFactory;
2729

28-
/** @param array<string, float|int|string> $defaultSettings */
2930
public function __construct(
3031
private HttpAsyncClient $asyncClient,
3132
private RequestFactory $requestFactory,
3233
private SqlLogger|null $sqlLogger = null,
33-
private array $defaultSettings = [],
34+
private SettingsProvider $defaultSettings = new EmptySettingsProvider(),
3435
) {
3536
$this->sqlFactory = new SqlFactory(new ValueFormatter());
3637
}
@@ -40,8 +41,11 @@ public function __construct(
4041
*
4142
* @throws Exception
4243
*/
43-
public function select(string $query, Format $outputFormat, array $settings = []): PromiseInterface
44-
{
44+
public function select(
45+
string $query,
46+
Format $outputFormat,
47+
SettingsProvider $settings = new EmptySettingsProvider(),
48+
): PromiseInterface {
4549
return $this->selectWithParams($query, [], $outputFormat, $settings);
4650
}
4751

@@ -54,7 +58,7 @@ public function selectWithParams(
5458
string $query,
5559
array $params,
5660
Format $outputFormat,
57-
array $settings = [],
61+
SettingsProvider $settings = new EmptySettingsProvider(),
5862
): PromiseInterface {
5963
$formatClause = $outputFormat::toSql();
6064

@@ -75,16 +79,15 @@ public function selectWithParams(
7579

7680
/**
7781
* @param array<string, mixed> $params
78-
* @param array<string, float|int|string> $settings
7982
* @param (callable(ResponseInterface):mixed)|null $processResponse
8083
*
8184
* @throws Exception
8285
*/
8386
private function executeRequest(
8487
string $sql,
8588
array $params,
86-
array $settings = [],
87-
callable|null $processResponse = null,
89+
SettingsProvider $settings,
90+
callable|null $processResponse,
8891
): PromiseInterface {
8992
$request = $this->requestFactory->prepareSqlRequest(
9093
$sql,

src/Client/PsrClickHouseClient.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use SimPod\ClickHouseClient\Logger\SqlLogger;
2222
use SimPod\ClickHouseClient\Output\Output;
2323
use SimPod\ClickHouseClient\Schema\Table;
24+
use SimPod\ClickHouseClient\Settings\EmptySettingsProvider;
25+
use SimPod\ClickHouseClient\Settings\SettingsProvider;
2426
use SimPod\ClickHouseClient\Sql\SqlFactory;
2527
use SimPod\ClickHouseClient\Sql\ValueFormatter;
2628

@@ -42,18 +44,17 @@ class PsrClickHouseClient implements ClickHouseClient
4244

4345
private SqlFactory $sqlFactory;
4446

45-
/** @param array<string, float|int|string> $defaultSettings */
4647
public function __construct(
4748
private ClientInterface $client,
4849
private RequestFactory $requestFactory,
4950
private SqlLogger|null $sqlLogger = null,
50-
private array $defaultSettings = [],
51+
private SettingsProvider $defaultSettings = new EmptySettingsProvider(),
5152
) {
5253
$this->valueFormatter = new ValueFormatter();
5354
$this->sqlFactory = new SqlFactory($this->valueFormatter);
5455
}
5556

56-
public function executeQuery(string $query, array $settings = []): void
57+
public function executeQuery(string $query, SettingsProvider $settings = new EmptySettingsProvider()): void
5758
{
5859
try {
5960
$this->executeRequest($query, params: [], settings: $settings);
@@ -62,26 +63,36 @@ public function executeQuery(string $query, array $settings = []): void
6263
}
6364
}
6465

65-
public function executeQueryWithParams(string $query, array $params, array $settings = []): void
66-
{
66+
public function executeQueryWithParams(
67+
string $query,
68+
array $params,
69+
SettingsProvider $settings = new EmptySettingsProvider(),
70+
): void {
6771
$this->executeRequest(
6872
$this->sqlFactory->createWithParameters($query, $params),
6973
params: $params,
7074
settings: $settings,
7175
);
7276
}
7377

74-
public function select(string $query, Format $outputFormat, array $settings = []): Output
75-
{
78+
public function select(
79+
string $query,
80+
Format $outputFormat,
81+
SettingsProvider $settings = new EmptySettingsProvider(),
82+
): Output {
7683
try {
7784
return $this->selectWithParams($query, params: [], outputFormat: $outputFormat, settings: $settings);
7885
} catch (UnsupportedParamValue | UnsupportedParamType) {
7986
absurd();
8087
}
8188
}
8289

83-
public function selectWithParams(string $query, array $params, Format $outputFormat, array $settings = []): Output
84-
{
90+
public function selectWithParams(
91+
string $query,
92+
array $params,
93+
Format $outputFormat,
94+
SettingsProvider $settings = new EmptySettingsProvider(),
95+
): Output {
8596
$formatClause = $outputFormat::toSql();
8697

8798
$sql = $this->sqlFactory->createWithParameters($query, $params);
@@ -98,8 +109,12 @@ public function selectWithParams(string $query, array $params, Format $outputFor
98109
return $outputFormat::output($response->getBody()->__toString());
99110
}
100111

101-
public function insert(Table|string $table, array $values, array|null $columns = null, array $settings = []): void
102-
{
112+
public function insert(
113+
Table|string $table,
114+
array $values,
115+
array|null $columns = null,
116+
SettingsProvider $settings = new EmptySettingsProvider(),
117+
): void {
103118
if ($values === []) {
104119
throw CannotInsert::noValues();
105120
}
@@ -192,7 +207,7 @@ public function insertWithFormat(
192207
Table|string $table,
193208
Format $inputFormat,
194209
string $data,
195-
array $settings = [],
210+
SettingsProvider $settings = new EmptySettingsProvider(),
196211
): void {
197212
$formatSql = $inputFormat::toSql();
198213

@@ -220,7 +235,7 @@ public function insertPayload(
220235
Format $inputFormat,
221236
StreamInterface $payload,
222237
array $columns = [],
223-
array $settings = [],
238+
SettingsProvider $settings = new EmptySettingsProvider(),
224239
): void {
225240
if ($payload->getSize() === 0) {
226241
throw CannotInsert::noValues();
@@ -259,13 +274,12 @@ public function insertPayload(
259274

260275
/**
261276
* @param array<string, mixed> $params
262-
* @param array<string, float|int|string> $settings
263277
*
264278
* @throws ServerError
265279
* @throws ClientExceptionInterface
266280
* @throws UnsupportedParamType
267281
*/
268-
private function executeRequest(string $sql, array $params, array $settings): ResponseInterface
282+
private function executeRequest(string $sql, array $params, SettingsProvider $settings): ResponseInterface
269283
{
270284
$request = $this->requestFactory->prepareSqlRequest(
271285
$sql,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimPod\ClickHouseClient\Settings;
6+
7+
/** @phpstan-import-type Settings from SettingsProvider */
8+
final readonly class ArraySettingsProvider implements SettingsProvider
9+
{
10+
/** @phpstan-param Settings $settings */
11+
public function __construct(private array $settings = [])
12+
{
13+
}
14+
15+
public function get(): array
16+
{
17+
return $this->settings;
18+
}
19+
}

0 commit comments

Comments
 (0)