Skip to content

Commit

Permalink
fix: allow underscores in param names
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jan 23, 2025
1 parent d710fc4 commit 8394b64
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Client/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function prepareSqlRequest(
): RequestInterface {
$request = $this->initRequest($requestSettings);

preg_match_all('~\{([a-zA-Z\d]+):([a-zA-Z\d ]+(\(.+\))?)}~', $sql, $matches);
preg_match_all('~\{([a-zA-Z\d_]+):([a-zA-Z\d ]+(\(.+\))?)}~', $sql, $matches);
if ($matches[0] === []) {
$body = $this->streamFactory->createStream($sql);
try {
Expand Down
41 changes: 41 additions & 0 deletions tests/Client/Http/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Tests\Client\Http;

use DateTimeImmutable;
use Generator;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -14,6 +15,8 @@
use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry;
use SimPod\ClickHouseClient\Tests\TestCaseBase;

use function implode;

#[CoversClass(RequestFactory::class)]
final class RequestFactoryTest extends TestCaseBase
{
Expand Down Expand Up @@ -66,4 +69,42 @@ public static function providerPrepareRequest(): Generator
'?database=database&max_block_size=1',
];
}

public function testParamParsed(): void
{
$requestFactory = new RequestFactory(
new ParamValueConverterRegistry(),
new Psr17Factory(),
new Psr17Factory(),
);

$request = $requestFactory->prepareSqlRequest(
'SELECT {p1:String}, {p_2:Date}',
new RequestSettings(
[],
[],
),
new RequestOptions(
[
'p1' => 'value1',
'p_2' => new DateTimeImmutable(),
],
),
);

$body = $request->getBody()->__toString();
self::assertStringContainsString('param_p1', $body);
self::assertStringContainsString(
implode(
"\r\n",
[
'Content-Disposition: form-data; name="param_p_2"',
'Content-Length: 10',
'',
'2025-01-23',
],
),
$body,
);
}
}

0 comments on commit 8394b64

Please sign in to comment.