-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDatabaseSize.php
More file actions
40 lines (34 loc) · 1.22 KB
/
DatabaseSize.php
File metadata and controls
40 lines (34 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
namespace SimPod\ClickHouseClient\Snippet;
use Psr\Http\Client\ClientExceptionInterface;
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Exception\UnsupportedParamType;
use SimPod\ClickHouseClient\Exception\UnsupportedParamValue;
use SimPod\ClickHouseClient\Format\JsonEachRow;
use SimPod\ClickHouseClient\Sql\Expression;
final class DatabaseSize
{
/**
* @throws ClientExceptionInterface
* @throws ServerError
* @throws UnsupportedParamType
* @throws UnsupportedParamValue
*/
public static function run(ClickHouseClient $clickHouseClient, string|null $databaseName = null): int
{
/** @var JsonEachRow<array{size: string|null}> $format */
$format = new JsonEachRow();
$currentDatabase = $clickHouseClient->selectWithParams(
<<<'CLICKHOUSE'
SELECT sum(bytes) AS size
FROM system.parts
WHERE active AND database=:database
CLICKHOUSE,
['database' => $databaseName ?? Expression::new('currentDatabase()')],
$format,
);
return (int) $currentDatabase->data[0]['size'];
}
}