Skip to content

Commit 2b104bf

Browse files
authored
feat: undeprecate ValueFormatter (#250)
- do not use multipart stream when there are no native query params present
1 parent 52ebb6d commit 2b104bf

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

src/Client/Http/RequestFactory.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use InvalidArgumentException;
99
use Psr\Http\Message\RequestFactoryInterface;
1010
use Psr\Http\Message\RequestInterface;
11+
use Psr\Http\Message\StreamFactoryInterface;
1112
use Psr\Http\Message\UriFactoryInterface;
1213
use Psr\Http\Message\UriInterface;
1314
use SimPod\ClickHouseClient\Exception\UnsupportedParamType;
@@ -31,6 +32,7 @@ final class RequestFactory
3132
public function __construct(
3233
private ParamValueConverterRegistry $paramValueConverterRegistry,
3334
private RequestFactoryInterface $requestFactory,
35+
private StreamFactoryInterface $streamFactory,
3436
UriFactoryInterface|null $uriFactory = null,
3537
UriInterface|string $uri = '',
3638
) {
@@ -71,37 +73,39 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
7173
$request = $this->requestFactory->createRequest('POST', $uri);
7274

7375
preg_match_all('~\{([a-zA-Z\d]+):([a-zA-Z\d ]+(\(.+\))?)}~', $requestOptions->sql, $matches);
74-
75-
$typeToParam = array_reduce(
76-
array_keys($matches[1]),
77-
static function (array $acc, string|int $k) use ($matches) {
78-
$acc[$matches[1][$k]] = Type::fromString($matches[2][$k]);
79-
80-
return $acc;
81-
},
82-
[],
83-
);
84-
85-
$streamElements = [['name' => 'query', 'contents' => $requestOptions->sql]];
86-
foreach ($requestOptions->params as $name => $value) {
87-
$type = $typeToParam[$name] ?? null;
88-
if ($type === null) {
89-
continue;
76+
if ($matches === []) {
77+
$body = $this->streamFactory->createStream($requestOptions->sql);
78+
} else {
79+
$typeToParam = array_reduce(
80+
array_keys($matches[1]),
81+
static function (array $acc, string|int $k) use ($matches) {
82+
$acc[$matches[1][$k]] = Type::fromString($matches[2][$k]);
83+
84+
return $acc;
85+
},
86+
[],
87+
);
88+
89+
$streamElements = [['name' => 'query', 'contents' => $requestOptions->sql]];
90+
foreach ($requestOptions->params as $name => $value) {
91+
$type = $typeToParam[$name] ?? null;
92+
if ($type === null) {
93+
continue;
94+
}
95+
96+
$streamElements[] = [
97+
'name' => 'param_' . $name,
98+
'contents' => $this->paramValueConverterRegistry->get($type)($value, $type, false),
99+
];
90100
}
91101

92-
$streamElements[] = [
93-
'name' => 'param_' . $name,
94-
'contents' => $this->paramValueConverterRegistry->get($type)($value, $type, false),
95-
];
96-
}
97-
98-
try {
99-
$body = new MultipartStream($streamElements);
100-
$request = $request
101-
->withHeader('Content-Type', 'multipart/form-data; boundary=' . $body->getBoundary())
102-
->withBody($body);
103-
} catch (InvalidArgumentException) {
104-
absurd();
102+
try {
103+
$body = new MultipartStream($streamElements);
104+
$request = $request->withBody($body)
105+
->withHeader('Content-Type', 'multipart/form-data; boundary=' . $body->getBoundary());
106+
} catch (InvalidArgumentException) {
107+
absurd();
108+
}
105109
}
106110

107111
return $request;

tests/Client/Http/RequestFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function testPrepareRequest(string $uri, string $expectedUri): void
2424
new ParamValueConverterRegistry(),
2525
$psr17Factory,
2626
$psr17Factory,
27+
$psr17Factory,
2728
$uri,
2829
);
2930

tests/WithClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ private static function restartClickHouseClient(): void
9090
new ParamValueConverterRegistry(),
9191
new Psr17Factory(),
9292
new Psr17Factory(),
93+
new Psr17Factory(),
9394
),
9495
);
9596

@@ -105,6 +106,7 @@ private static function restartClickHouseClient(): void
105106
new ParamValueConverterRegistry(),
106107
new Psr17Factory(),
107108
new Psr17Factory(),
109+
new Psr17Factory(),
108110
),
109111
);
110112

@@ -120,6 +122,7 @@ private static function restartClickHouseClient(): void
120122
new ParamValueConverterRegistry(),
121123
new Psr17Factory(),
122124
new Psr17Factory(),
125+
new Psr17Factory(),
123126
),
124127
);
125128

0 commit comments

Comments
 (0)