Skip to content

Commit

Permalink
feat: pass parameters natively via http interface along the query
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jan 16, 2024
1 parent a1ee820 commit 620c0e7
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 65 deletions.
6 changes: 5 additions & 1 deletion src/Client/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ static function (array $acc, string|int $k) use ($matches) {

$streamElements = [['name' => 'query', 'contents' => $requestOptions->sql]];
foreach ($requestOptions->params as $name => $value) {
$type = $typeToParam[$name];
$type = $typeToParam[$name] ?? null;
if ($type === null) {
continue;

Check warning on line 87 in src/Client/Http/RequestFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Client/Http/RequestFactory.php#L87

Added line #L87 was not covered by tests
}

$streamElements[] = [
'name' => 'param_' . $name,
'contents' => $this->paramValueConverterRegistry->get($type)($value, $type, false),
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/UnsupportedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public static function fromType(Type $type): self
{
return new self($type->name);

Check warning on line 14 in src/Exception/UnsupportedType.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/UnsupportedType.php#L14

Added line #L14 was not covered by tests
}

public static function fromString(string $type): self

Check warning on line 17 in src/Exception/UnsupportedType.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/UnsupportedType.php#L17

Added line #L17 was not covered by tests
{
return new self($type);

Check warning on line 19 in src/Exception/UnsupportedType.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/UnsupportedType.php#L19

Added line #L19 was not covered by tests
}
}
32 changes: 16 additions & 16 deletions tests/Client/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public function testInsert(string $tableSql): void
['PageViews' => 6, 'UserID' => 4324182021466249494, 'Duration' => 185, 'Sign' => 1],
];

$this->client->executeQuery($tableSql);
self::$client->executeQuery($tableSql);

$this->client->insert('UserActivity', $data);
self::$client->insert('UserActivity', $data);

$output = $this->client->select(
$output = self::$client->select(
<<<'CLICKHOUSE'
SELECT * FROM UserActivity
CLICKHOUSE,
Expand All @@ -57,9 +57,9 @@ public function testInsertUseColumns(string $tableSql): void
['PageViews' => 6, 'UserID' => '4324182021466249494', 'Duration' => 185, 'Sign' => 1],
];

$this->client->executeQuery($tableSql);
self::$client->executeQuery($tableSql);

$this->client->insert(
self::$client->insert(
'UserActivity',
[
[5, 4324182021466249494, 146, -1],
Expand All @@ -68,7 +68,7 @@ public function testInsertUseColumns(string $tableSql): void
['PageViews', 'UserID', 'Duration', 'Sign'],
);

$output = $this->client->select(
$output = self::$client->select(
<<<'CLICKHOUSE'
SELECT * FROM UserActivity
CLICKHOUSE,
Expand All @@ -80,7 +80,7 @@ public function testInsertUseColumns(string $tableSql): void

public function testInsertEscaping(): void
{
$this->client->executeQuery(
self::$client->executeQuery(
<<<'CLICKHOUSE'
CREATE TABLE a (
b Nullable(String)
Expand All @@ -94,9 +94,9 @@ public function testInsertEscaping(): void
["\t"],
];

$this->client->insert('a', $expectedData);
self::$client->insert('a', $expectedData);

$output = $this->client->select(
$output = self::$client->select(
<<<'CLICKHOUSE'
SELECT * FROM a
CLICKHOUSE,
Expand Down Expand Up @@ -124,7 +124,7 @@ public static function providerInsert(): iterable

public function testInsertWithFormat(): void
{
$this->client->executeQuery(
self::$client->executeQuery(
<<<'CLICKHOUSE'
CREATE TABLE UserActivity (
PageViews UInt32,
Expand All @@ -136,7 +136,7 @@ public function testInsertWithFormat(): void
CLICKHOUSE,
);

$this->client->insertWithFormat(
self::$client->insertWithFormat(
'UserActivity',
new JsonEachRow(),
<<<'JSONEACHROW'
Expand All @@ -145,7 +145,7 @@ public function testInsertWithFormat(): void
JSONEACHROW,
);

$output = $this->client->select(
$output = self::$client->select(
<<<'CLICKHOUSE'
SELECT * FROM UserActivity
CLICKHOUSE
Expand All @@ -166,14 +166,14 @@ public function testInsertEmptyValuesThrowsException(): void
{
$this->expectException(CannotInsert::class);

$this->client->insert('table', []);
self::$client->insert('table', []);
}

public function testInsertToNonExistentTableExpectServerError(): void
{
$this->expectException(ServerError::class);

$this->client->insert('table', [[1]]);
self::$client->insert('table', [[1]]);
}

public function testInsertWithWrongColumns(): void
Expand All @@ -188,12 +188,12 @@ public function testInsertWithWrongColumns(): void
ENGINE Memory
CLICKHOUSE;

$this->client->executeQuery($tableSql);
self::$client->executeQuery($tableSql);

$this->expectException(ServerError::class);
$this->expectExceptionMessage('SYNTAX_ERROR');

$this->client->insert(
self::$client->insert(
'UserActivity',
[
[5],
Expand Down
4 changes: 2 additions & 2 deletions tests/Client/SelectAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class SelectAsyncTest extends TestCaseBase

public function testAsyncSelect(): void
{
$client = $this->asyncClient;
$client = self::$asyncClient;

$sql = <<<'CLICKHOUSE'
SELECT number FROM system.numbers LIMIT 2
Expand Down Expand Up @@ -56,6 +56,6 @@ public function testSelectFromNonExistentTableExpectServerError(): void
{
$this->expectException(ServerError::class);

$this->asyncClient->select('table', new TabSeparated())->wait();
self::$asyncClient->select('table', new TabSeparated())->wait();
}
}
12 changes: 6 additions & 6 deletions tests/Client/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class SelectTest extends TestCaseBase

public function testSelectWithParams(): void
{
$client = $this->client;
$client = self::$client;
$output = $client->selectWithParams('SELECT {p1:UInt8} AS data', ['p1' => 3], new TabSeparated());

self::assertSame("3\n", $output->contents);
Expand All @@ -43,7 +43,7 @@ public function testSelectWithParams(): void
#[DataProvider('providerJson')]
public function testJson(mixed $expectedData, string $sql): void
{
$client = $this->client;
$client = self::$client;
$output = $client->select($sql, new Json());

self::assertSame($expectedData, $output->data);
Expand Down Expand Up @@ -82,7 +82,7 @@ public static function providerJson(): iterable
#[DataProvider('providerJsonCompact')]
public function testJsonCompact(mixed $expectedData, string $sql): void
{
$client = $this->client;
$client = self::$client;
$output = $client->select($sql, new JsonCompact());

self::assertSame($expectedData, $output->data);
Expand Down Expand Up @@ -121,7 +121,7 @@ public static function providerJsonCompact(): iterable
#[DataProvider('providerJsonEachRow')]
public function testJsonEachRow(mixed $expectedData, string $sql): void
{
$client = $this->client;
$client = self::$client;
$output = $client->select($sql, new JsonEachRow());

self::assertSame($expectedData, $output->data);
Expand Down Expand Up @@ -159,7 +159,7 @@ public static function providerJsonEachRow(): iterable

public function testNull(): void
{
$client = $this->client;
$client = self::$client;
$client->select('SELECT 1', new Null_());

self::assertTrue(true);
Expand All @@ -170,6 +170,6 @@ public function testSettingsArePassed(): void
self::expectException(ServerError::class);
$this->expectExceptionMessageMatches("~DB::Exception: Database `non-existent` (doesn't|does not) exist~");

$this->client->select('SELECT 1', new JsonCompact(), ['database' => 'non-existent']);
self::$client->select('SELECT 1', new JsonCompact(), ['database' => 'non-existent']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Http\Client\ClientExceptionInterface;
use ReflectionFunction;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Format\JsonEachRow;
use SimPod\ClickHouseClient\Format\TabSeparated;
use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry;
use SimPod\ClickHouseClient\Sql\Type;
use SimPod\ClickHouseClient\Tests\ClickHouseVersion;
use SimPod\ClickHouseClient\Tests\TestCaseBase;
use SimPod\ClickHouseClient\Tests\WithClient;

Expand All @@ -27,7 +27,7 @@
use function trim;

#[CoversClass(ParamValueConverterRegistry::class)]
final class ParamValueConverterTest extends TestCaseBase
final class ParamValueConverterRegistryTest extends TestCaseBase
{
use WithClient;

Expand Down Expand Up @@ -76,22 +76,20 @@ public function testAllTypesAreCovered(): void
#[DataProvider('providerConvert')]
public function testConvert(string $type, mixed $value, mixed $expected): void
{
$registry = new ParamValueConverterRegistry();

$converter = $registry->get(Type::fromString($type));
$reflection = new ReflectionFunction($converter);
// $argTypeName = $reflection->getParameters()[0]->getType()->getName();

self::assertSame(
$expected,
trim(
self::$client->selectWithParams(
sprintf('SELECT {p1:%s}', $type),
['p1' => $value],
new TabSeparated(),
)->contents,
),
);
if (ClickHouseVersion::get() < 2307) {
self::markTestSkipped();
}

self::assertSame(
$expected,
trim(
self::$client->selectWithParams(
sprintf('SELECT {p1:%s}', $type),
['p1' => $value],
new TabSeparated(),
)->contents,
),
);
}

/** @return Generator<array{string, mixed, mixed}> */
Expand Down
4 changes: 2 additions & 2 deletions tests/Snippet/CurrentDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class CurrentDatabaseTest extends TestCaseBase
public function testRun(): void
{
self::assertSame(
$this->currentDbName,
CurrentDatabase::run($this->client),
self::$currentDbName,
CurrentDatabase::run(self::$client),
);
}
}
10 changes: 5 additions & 5 deletions tests/Snippet/DatabaseSizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class DatabaseSizeTest extends TestCaseBase

public function setUp(): void
{
$this->client->executeQuery(
self::$client->executeQuery(
<<<'CLICKHOUSE'
CREATE TABLE test (
a_date DateTime,
Expand All @@ -33,9 +33,9 @@ public function setUp(): void

public function testRun(): void
{
self::assertSame(0, DatabaseSize::run($this->client));
self::assertSame(0, DatabaseSize::run(self::$client));

$this->client->insert('test', [[new DateTimeImmutable('2020-08-01 00:11:22'), 1]]);
self::$client->insert('test', [[new DateTimeImmutable('2020-08-01 00:11:22'), 1]]);

if (ClickHouseVersion::get() >= 2307) {
$expectedSize = 316;
Expand All @@ -45,11 +45,11 @@ public function testRun(): void
$expectedSize = 150;
}

self::assertSame($expectedSize, DatabaseSize::run($this->client));
self::assertSame($expectedSize, DatabaseSize::run(self::$client));
}

public function tearDown(): void
{
$this->client->executeQuery('DROP TABLE test');
self::$client->executeQuery('DROP TABLE test');
}
}
2 changes: 1 addition & 1 deletion tests/Snippet/PartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ final class PartsTest extends TestCaseBase

public function testRun(): void
{
self::assertSame([], Parts::run($this->client, 'system.query_log'));
self::assertSame([], Parts::run(self::$client, 'system.query_log'));
}
}
6 changes: 3 additions & 3 deletions tests/Snippet/ShowCreateTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ final class ShowCreateTableTest extends TestCaseBase

public function testRun(): void
{
$dbName = $this->currentDbName;
$dbName = self::$currentDbName;
$sql = <<<CLICKHOUSE
CREATE TABLE $dbName.test (`date` Date) ENGINE = Memory
CLICKHOUSE;

$this->client->executeQuery($sql);
self::$client->executeQuery($sql);

$createTableSql = ShowCreateTable::run($this->client, 'test');
$createTableSql = ShowCreateTable::run(self::$client, 'test');

// BC
$replaced = preg_replace(
Expand Down
10 changes: 5 additions & 5 deletions tests/Snippet/ShowDatabasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ final class ShowDatabasesTest extends TestCaseBase

public function testRun(): void
{
$databases = ShowDatabases::run($this->client);
$databases = ShowDatabases::run(self::$client);
self::assertGreaterThan(2, count($databases)); // Default, system, at least one test database

$databases = array_filter(
$databases,
fn (string $database): bool => ! str_starts_with($database, 'clickhouse_client_test__')
|| $database === $this->currentDbName
static fn (string $database): bool => ! str_starts_with($database, 'clickhouse_client_test__')
|| $database === self::$currentDbName
);

$databases = array_values($databases);
Expand All @@ -42,13 +42,13 @@ public function testRun(): void
$expected = ClickHouseVersion::get() >= 2111
? [
'INFORMATION_SCHEMA',
$this->currentDbName,
self::$currentDbName,
'default',
'information_schema',
'system',
]
: [
$this->currentDbName,
self::$currentDbName,
'default',
'system',
];
Expand Down
Loading

0 comments on commit 620c0e7

Please sign in to comment.