Skip to content

Commit

Permalink
Add templates into Format and Output
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Aug 26, 2020
1 parent 7679e5d commit 1ebb099
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 64 deletions.
5 changes: 4 additions & 1 deletion src/Format/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use SimPod\ClickHouseClient\Output\Output;

/** @implements Format<\SimPod\ClickHouseClient\Output\Json> */
/**
* @template T
* @implements Format<\SimPod\ClickHouseClient\Output\Json<T>>
*/
final class Json implements Format
{
public static function output(string $contents) : Output
Expand Down
5 changes: 4 additions & 1 deletion src/Format/JsonCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use SimPod\ClickHouseClient\Output\Output;

/** @implements Format<\SimPod\ClickHouseClient\Output\JsonCompact> */
/**
* @template T
* @implements Format<\SimPod\ClickHouseClient\Output\JsonCompact<T>>
*/
final class JsonCompact implements Format
{
public static function output(string $contents) : Output
Expand Down
5 changes: 4 additions & 1 deletion src/Format/JsonEachRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use SimPod\ClickHouseClient\Output\Output;

/** @implements Format<\SimPod\ClickHouseClient\Output\JsonEachRow> */
/**
* @template T
* @implements Format<\SimPod\ClickHouseClient\Output\JsonEachRow<T>>
*/
final class JsonEachRow implements Format
{
public static function output(string $contents) : Output
Expand Down
5 changes: 4 additions & 1 deletion src/Format/TabSeparated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use SimPod\ClickHouseClient\Output\Output;

/** @implements Format<\SimPod\ClickHouseClient\Output\TabSeparated> */
/**
* @template T
* @implements Format<\SimPod\ClickHouseClient\Output\TabSeparated<T>>
*/
final class TabSeparated implements Format
{
public static function output(string $contents) : Output
Expand Down
8 changes: 6 additions & 2 deletions src/Output/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

use function Safe\json_decode;

/** @psalm-immutable */
/**
* @psalm-immutable
* @template T
* @implements Output<T>
*/
final class Json implements Output
{
/** @var array<array<string, mixed>> */
public array $data;

/** @var array<mixed> */
/** @var array<mixed> */
public array $meta;

public int $rows;
Expand Down
6 changes: 5 additions & 1 deletion src/Output/JsonCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

use function Safe\json_decode;

/** @psalm-immutable */
/**
* @psalm-immutable
* @template T
* @implements Output<T>
*/
final class JsonCompact implements Output
{
/** @var array<array<mixed>> */
Expand Down
10 changes: 7 additions & 3 deletions src/Output/JsonEachRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
use function Safe\sprintf;
use function str_replace;

/** @psalm-immutable */
/**
* @psalm-immutable
* @template T
* @implements Output<T>
*/
final class JsonEachRow implements Output
{
/** @var array<array<string, mixed>> */
/** @var list<T> */
public array $data;

public function __construct(string $contentsJson)
{
/**
* @var array<array<string, mixed>> $contents
* @var list<T> $contents
* @psalm-suppress ImpureFunctionCall
*/
$contents = json_decode(sprintf('[%s]', str_replace("}\n{", '},{', $contentsJson)), true);
Expand Down
1 change: 1 addition & 0 deletions src/Output/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Output;

/** @template T */
interface Output
{
public function __construct(string $contents);
Expand Down
6 changes: 5 additions & 1 deletion src/Output/TabSeparated.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace SimPod\ClickHouseClient\Output;

/** @psalm-immutable */
/**
* @psalm-immutable
* @template T
* @implements Output<T>
*/
final class TabSeparated implements Output
{
public string $contents;
Expand Down
13 changes: 5 additions & 8 deletions src/Snippet/CurrentDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Format\JsonEachRow;

use function assert;
use function is_string;

final class CurrentDatabase
{
public static function run(ClickHouseClient $clickHouseClient) : string
{
/** @var JsonEachRow<array{database: string}> $format */
$format = new JsonEachRow();

$currentDatabase = $clickHouseClient->select(
<<<CLICKHOUSE
SELECT currentDatabase() AS database
CLICKHOUSE,
new JsonEachRow()
$format
);

$databaseName = $currentDatabase->data[0]['database'];
assert(is_string($databaseName));

return $databaseName;
return $currentDatabase->data[0]['database'];
}
}
14 changes: 5 additions & 9 deletions src/Snippet/DatabaseSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,23 @@
use SimPod\ClickHouseClient\Format\JsonEachRow;
use SimPod\ClickHouseClient\Sql\Expression;

use function assert;

final class DatabaseSize
{
public static function run(ClickHouseClient $clickHouseClient, ?string $databaseName = null) : int
{
/** @var JsonEachRow<array{size: string|null}> $format */
$format = new JsonEachRow();

$currentDatabase = $clickHouseClient->selectWithParameters(
<<<CLICKHOUSE
SELECT sum(bytes) AS size
FROM system.parts
WHERE active AND database=:database
CLICKHOUSE,
['database' => $databaseName ?? Expression::new('currentDatabase()')],
new JsonEachRow()
$format
);

/** @psalm-suppress MixedAssignment */
$size = $currentDatabase->data[0]['size'];

assert($size !== null);

return (int) $size;
return (int) $currentDatabase->data[0]['size'];
}
}
9 changes: 6 additions & 3 deletions src/Snippet/Parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ public static function run(ClickHouseClient $clickHouseClient, string $table, ?b
{
$whereActiveClause = $active === null ? '' : sprintf(' AND active = %s', (int) $active);

$currentDatabase = $clickHouseClient->selectWithParameters(
/** @var JsonEachRow<array<string, mixed>> $format */
$format = new JsonEachRow();

$output = $clickHouseClient->selectWithParameters(
<<<CLICKHOUSE
SELECT *
FROM system.parts
WHERE table=:table $whereActiveClause
ORDER BY max_date
CLICKHOUSE,
['table' => $table],
new JsonEachRow()
$format
);

return $currentDatabase->data;
return $output->data;
}
}
11 changes: 5 additions & 6 deletions src/Snippet/ShowCreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@

use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Format\JsonEachRow;
use Webmozart\Assert\Assert;

use function trim;

final class ShowCreateTable
{
public static function run(ClickHouseClient $clickHouseClient, string $tableName) : string
{
/** @var JsonEachRow<array{statement: string}> $format */
$format = new JsonEachRow();

$output = $clickHouseClient->select(
<<<CLICKHOUSE
SHOW CREATE TABLE $tableName
CLICKHOUSE,
new JsonEachRow()
$format
);

$statement = $output->data[0]['statement'];
Assert::string($statement);

return trim($statement);
return trim($output->data[0]['statement']);
}
}
12 changes: 5 additions & 7 deletions src/Snippet/ShowDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,25 @@
use SimPod\ClickHouseClient\Format\JsonEachRow;

use function array_map;
use function assert;
use function is_string;

final class ShowDatabases
{
/** @return array<string> */
public static function run(ClickHouseClient $clickHouseClient) : array
{
/** @var JsonEachRow<array{name: string}> $format */
$format = new JsonEachRow();

$output = $clickHouseClient->select(
<<<CLICKHOUSE
SHOW DATABASES
CLICKHOUSE,
new JsonEachRow()
$format
);

return array_map(
static function (array $database) : string {
$databaseName = $database['name'];
assert(is_string($databaseName));

return $databaseName;
return $database['name'];
},
$output->data
);
Expand Down
21 changes: 15 additions & 6 deletions src/Snippet/TableSizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@
use SimPod\ClickHouseClient\Format\JsonEachRow;
use SimPod\ClickHouseClient\Sql\Expression;

/** @psalm-type entry = array{name: string, database: string, size: string, min_date: string, max_date: string} */
final class TableSizes
{
/** @return array<array<string, mixed>> */
/**
* @return array<entry>
*
* @phpstan-return array<array<string, mixed>>
*/
public static function run(ClickHouseClient $clickHouseClient, ?string $databaseName = null) : array
{
$currentDatabase = $clickHouseClient->selectWithParameters(
/**
* @phpstan-var JsonEachRow<array<string, mixed>> $format
* @var JsonEachRow<entry> $format
*/
$format = new JsonEachRow();

return $clickHouseClient->selectWithParameters(
<<<CLICKHOUSE
SELECT
name AS table,
Expand All @@ -37,9 +48,7 @@ public static function run(ClickHouseClient $clickHouseClient, ?string $database
GROUP BY table, database
CLICKHOUSE,
['database' => $databaseName ?? Expression::new('currentDatabase()')],
new JsonEachRow()
);

return $currentDatabase->data;
$format
)->data;
}
}
16 changes: 6 additions & 10 deletions src/Snippet/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Format\JsonEachRow;

use function assert;
use function is_string;

final class Version
{
public static function run(ClickHouseClient $clickHouseClient) : string
{
$version = $clickHouseClient->select(
/** @var JsonEachRow<array{version: string}> $format */
$format = new JsonEachRow();

$output = $clickHouseClient->select(
<<<CLICKHOUSE
SELECT version() AS version
CLICKHOUSE,
new JsonEachRow()
$format
);

/** @psalm-suppress MixedAssignment */
$version = $version->data[0]['version'];
assert(is_string($version));

return $version;
return $output->data[0]['version'];
}
}
10 changes: 6 additions & 4 deletions tests/Client/SelectAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public function testAsyncSelect() : void
SELECT number FROM system.numbers LIMIT 2
CLICKHOUSE;

$promises = [];
$promises[] = $client->select($sql, new JsonEachRow());
$promises[] = $client->select($sql, new JsonEachRow());
/** @var JsonEachRow<array{number: string}> $format */
$format = new JsonEachRow();
$promises = [
$client->select($sql, $format),
$client->select($sql, $format),
];

/** @var array<\SimPod\ClickHouseClient\Output\JsonEachRow> $jsonEachRowOutputs */
$jsonEachRowOutputs = all($promises)->wait();

$expectedData = [
Expand Down

0 comments on commit 1ebb099

Please sign in to comment.