Skip to content

Commit 9a2b0dd

Browse files
author
Ihor Tymoshenko
authored
Added Doctrine DBAL 3 support (#54)
1 parent 74536d6 commit 9a2b0dd

40 files changed

+1426
-1463
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919

2020
strategy:
2121
matrix:
22-
php-versions: ['7.1', '7.4', '8.0', '8.1']
22+
php-versions: ['8.0', '8.1']
2323
fail-fast: false
2424

2525
services:
@@ -45,4 +45,4 @@ jobs:
4545
run: composer install --prefer-dist
4646

4747
- name: Run tests
48-
run: ./vendor/bin/phpunit --configuration ./phpunit.xml.dist --coverage-text
48+
run: ./vendor/bin/phpunit --configuration ./phpunit.xml.dist --coverage-text

.gitignore

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
.idea/
2-
composer.lock
3-
vendor/
4-
phpunit.xml
5-
.phpunit.result.cache
6-
Vagrantfile
7-
puphpet/
8-
.vagrant/
1+
/.idea/
2+
/vendor/
3+
/.phpunit.result.cache
4+
/composer.lock
5+
/phpunit.xml

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ doctrine:
198198
array(uint64): FOD\DBALClickHouse\Types\ArrayUInt64Type
199199
array(float32): FOD\DBALClickHouse\Types\ArrayFloat32Type
200200
array(float64): FOD\DBALClickHouse\Types\ArrayFloat64Type
201-
array(string): FOD\DBALClickHouse\Types\ArrayStringType
201+
array(string): FOD\DBALClickHouse\Types\ArrayStringableType
202202
array(datetime): FOD\DBALClickHouse\Types\ArrayDateTimeType
203203
array(date): FOD\DBALClickHouse\Types\ArrayDateType
204204
```

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
}
2626
],
2727
"require": {
28-
"php": "^7.1 || ^8.0",
29-
"ext-pdo": "*",
28+
"php": "^8.0",
3029
"ext-pcre": "*",
31-
"doctrine/dbal": "^2.7",
30+
"ext-mbstring": "*",
31+
"doctrine/dbal": "^3.0",
3232
"smi2/phpclickhouse": "^1.0"
3333
},
3434
"require-dev": {
3535
"doctrine/coding-standard": "^4.0 || ^9.0",
36-
"phpunit/phpunit": "^7.0 || ^9.0"
36+
"phpunit/phpunit": "^9.5"
3737
},
3838
"autoload": {
3939
"psr-4": {

src/ClickHouseConnection.php

Lines changed: 39 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,160 +14,119 @@
1414

1515
namespace FOD\DBALClickHouse;
1616

17-
use ClickHouseDB\Client as Smi2CHClient;
18-
use ClickHouseDB\Exception\TransportException;
17+
use ClickHouseDB\Client;
18+
use ClickHouseDB\Exception\ClickHouseException;
1919
use Doctrine\DBAL\Driver\Connection;
20-
use Doctrine\DBAL\Driver\PingableConnection;
20+
use Doctrine\DBAL\Driver\Result;
2121
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
22+
use Doctrine\DBAL\Driver\Statement;
23+
use Doctrine\DBAL\Exception;
2224
use Doctrine\DBAL\ParameterType;
2325
use Doctrine\DBAL\Platforms\AbstractPlatform;
26+
2427
use function array_merge;
25-
use function func_get_args;
2628

27-
/**
28-
* ClickHouse implementation for the Connection interface.
29-
*/
30-
class ClickHouseConnection implements Connection, PingableConnection, ServerInfoAwareConnection
29+
class ClickHouseConnection implements Connection, ServerInfoAwareConnection
3130
{
32-
/** @var Smi2CHClient */
33-
protected $smi2CHClient;
31+
protected Client $client;
3432

35-
/** @var AbstractPlatform */
36-
protected $platform;
33+
protected AbstractPlatform $platform;
3734

3835
public function __construct(
3936
array $params,
40-
string $username,
37+
string $user,
4138
string $password,
4239
AbstractPlatform $platform
4340
) {
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+
);
5250
$this->platform = $platform;
5351
}
5452

5553
/**
5654
* {@inheritDoc}
5755
*/
58-
public function prepare($prepareString) : ClickHouseStatement
56+
public function prepare(string $sql): Statement
5957
{
60-
return new ClickHouseStatement($this->smi2CHClient, $prepareString, $this->platform);
58+
return new ClickHouseStatement($this->client, $sql, $this->platform);
6159
}
6260

6361
/**
6462
* {@inheritDoc}
6563
*/
66-
public function query() : ClickHouseStatement
64+
public function query(string $sql): Result
6765
{
68-
$args = func_get_args();
69-
$stmt = $this->prepare($args[0]);
70-
$stmt->execute();
71-
72-
return $stmt;
66+
return $this->prepare($sql)->execute();
7367
}
7468

7569
/**
7670
* {@inheritDoc}
7771
*/
78-
public function quote($input, $type = ParameterType::STRING)
72+
public function quote($value, $type = ParameterType::STRING)
7973
{
80-
if ($type === ParameterType::INTEGER) {
81-
return $input;
74+
if ($type === ParameterType::STRING) {
75+
return $this->platform->quoteStringLiteral($value);
8276
}
8377

84-
return $this->platform->quoteStringLiteral($input);
78+
return $value;
8579
}
8680

8781
/**
8882
* {@inheritDoc}
8983
*/
90-
public function exec($statement) : int
84+
public function exec(string $sql): int
9185
{
92-
$stmt = $this->prepare($statement);
93-
$stmt->execute();
94-
95-
return $stmt->rowCount();
86+
return $this->prepare($sql)->execute()->rowCount();
9687
}
9788

9889
/**
9990
* {@inheritDoc}
10091
*/
10192
public function lastInsertId($name = null)
10293
{
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__);
12895
}
12996

13097
/**
13198
* {@inheritDoc}
13299
*/
133-
public function errorCode() : ?string
100+
public function beginTransaction(): bool
134101
{
135-
throw ClickHouseException::notSupported('You need to implement ClickHouseConnection::errorCode()');
102+
throw Exception::notSupported(__METHOD__);
136103
}
137104

138105
/**
139106
* {@inheritDoc}
140107
*/
141-
public function errorInfo() : array
108+
public function commit(): bool
142109
{
143-
throw ClickHouseException::notSupported('You need to implement ClickHouseConnection::errorInfo()');
110+
throw Exception::notSupported(__METHOD__);
144111
}
145112

146113
/**
147114
* {@inheritDoc}
148115
*/
149-
public function ping() : bool
116+
public function rollBack(): bool
150117
{
151-
return $this->smi2CHClient->ping();
118+
throw Exception::notSupported(__METHOD__);
152119
}
153120

154121
/**
155122
* {@inheritDoc}
156123
*/
157-
public function getServerVersion() : string
124+
public function getServerVersion(): string
158125
{
159126
try {
160-
return $this->smi2CHClient->getServerVersion();
161-
} catch (TransportException $e) {
127+
return $this->client->getServerVersion();
128+
} catch (ClickHouseException) {
162129
return '';
163130
}
164131
}
165-
166-
/**
167-
* {@inheritDoc}
168-
*/
169-
public function requiresQueryForServerVersion() : bool
170-
{
171-
return true;
172-
}
173132
}

src/ClickHouseException.php renamed to src/ClickHouseExceptionConverter.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
namespace FOD\DBALClickHouse;
1616

17-
use Doctrine\DBAL\Exception;
17+
use Doctrine\DBAL\Driver\API\ExceptionConverter;
18+
use Doctrine\DBAL\Driver\Exception;
19+
use Doctrine\DBAL\Exception\DriverException;
20+
use Doctrine\DBAL\Query;
1821

19-
/**
20-
* Specific Exception for ClickHouse
21-
*/
22-
class ClickHouseException extends Exception
22+
class ClickHouseExceptionConverter implements ExceptionConverter
2323
{
24-
public static function notSupported($method) : ClickHouseException
24+
public function convert(Exception $exception, ?Query $query): DriverException
2525
{
26-
return new self(sprintf("Operation '%s' is not supported by platform.", $method));
26+
return new DriverException($exception, $query);
2727
}
2828
}

src/ClickHouseKeywords.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,14 @@
1616

1717
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
1818

19-
/**
20-
* ClickHouse Keywordlist
21-
*/
2219
class ClickHouseKeywords extends KeywordList
2320
{
2421
/**
2522
* {@inheritdoc}
2623
*/
27-
public function getName() : string
28-
{
29-
return 'ClickHouse';
30-
}
31-
32-
/**
33-
* {@inheritdoc}
34-
*/
35-
protected function getKeywords() : array
24+
protected function getKeywords(): array
3625
{
37-
//TODO actualize it!
26+
// @todo actualize it!
3827
return [
3928
'ADD',
4029
'ALL',
@@ -266,4 +255,12 @@ protected function getKeywords() : array
266255
'ANY',
267256
];
268257
}
258+
259+
/**
260+
* {@inheritdoc}
261+
*/
262+
public function getName(): string
263+
{
264+
return 'ClickHouse';
265+
}
269266
}

0 commit comments

Comments
 (0)