|
14 | 14 |
|
15 | 15 | namespace FOD\DBALClickHouse;
|
16 | 16 |
|
17 |
| -use ClickHouseDB\Client as Smi2CHClient; |
18 |
| -use ClickHouseDB\Exception\TransportException; |
| 17 | +use ClickHouseDB\Client; |
| 18 | +use ClickHouseDB\Exception\ClickHouseException; |
19 | 19 | use Doctrine\DBAL\Driver\Connection;
|
20 |
| -use Doctrine\DBAL\Driver\PingableConnection; |
| 20 | +use Doctrine\DBAL\Driver\Result; |
21 | 21 | use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
|
| 22 | +use Doctrine\DBAL\Driver\Statement; |
| 23 | +use Doctrine\DBAL\Exception; |
22 | 24 | use Doctrine\DBAL\ParameterType;
|
23 | 25 | use Doctrine\DBAL\Platforms\AbstractPlatform;
|
| 26 | + |
24 | 27 | use function array_merge;
|
25 |
| -use function func_get_args; |
26 | 28 |
|
27 |
| -/** |
28 |
| - * ClickHouse implementation for the Connection interface. |
29 |
| - */ |
30 |
| -class ClickHouseConnection implements Connection, PingableConnection, ServerInfoAwareConnection |
| 29 | +class ClickHouseConnection implements Connection, ServerInfoAwareConnection |
31 | 30 | {
|
32 |
| - /** @var Smi2CHClient */ |
33 |
| - protected $smi2CHClient; |
| 31 | + protected Client $client; |
34 | 32 |
|
35 |
| - /** @var AbstractPlatform */ |
36 |
| - protected $platform; |
| 33 | + protected AbstractPlatform $platform; |
37 | 34 |
|
38 | 35 | public function __construct(
|
39 | 36 | array $params,
|
40 |
| - string $username, |
| 37 | + string $user, |
41 | 38 | string $password,
|
42 | 39 | AbstractPlatform $platform
|
43 | 40 | ) {
|
44 |
| - $this->smi2CHClient = new Smi2CHClient([ |
45 |
| - 'host' => $params['host'] ?? 'localhost', |
46 |
| - 'port' => $params['port'] ?? 8123, |
47 |
| - 'username' => $username, |
48 |
| - 'password' => $password, |
49 |
| - ], array_merge([ |
50 |
| - 'database' => $params['dbname'] ?? 'default', |
51 |
| - ], $params['driverOptions'] ?? [])); |
| 41 | + $this->client = new Client( |
| 42 | + [ |
| 43 | + 'host' => $params['host'] ?? 'localhost', |
| 44 | + 'port' => $params['port'] ?? 8123, |
| 45 | + 'username' => $user, |
| 46 | + 'password' => $password, |
| 47 | + ], |
| 48 | + array_merge(['database' => $params['dbname'] ?? 'default'], $params['driverOptions'] ?? []) |
| 49 | + ); |
52 | 50 | $this->platform = $platform;
|
53 | 51 | }
|
54 | 52 |
|
55 | 53 | /**
|
56 | 54 | * {@inheritDoc}
|
57 | 55 | */
|
58 |
| - public function prepare($prepareString) : ClickHouseStatement |
| 56 | + public function prepare(string $sql): Statement |
59 | 57 | {
|
60 |
| - return new ClickHouseStatement($this->smi2CHClient, $prepareString, $this->platform); |
| 58 | + return new ClickHouseStatement($this->client, $sql, $this->platform); |
61 | 59 | }
|
62 | 60 |
|
63 | 61 | /**
|
64 | 62 | * {@inheritDoc}
|
65 | 63 | */
|
66 |
| - public function query() : ClickHouseStatement |
| 64 | + public function query(string $sql): Result |
67 | 65 | {
|
68 |
| - $args = func_get_args(); |
69 |
| - $stmt = $this->prepare($args[0]); |
70 |
| - $stmt->execute(); |
71 |
| - |
72 |
| - return $stmt; |
| 66 | + return $this->prepare($sql)->execute(); |
73 | 67 | }
|
74 | 68 |
|
75 | 69 | /**
|
76 | 70 | * {@inheritDoc}
|
77 | 71 | */
|
78 |
| - public function quote($input, $type = ParameterType::STRING) |
| 72 | + public function quote($value, $type = ParameterType::STRING) |
79 | 73 | {
|
80 |
| - if ($type === ParameterType::INTEGER) { |
81 |
| - return $input; |
| 74 | + if ($type === ParameterType::STRING) { |
| 75 | + return $this->platform->quoteStringLiteral($value); |
82 | 76 | }
|
83 | 77 |
|
84 |
| - return $this->platform->quoteStringLiteral($input); |
| 78 | + return $value; |
85 | 79 | }
|
86 | 80 |
|
87 | 81 | /**
|
88 | 82 | * {@inheritDoc}
|
89 | 83 | */
|
90 |
| - public function exec($statement) : int |
| 84 | + public function exec(string $sql): int |
91 | 85 | {
|
92 |
| - $stmt = $this->prepare($statement); |
93 |
| - $stmt->execute(); |
94 |
| - |
95 |
| - return $stmt->rowCount(); |
| 86 | + return $this->prepare($sql)->execute()->rowCount(); |
96 | 87 | }
|
97 | 88 |
|
98 | 89 | /**
|
99 | 90 | * {@inheritDoc}
|
100 | 91 | */
|
101 | 92 | public function lastInsertId($name = null)
|
102 | 93 | {
|
103 |
| - throw ClickHouseException::notSupported('Unable to get last insert id in ClickHouse'); |
104 |
| - } |
105 |
| - |
106 |
| - /** |
107 |
| - * {@inheritDoc} |
108 |
| - */ |
109 |
| - public function beginTransaction() : bool |
110 |
| - { |
111 |
| - throw ClickHouseException::notSupported('Transactions are not allowed in ClickHouse'); |
112 |
| - } |
113 |
| - |
114 |
| - /** |
115 |
| - * {@inheritDoc} |
116 |
| - */ |
117 |
| - public function commit() : bool |
118 |
| - { |
119 |
| - throw ClickHouseException::notSupported('Transactions are not allowed in ClickHouse'); |
120 |
| - } |
121 |
| - |
122 |
| - /** |
123 |
| - * {@inheritDoc} |
124 |
| - */ |
125 |
| - public function rollBack() : bool |
126 |
| - { |
127 |
| - throw ClickHouseException::notSupported('Transactions are not allowed in ClickHouse'); |
| 94 | + throw Exception::notSupported(__METHOD__); |
128 | 95 | }
|
129 | 96 |
|
130 | 97 | /**
|
131 | 98 | * {@inheritDoc}
|
132 | 99 | */
|
133 |
| - public function errorCode() : ?string |
| 100 | + public function beginTransaction(): bool |
134 | 101 | {
|
135 |
| - throw ClickHouseException::notSupported('You need to implement ClickHouseConnection::errorCode()'); |
| 102 | + throw Exception::notSupported(__METHOD__); |
136 | 103 | }
|
137 | 104 |
|
138 | 105 | /**
|
139 | 106 | * {@inheritDoc}
|
140 | 107 | */
|
141 |
| - public function errorInfo() : array |
| 108 | + public function commit(): bool |
142 | 109 | {
|
143 |
| - throw ClickHouseException::notSupported('You need to implement ClickHouseConnection::errorInfo()'); |
| 110 | + throw Exception::notSupported(__METHOD__); |
144 | 111 | }
|
145 | 112 |
|
146 | 113 | /**
|
147 | 114 | * {@inheritDoc}
|
148 | 115 | */
|
149 |
| - public function ping() : bool |
| 116 | + public function rollBack(): bool |
150 | 117 | {
|
151 |
| - return $this->smi2CHClient->ping(); |
| 118 | + throw Exception::notSupported(__METHOD__); |
152 | 119 | }
|
153 | 120 |
|
154 | 121 | /**
|
155 | 122 | * {@inheritDoc}
|
156 | 123 | */
|
157 |
| - public function getServerVersion() : string |
| 124 | + public function getServerVersion(): string |
158 | 125 | {
|
159 | 126 | try {
|
160 |
| - return $this->smi2CHClient->getServerVersion(); |
161 |
| - } catch (TransportException $e) { |
| 127 | + return $this->client->getServerVersion(); |
| 128 | + } catch (ClickHouseException) { |
162 | 129 | return '';
|
163 | 130 | }
|
164 | 131 | }
|
165 |
| - |
166 |
| - /** |
167 |
| - * {@inheritDoc} |
168 |
| - */ |
169 |
| - public function requiresQueryForServerVersion() : bool |
170 |
| - { |
171 |
| - return true; |
172 |
| - } |
173 | 132 | }
|
0 commit comments