Skip to content

Commit

Permalink
Added Doctrine DBAL 3 support (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihor Tymoshenko authored Sep 13, 2023
1 parent 74536d6 commit 9a2b0dd
Show file tree
Hide file tree
Showing 40 changed files with 1,426 additions and 1,463 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.1', '7.4', '8.0', '8.1']
php-versions: ['8.0', '8.1']
fail-fast: false

services:
Expand All @@ -45,4 +45,4 @@ jobs:
run: composer install --prefer-dist

- name: Run tests
run: ./vendor/bin/phpunit --configuration ./phpunit.xml.dist --coverage-text
run: ./vendor/bin/phpunit --configuration ./phpunit.xml.dist --coverage-text
13 changes: 5 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.idea/
composer.lock
vendor/
phpunit.xml
.phpunit.result.cache
Vagrantfile
puphpet/
.vagrant/
/.idea/
/vendor/
/.phpunit.result.cache
/composer.lock
/phpunit.xml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ doctrine:
array(uint64): FOD\DBALClickHouse\Types\ArrayUInt64Type
array(float32): FOD\DBALClickHouse\Types\ArrayFloat32Type
array(float64): FOD\DBALClickHouse\Types\ArrayFloat64Type
array(string): FOD\DBALClickHouse\Types\ArrayStringType
array(string): FOD\DBALClickHouse\Types\ArrayStringableType
array(datetime): FOD\DBALClickHouse\Types\ArrayDateTimeType
array(date): FOD\DBALClickHouse\Types\ArrayDateType
```
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
}
],
"require": {
"php": "^7.1 || ^8.0",
"ext-pdo": "*",
"php": "^8.0",
"ext-pcre": "*",
"doctrine/dbal": "^2.7",
"ext-mbstring": "*",
"doctrine/dbal": "^3.0",
"smi2/phpclickhouse": "^1.0"
},
"require-dev": {
"doctrine/coding-standard": "^4.0 || ^9.0",
"phpunit/phpunit": "^7.0 || ^9.0"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand Down
119 changes: 39 additions & 80 deletions src/ClickHouseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,160 +14,119 @@

namespace FOD\DBALClickHouse;

use ClickHouseDB\Client as Smi2CHClient;
use ClickHouseDB\Exception\TransportException;
use ClickHouseDB\Client;
use ClickHouseDB\Exception\ClickHouseException;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

use function array_merge;
use function func_get_args;

/**
* ClickHouse implementation for the Connection interface.
*/
class ClickHouseConnection implements Connection, PingableConnection, ServerInfoAwareConnection
class ClickHouseConnection implements Connection, ServerInfoAwareConnection
{
/** @var Smi2CHClient */
protected $smi2CHClient;
protected Client $client;

/** @var AbstractPlatform */
protected $platform;
protected AbstractPlatform $platform;

public function __construct(
array $params,
string $username,
string $user,
string $password,
AbstractPlatform $platform
) {
$this->smi2CHClient = new Smi2CHClient([
'host' => $params['host'] ?? 'localhost',
'port' => $params['port'] ?? 8123,
'username' => $username,
'password' => $password,
], array_merge([
'database' => $params['dbname'] ?? 'default',
], $params['driverOptions'] ?? []));
$this->client = new Client(
[
'host' => $params['host'] ?? 'localhost',
'port' => $params['port'] ?? 8123,
'username' => $user,
'password' => $password,
],
array_merge(['database' => $params['dbname'] ?? 'default'], $params['driverOptions'] ?? [])
);
$this->platform = $platform;
}

/**
* {@inheritDoc}
*/
public function prepare($prepareString) : ClickHouseStatement
public function prepare(string $sql): Statement
{
return new ClickHouseStatement($this->smi2CHClient, $prepareString, $this->platform);
return new ClickHouseStatement($this->client, $sql, $this->platform);
}

/**
* {@inheritDoc}
*/
public function query() : ClickHouseStatement
public function query(string $sql): Result
{
$args = func_get_args();
$stmt = $this->prepare($args[0]);
$stmt->execute();

return $stmt;
return $this->prepare($sql)->execute();
}

/**
* {@inheritDoc}
*/
public function quote($input, $type = ParameterType::STRING)
public function quote($value, $type = ParameterType::STRING)
{
if ($type === ParameterType::INTEGER) {
return $input;
if ($type === ParameterType::STRING) {
return $this->platform->quoteStringLiteral($value);
}

return $this->platform->quoteStringLiteral($input);
return $value;
}

/**
* {@inheritDoc}
*/
public function exec($statement) : int
public function exec(string $sql): int
{
$stmt = $this->prepare($statement);
$stmt->execute();

return $stmt->rowCount();
return $this->prepare($sql)->execute()->rowCount();
}

/**
* {@inheritDoc}
*/
public function lastInsertId($name = null)
{
throw ClickHouseException::notSupported('Unable to get last insert id in ClickHouse');
}

/**
* {@inheritDoc}
*/
public function beginTransaction() : bool
{
throw ClickHouseException::notSupported('Transactions are not allowed in ClickHouse');
}

/**
* {@inheritDoc}
*/
public function commit() : bool
{
throw ClickHouseException::notSupported('Transactions are not allowed in ClickHouse');
}

/**
* {@inheritDoc}
*/
public function rollBack() : bool
{
throw ClickHouseException::notSupported('Transactions are not allowed in ClickHouse');
throw Exception::notSupported(__METHOD__);
}

/**
* {@inheritDoc}
*/
public function errorCode() : ?string
public function beginTransaction(): bool
{
throw ClickHouseException::notSupported('You need to implement ClickHouseConnection::errorCode()');
throw Exception::notSupported(__METHOD__);
}

/**
* {@inheritDoc}
*/
public function errorInfo() : array
public function commit(): bool
{
throw ClickHouseException::notSupported('You need to implement ClickHouseConnection::errorInfo()');
throw Exception::notSupported(__METHOD__);
}

/**
* {@inheritDoc}
*/
public function ping() : bool
public function rollBack(): bool
{
return $this->smi2CHClient->ping();
throw Exception::notSupported(__METHOD__);
}

/**
* {@inheritDoc}
*/
public function getServerVersion() : string
public function getServerVersion(): string
{
try {
return $this->smi2CHClient->getServerVersion();
} catch (TransportException $e) {
return $this->client->getServerVersion();
} catch (ClickHouseException) {
return '';
}
}

/**
* {@inheritDoc}
*/
public function requiresQueryForServerVersion() : bool
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

namespace FOD\DBALClickHouse;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Query;

/**
* Specific Exception for ClickHouse
*/
class ClickHouseException extends Exception
class ClickHouseExceptionConverter implements ExceptionConverter
{
public static function notSupported($method) : ClickHouseException
public function convert(Exception $exception, ?Query $query): DriverException
{
return new self(sprintf("Operation '%s' is not supported by platform.", $method));
return new DriverException($exception, $query);
}
}
23 changes: 10 additions & 13 deletions src/ClickHouseKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,14 @@

use Doctrine\DBAL\Platforms\Keywords\KeywordList;

/**
* ClickHouse Keywordlist
*/
class ClickHouseKeywords extends KeywordList
{
/**
* {@inheritdoc}
*/
public function getName() : string
{
return 'ClickHouse';
}

/**
* {@inheritdoc}
*/
protected function getKeywords() : array
protected function getKeywords(): array
{
//TODO actualize it!
// @todo actualize it!
return [
'ADD',
'ALL',
Expand Down Expand Up @@ -266,4 +255,12 @@ protected function getKeywords() : array
'ANY',
];
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'ClickHouse';
}
}
Loading

0 comments on commit 9a2b0dd

Please sign in to comment.