/
ClickHouseClientAdapter.php
230 lines (206 loc) · 6.05 KB
/
ClickHouseClientAdapter.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace Borislav\Clickhouse\Client\Adapter;
use Borislav\Clickhouse\Client\Adapter\Interfaces\ClickHouseClientAdapterInterface;
use Borislav\Clickhouse\Client\Config\ClickHouseClientConfig;
use Borislav\Clickhouse\Exception\ClickHouseBadRequestException;
use Borislav\Clickhouse\Exception\ClickHouseConfigNotFoundException;
use Borislav\Clickhouse\Exception\ClickHouseInstanceIsUnserializeableException;
use Borislav\Clickhouse\Exception\ClickHouseTransportException;
use Borislav\Clickhouse\Traits\ClickHouseClientTrait;
use Borislav\Clickhouse\Traits\Interfaces\ClickHouseClientTraitInterface;
use Borislav\Clickhouse\Client\Response\ClickHouseResponse;
use Borislav\Clickhouse\Client\Response\ClickHouseSyncResponse;
use Borislav\Clickhouse\Client\Response\Interfaces\ClickHouseResponseInterface;
use Borislav\Clickhouse\Client\Response\Interfaces\ClickHouseSyncResponseInterface;
use ClickHouseDB\Client;
use ClickHouseDB\Exception\TransportException;
use ClickHouseDB\Statement;
use Ramsey\Uuid\Uuid;
/**
* This class must be used from suitable trait.
* @see ClickHouseClientTrait
* @see ClickHouseClientTraitInterface
*/
class ClickHouseClientAdapter implements ClickHouseClientAdapterInterface
{
private static Client|null $client = null;
/**
* @return ClickHouseClientAdapter
*
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
public function clickhouse(): ClickHouseClientAdapterInterface
{
self::initClient();
return $this;
}
/**
* @param string $sql
* @param array $params
*
* @return ClickHouseResponseInterface
*
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
public function select(string $sql, array $params = []): ClickHouseResponseInterface
{
return $this->respond(self::getClient()->select($sql, $params));
}
/**
* @param string $table
* @param array $values
* @param array|null $columns
*
* @return ClickHouseResponseInterface
*
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
public function insert(string $table, array $values, array|null $columns = null): ClickHouseResponseInterface
{
try {
return $this->respond(self::getClient()->insert($table, $values, $columns));
} catch (TransportException $e) {
throw new ClickHouseTransportException(
message: $e->getMessage(), previous: $e
);
}
}
/**
* If $isAwaitableQuery === false, then @see ClickHouseResponseInterface
* else @see ClickHouseSyncResponseInterface
*
* @param string $sql
* @param array $params
* @param bool $isAwaitableQuery
*
* @return ClickHouseResponseInterface|ClickHouseSyncResponseInterface
*
* @throws ClickHouseBadRequestException
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
public function query(
string $sql,
array $params = [],
bool $isAwaitableQuery = false
): ClickHouseResponseInterface|ClickHouseSyncResponseInterface {
try {
if ($isAwaitableQuery) {
[$queryUniq, $sql] = $this->makeQueryAwaitable($sql);
}
$response = $this->respond(
self::getClient()->write($sql, $params),
$isAwaitableQuery
);
if ($response->isSyncable() && $isAwaitableQuery) {
$response /** @var ClickHouseSyncResponse $response */
->setQueryUniqueKey($queryUniq);
}
return $response;
} catch (TransportException $e) {
throw new ClickHouseTransportException(
message: $e->getMessage(), previous: $e
);
}
}
/**
* @param string $sql
*
* @see ClickHouseResponseInterface
* @see ClickHouseSyncResponseInterface
*
* @return string[] Return a unique query key and awaitable-sql
* Usage -> unpack it: [uniq, awaitableSql] = $this->makeQueryAwaitable(sql).
*
* @throws ClickHouseBadRequestException
*/
private function makeQueryAwaitable(string $sql): array
{
if (!is_int(stripos($sql, ' WHERE '))) {
throw new ClickHouseBadRequestException(
'queries without `WHERE` condition is not awaitable, try to use sync. ClickHouseResponse instead ClickHouseSyncResponse.'
);
} else {
$queryUniqueKey = Uuid::uuid4();
$sql = str_ireplace(' WHERE ', sprintf(' WHERE (\'%1$s\' = \'%1$s\') AND ', $queryUniqueKey), $sql);
}
return [$queryUniqueKey, $sql];
}
/**
* @return bool
*
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
public function ping(): bool
{
return self::getClient()->ping();
}
/**
* @return array
*
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
public function showTables(): array
{
return self::getClient()->showTables();
}
/**
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
private static function initClient(): void
{
if (!is_null(self::$client)) {
return;
}
$cfg = ClickHouseClientConfig::getConfig();
self::$client = new Client(
$cfg->getConnectParams(),
$cfg->getSettings()
);
if (!self::$client->ping()) {
throw new ClickHouseTransportException(
'ping database failed.'
);
}
}
/**
* @return Client
*
* @throws ClickHouseConfigNotFoundException
* @throws ClickHouseTransportException
*/
private function getClient(): Client
{
if (!is_null(self::$client)) {
return self::$client;
}
self::initClient();
return self::$client;
}
/**
* @param Statement $statement
* @param bool $isAwaitableQuery
*
* @return ClickHouseResponseInterface|ClickHouseSyncResponseInterface
*/
private function respond(
Statement $statement,
bool $isAwaitableQuery = false
): ClickHouseResponseInterface|ClickHouseSyncResponseInterface {
return $isAwaitableQuery
? (new ClickHouseSyncResponse($statement->getRequest()))
: (new ClickHouseResponse($statement->getRequest()));
}
/**
* @throws ClickHouseInstanceIsUnserializeableException
*/
public function __wakeup() { throw new ClickHouseInstanceIsUnserializeableException(); }
private function __construct() {}
private function __clone() {}
}