From 421f2be752a6c0af0254f8ccd1ec125f2a458dc0 Mon Sep 17 00:00:00 2001 From: Philip Date: Sat, 29 Dec 2018 15:29:25 +0300 Subject: [PATCH 1/8] Add new DBAL types. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлены дополнительные DBAL типы для удобной работы с числовыми параметрами. --- README.md | 23 +++++--- src/ClickHouseSchemaManager.php | 2 + src/Types/Float32Type.php | 38 +++++++++++++ src/Types/Float64Type.php | 38 +++++++++++++ src/Types/Int16Type.php | 38 +++++++++++++ src/Types/Int32Type.php | 38 +++++++++++++ src/Types/Int64Type.php | 38 +++++++++++++ src/Types/Int8Type.php | 38 +++++++++++++ src/Types/Type.php | 97 +++++++++++++++++++++++++++++++++ 9 files changed, 342 insertions(+), 8 deletions(-) create mode 100644 src/Types/Float32Type.php create mode 100644 src/Types/Float64Type.php create mode 100644 src/Types/Int16Type.php create mode 100644 src/Types/Int32Type.php create mode 100644 src/Types/Int64Type.php create mode 100644 src/Types/Int8Type.php create mode 100644 src/Types/Type.php diff --git a/README.md b/README.md index e559ab8..918980a 100644 --- a/README.md +++ b/README.md @@ -195,19 +195,26 @@ doctrine: array(date): FOD\DBALClickHouse\Types\ArrayDateType ``` -Additional type `BigIntType` helps you to store bigint values as [Int64/UInt64](https://clickhouse.yandex/reference_en.html#UInt8,%20UInt16,%20UInt32,%20UInt64,%20Int8,%20Int16,%20Int32,%20Int64) value type in ClickHouse. -You can override DBAL type in your code: -```php -Type::overrideType(Type::BIGINT, 'FOD\DBALClickHouse\Types\BigIntType'); -``` -or use custom mapping types in Symfony configuration: +If you want to use [numeric types](https://clickhouse.yandex/docs/en/single/#uint8-uint16-uint32-uint64-int8-int16-int32-int64), register additional DBAL types in your Symfony configuration file: + ```yml # app/config/config.yml doctrine: dbal: + connections: + ... types: - bigint: FOD\DBALClickHouse\Types\BigIntType - ... + int8: FOD\DBALClickHouse\Types\Int8Type + int16: FOD\DBALClickHouse\Types\Int16Type + int32: FOD\DBALClickHouse\Types\Int32Type + int64: FOD\DBALClickHouse\Types\Int64Type + float32: FOD\DBALClickHouse\Types\Float32Type + float64: FOD\DBALClickHouse\Types\Float64Type +``` + +or you can override DBAL type in your code: +```php +Type::overrideType(Type::BIGINT, 'FOD\DBALClickHouse\Types\Int64Type'); ``` ### More information in Doctrine DBAL documentation: diff --git a/src/ClickHouseSchemaManager.php b/src/ClickHouseSchemaManager.php index 5607c31..17d6268 100644 --- a/src/ClickHouseSchemaManager.php +++ b/src/ClickHouseSchemaManager.php @@ -120,6 +120,8 @@ protected function _getPortableTableColumnDefinition($tableColumn) : Column $unsigned = false; if (stripos($columnType, 'uint') === 0) { $unsigned = true; + // getting correct name of integer column + $dbType = substr($columnType, 1); } if (! isset($tableColumn['name'])) { diff --git a/src/Types/Float32Type.php b/src/Types/Float32Type.php new file mode 100644 index 0000000..78b819f --- /dev/null +++ b/src/Types/Float32Type.php @@ -0,0 +1,38 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +/** + * Float32 Type + */ +class Float32Type extends Type implements BitNumericalClickHouseType +{ + public function getBits() : int + { + return BitNumericalClickHouseType::THIRTY_TWO_BIT; + } + + public function getBaseClickHouseType() : string + { + return NumericalClickHouseType::TYPE_FLOAT; + } + + /** + * {@inheritdoc} + */ + protected function getDeclaration(array $fieldDeclaration = []) : string + { + + return $this->getBaseClickHouseType(). $this->getBits(); + } +} diff --git a/src/Types/Float64Type.php b/src/Types/Float64Type.php new file mode 100644 index 0000000..b224ce4 --- /dev/null +++ b/src/Types/Float64Type.php @@ -0,0 +1,38 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +/** + * Float64 Type + */ +class Float64Type extends Type implements BitNumericalClickHouseType +{ + public function getBits() : int + { + return BitNumericalClickHouseType::SIXTY_FOUR_BIT; + } + + public function getBaseClickHouseType() : string + { + return NumericalClickHouseType::TYPE_FLOAT; + } + + /** + * {@inheritdoc} + */ + protected function getDeclaration(array $fieldDeclaration = []) : string + { + + return $this->getBaseClickHouseType(). $this->getBits(); + } +} diff --git a/src/Types/Int16Type.php b/src/Types/Int16Type.php new file mode 100644 index 0000000..8dbbe95 --- /dev/null +++ b/src/Types/Int16Type.php @@ -0,0 +1,38 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +/** + * Int16 Type + */ +class Int16Type extends Type implements BitNumericalClickHouseType +{ + public function getBits() : int + { + return BitNumericalClickHouseType::SIXTEEN_BIT; + } + + public function getBaseClickHouseType() : string + { + return NumericalClickHouseType::TYPE_INT; + } + + /** + * {@inheritdoc} + */ + protected function getDeclaration(array $fieldDeclaration = []) : string + { + + return (empty($fieldDeclaration['unsigned']) ? '' : 'U') . $this->getBaseClickHouseType(). $this->getBits(); + } +} diff --git a/src/Types/Int32Type.php b/src/Types/Int32Type.php new file mode 100644 index 0000000..f6e139a --- /dev/null +++ b/src/Types/Int32Type.php @@ -0,0 +1,38 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +/** + * Int32 Type + */ +class Int32Type extends Type implements BitNumericalClickHouseType +{ + public function getBits() : int + { + return BitNumericalClickHouseType::THIRTY_TWO_BIT; + } + + public function getBaseClickHouseType() : string + { + return NumericalClickHouseType::TYPE_INT; + } + + /** + * {@inheritdoc} + */ + protected function getDeclaration(array $fieldDeclaration = []) : string + { + + return (empty($fieldDeclaration['unsigned']) ? '' : 'U') . $this->getBaseClickHouseType(). $this->getBits(); + } +} diff --git a/src/Types/Int64Type.php b/src/Types/Int64Type.php new file mode 100644 index 0000000..f28490a --- /dev/null +++ b/src/Types/Int64Type.php @@ -0,0 +1,38 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +/** + * Int64 Type + */ +class Int64Type extends Type implements BitNumericalClickHouseType +{ + public function getBits() : int + { + return BitNumericalClickHouseType::SIXTY_FOUR_BIT; + } + + public function getBaseClickHouseType() : string + { + return NumericalClickHouseType::TYPE_INT; + } + + /** + * {@inheritdoc} + */ + protected function getDeclaration(array $fieldDeclaration = []) : string + { + + return (empty($fieldDeclaration['unsigned']) ? '' : 'U') . $this->getBaseClickHouseType(). $this->getBits(); + } +} diff --git a/src/Types/Int8Type.php b/src/Types/Int8Type.php new file mode 100644 index 0000000..cdc640c --- /dev/null +++ b/src/Types/Int8Type.php @@ -0,0 +1,38 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +/** + * Int8 Type + */ +class Int8Type extends Type implements BitNumericalClickHouseType +{ + public function getBits() : int + { + return BitNumericalClickHouseType::EIGHT_BIT; + } + + public function getBaseClickHouseType() : string + { + return NumericalClickHouseType::TYPE_INT; + } + + /** + * {@inheritdoc} + */ + protected function getDeclaration(array $fieldDeclaration = []) : string + { + + return (empty($fieldDeclaration['unsigned']) ? '' : 'U') . $this->getBaseClickHouseType(). $this->getBits(); + } +} diff --git a/src/Types/Type.php b/src/Types/Type.php new file mode 100644 index 0000000..e4e8c35 --- /dev/null +++ b/src/Types/Type.php @@ -0,0 +1,97 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Platforms\AbstractPlatform; +use function array_key_exists; +use function sprintf; +use function strtolower; + +/** + * Clickhouse types basic class + */ +abstract class Type extends \Doctrine\DBAL\Types\Type implements ClickHouseType +{ + + public const INT8 = 'int8'; + public const INT16 = 'int16'; + public const INT32 = 'int32'; + public const INT64 = 'int64'; + public const FLOAT32 = 'float32'; + public const FLOAT64 = 'float64'; + + protected const TYPES = [ + self::INT8 => Int8Type::class, + self::INT16 => Int16Type::class, + self::INT32 => Int32Type::class, + self::INT64 => Int64Type::class, + self::FLOAT32 => Float32Type::class, + self::FLOAT64 => Float64Type::class + ]; + + /** + * Register Array types to the type map. + * + * @param AbstractPlatform $platform + * @return void + * + * @throws DBALException + */ + public static function registerTypes(AbstractPlatform $platform) : void + { + foreach (self::TYPES as $typeName => $className) { + if (self::hasType($typeName)) { + continue; + } + + self::addType($typeName, $className); + foreach (Type::getType($typeName)->getMappedDatabaseTypes($platform) as $dbType) { + $platform->registerDoctrineTypeMapping($dbType, $typeName); + } + } + } + + /** + * {@inheritDoc} + */ + public function getMappedDatabaseTypes(AbstractPlatform $platform) : array + { + return [$this->getName()]; + } + + /** + * {@inheritDoc} + */ + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string + { + return $this->getDeclaration($fieldDeclaration); + } + + /** + * {@inheritDoc} + */ + public function getName() : string + { + return strtolower($this->getDeclaration()); + } + + /** + * @param mixed[] $fieldDeclaration + * + * @return string $declaration + * + * @throws DBALException + */ + abstract protected function getDeclaration(array $fieldDeclaration = []) : string; +} From ceeadd1d87a00f298ce7a12caf4797c7a238fc44 Mon Sep 17 00:00:00 2001 From: Philip Date: Sat, 29 Dec 2018 15:30:19 +0300 Subject: [PATCH 2/8] Add tests DBAL types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлено тестирование дополниительных DBAL типов --- tests/TypesTest.php | 228 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 tests/TypesTest.php diff --git a/tests/TypesTest.php b/tests/TypesTest.php new file mode 100644 index 0000000..2383fc2 --- /dev/null +++ b/tests/TypesTest.php @@ -0,0 +1,228 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license inflormation, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Tests; + +use \FOD\DBALClickHouse\Types\Type; +use FOD\DBALClickHouse\Connection; +use PHPUnit\Framework\TestCase; + +/** + * ClickHouse DBAL test class. Testing work with DBAL types + * + * @author Philip Shcherbanich + */ +class TypesTest extends TestCase +{ + /** @var Connection */ + protected $connection; + + protected $schemaSQLs = []; + + protected $tableName = '_test_type_table'; + + public function setUp() + { + $this->connection = CreateConnectionTest::createConnection(); + Type::registerTypes($this->connection->getDatabasePlatform()); + } + + public function tearDown() + { + $this->connection->exec("DROP TABLE {$this->tableName}"); + } + + public function testInt8() + { + + $fieldName = 'int8'; + + $neededFieldSQLDeclaration = 'Int8'; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration([], $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testInt16() + { + + $fieldName = 'int16'; + + $neededFieldSQLDeclaration = 'Int16'; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration([], $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testInt32() + { + + $fieldName = 'int32'; + + $neededFieldSQLDeclaration = 'Int32'; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration([], $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testInt64() + { + + $fieldName = 'int64'; + + $neededFieldSQLDeclaration = 'Int64'; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration([], $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + + public function testUInt8() + { + + $fieldName = 'int8'; + + $neededFieldSQLDeclaration = 'UInt8'; + + $options = ['unsigned' => true]; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration($options, $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName, $options); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testUInt16() + { + + $fieldName = 'int16'; + + $neededFieldSQLDeclaration = 'UInt16'; + + $options = ['unsigned' => true]; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration($options, $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName, $options); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testUInt32() + { + + $fieldName = 'int32'; + + $neededFieldSQLDeclaration = 'UInt32'; + + $options = ['unsigned' => true]; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration($options, $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName, $options); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testUInt64() + { + + $fieldName = 'int64'; + + $neededFieldSQLDeclaration = 'UInt64'; + + $options = ['unsigned' => true]; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration($options, $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName, $options); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testFloat32() + { + + $fieldName = 'float32'; + + $neededFieldSQLDeclaration = 'Float32'; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration([], $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + public function testFloat64() + { + + $fieldName = 'float64'; + + $neededFieldSQLDeclaration = 'Float64'; + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration([], $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + + protected function createTempTable($type, $options = []) + { + $fromSchema = $this->connection->getSchemaManager()->createSchema(); + $toSchema = clone $fromSchema; + if ($toSchema->hasTable($this->tableName) || $fromSchema->hasTable($this->tableName)) { + $this->connection->exec("DROP TABLE {$this->tableName}"); + } + $newTable = $toSchema->createTable($this->tableName); + + $newTable->addColumn('field', $type, $options); + $newTable->addOption('engine', 'Memory'); + + foreach ($fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform()) as $sql) { + $this->connection->exec($sql); + } + } +} From 4bf04ef6c8404cc85ed326bfa0adacba4aff83c4 Mon Sep 17 00:00:00 2001 From: Philip Date: Sat, 29 Dec 2018 15:32:47 +0300 Subject: [PATCH 3/8] Fixed spelling --- src/ClickHouseConnection.php | 2 +- src/ClickHouseException.php | 2 +- src/ClickHouseKeywords.php | 2 +- src/ClickHousePlatform.php | 2 +- src/ClickHouseSchemaManager.php | 2 +- src/ClickHouseStatement.php | 2 +- src/Connection.php | 2 +- src/Driver.php | 2 +- src/Types/ArrayDateTimeType.php | 2 +- src/Types/ArrayDateType.php | 2 +- src/Types/ArrayFloat32Type.php | 2 +- src/Types/ArrayFloat64Type.php | 2 +- src/Types/ArrayInt16Type.php | 2 +- src/Types/ArrayInt32Type.php | 2 +- src/Types/ArrayInt64Type.php | 2 +- src/Types/ArrayInt8Type.php | 2 +- src/Types/ArrayStringType.php | 2 +- src/Types/ArrayType.php | 2 +- src/Types/ArrayUInt16Type.php | 2 +- src/Types/ArrayUInt32Type.php | 2 +- src/Types/ArrayUInt64Type.php | 2 +- src/Types/ArrayUInt8Type.php | 2 +- src/Types/BigIntType.php | 2 +- src/Types/BitNumericalClickHouseType.php | 2 +- src/Types/ClickHouseType.php | 2 +- src/Types/DatableClickHouseType.php | 2 +- src/Types/NumericalClickHouseType.php | 2 +- src/Types/StringClickHouseType.php | 2 +- src/Types/UnsignedNumericalClickHouseType.php | 2 +- tests/ArraysTest.php | 2 +- tests/ConnectionTest.php | 2 +- tests/CreateConnectionTest.php | 2 +- tests/CreateSchemaTest.php | 2 +- tests/DbalTypeTest.php | 2 +- tests/DriverTest.php | 2 +- tests/InsertTest.php | 2 +- tests/SelectTest.php | 2 +- tests/TypesTest.php | 2 +- 38 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/ClickHouseConnection.php b/src/ClickHouseConnection.php index 15cd109..a3786a3 100644 --- a/src/ClickHouseConnection.php +++ b/src/ClickHouseConnection.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/ClickHouseException.php b/src/ClickHouseException.php index 1b8be8c..0e3128e 100644 --- a/src/ClickHouseException.php +++ b/src/ClickHouseException.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/ClickHouseKeywords.php b/src/ClickHouseKeywords.php index 9109103..b375b7c 100644 --- a/src/ClickHouseKeywords.php +++ b/src/ClickHouseKeywords.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/ClickHousePlatform.php b/src/ClickHousePlatform.php index 7962d96..ccae5f1 100644 --- a/src/ClickHousePlatform.php +++ b/src/ClickHousePlatform.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/ClickHouseSchemaManager.php b/src/ClickHouseSchemaManager.php index 17d6268..dacd861 100644 --- a/src/ClickHouseSchemaManager.php +++ b/src/ClickHouseSchemaManager.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/ClickHouseStatement.php b/src/ClickHouseStatement.php index 9587257..3f5e7c6 100644 --- a/src/ClickHouseStatement.php +++ b/src/ClickHouseStatement.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Connection.php b/src/Connection.php index 80f8e72..6cfb4de 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Driver.php b/src/Driver.php index 65ac5f7..7ec6d87 100644 --- a/src/Driver.php +++ b/src/Driver.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayDateTimeType.php b/src/Types/ArrayDateTimeType.php index 76cf715..4eb63f9 100644 --- a/src/Types/ArrayDateTimeType.php +++ b/src/Types/ArrayDateTimeType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayDateType.php b/src/Types/ArrayDateType.php index f96829d..54eab28 100644 --- a/src/Types/ArrayDateType.php +++ b/src/Types/ArrayDateType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayFloat32Type.php b/src/Types/ArrayFloat32Type.php index fcc0b27..77eac6a 100644 --- a/src/Types/ArrayFloat32Type.php +++ b/src/Types/ArrayFloat32Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayFloat64Type.php b/src/Types/ArrayFloat64Type.php index 01c8790..d2924f9 100644 --- a/src/Types/ArrayFloat64Type.php +++ b/src/Types/ArrayFloat64Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayInt16Type.php b/src/Types/ArrayInt16Type.php index 0c78d76..7b6faf1 100644 --- a/src/Types/ArrayInt16Type.php +++ b/src/Types/ArrayInt16Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayInt32Type.php b/src/Types/ArrayInt32Type.php index 201278e..5df7fed 100644 --- a/src/Types/ArrayInt32Type.php +++ b/src/Types/ArrayInt32Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayInt64Type.php b/src/Types/ArrayInt64Type.php index 2e1860d..591708d 100644 --- a/src/Types/ArrayInt64Type.php +++ b/src/Types/ArrayInt64Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayInt8Type.php b/src/Types/ArrayInt8Type.php index 9f18e6f..f3db1f9 100644 --- a/src/Types/ArrayInt8Type.php +++ b/src/Types/ArrayInt8Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayStringType.php b/src/Types/ArrayStringType.php index 8b39e5f..65fcd90 100644 --- a/src/Types/ArrayStringType.php +++ b/src/Types/ArrayStringType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayType.php b/src/Types/ArrayType.php index 2c185fc..a7a03dc 100644 --- a/src/Types/ArrayType.php +++ b/src/Types/ArrayType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayUInt16Type.php b/src/Types/ArrayUInt16Type.php index 3fb8d4c..89e00bf 100644 --- a/src/Types/ArrayUInt16Type.php +++ b/src/Types/ArrayUInt16Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayUInt32Type.php b/src/Types/ArrayUInt32Type.php index ef6794c..d2a609e 100644 --- a/src/Types/ArrayUInt32Type.php +++ b/src/Types/ArrayUInt32Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayUInt64Type.php b/src/Types/ArrayUInt64Type.php index a21c280..7fff264 100644 --- a/src/Types/ArrayUInt64Type.php +++ b/src/Types/ArrayUInt64Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ArrayUInt8Type.php b/src/Types/ArrayUInt8Type.php index 626eafa..7692369 100644 --- a/src/Types/ArrayUInt8Type.php +++ b/src/Types/ArrayUInt8Type.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/BigIntType.php b/src/Types/BigIntType.php index ace53cf..8b479c5 100644 --- a/src/Types/BigIntType.php +++ b/src/Types/BigIntType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/BitNumericalClickHouseType.php b/src/Types/BitNumericalClickHouseType.php index ab843b8..8a1f8dc 100644 --- a/src/Types/BitNumericalClickHouseType.php +++ b/src/Types/BitNumericalClickHouseType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/ClickHouseType.php b/src/Types/ClickHouseType.php index 9f12543..ef56712 100644 --- a/src/Types/ClickHouseType.php +++ b/src/Types/ClickHouseType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/DatableClickHouseType.php b/src/Types/DatableClickHouseType.php index e2e7e5a..c82b069 100644 --- a/src/Types/DatableClickHouseType.php +++ b/src/Types/DatableClickHouseType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/NumericalClickHouseType.php b/src/Types/NumericalClickHouseType.php index ebee8ed..70ce5af 100644 --- a/src/Types/NumericalClickHouseType.php +++ b/src/Types/NumericalClickHouseType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/StringClickHouseType.php b/src/Types/StringClickHouseType.php index 524d7e9..2705923 100644 --- a/src/Types/StringClickHouseType.php +++ b/src/Types/StringClickHouseType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/src/Types/UnsignedNumericalClickHouseType.php b/src/Types/UnsignedNumericalClickHouseType.php index f17132e..70e8291 100644 --- a/src/Types/UnsignedNumericalClickHouseType.php +++ b/src/Types/UnsignedNumericalClickHouseType.php @@ -8,7 +8,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/ArraysTest.php b/tests/ArraysTest.php index dfc412d..7e3558d 100644 --- a/tests/ArraysTest.php +++ b/tests/ArraysTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 5603b10..ac5864b 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/CreateConnectionTest.php b/tests/CreateConnectionTest.php index 169049f..a7c1a29 100644 --- a/tests/CreateConnectionTest.php +++ b/tests/CreateConnectionTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/CreateSchemaTest.php b/tests/CreateSchemaTest.php index 5abdac6..a7103ca 100644 --- a/tests/CreateSchemaTest.php +++ b/tests/CreateSchemaTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/DbalTypeTest.php b/tests/DbalTypeTest.php index e9c2375..fb0ad2f 100644 --- a/tests/DbalTypeTest.php +++ b/tests/DbalTypeTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/DriverTest.php b/tests/DriverTest.php index ed1e11b..ca5d849 100644 --- a/tests/DriverTest.php +++ b/tests/DriverTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/InsertTest.php b/tests/InsertTest.php index 59be336..6ca6d60 100644 --- a/tests/InsertTest.php +++ b/tests/InsertTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/SelectTest.php b/tests/SelectTest.php index 52e1dba..78cf9af 100644 --- a/tests/SelectTest.php +++ b/tests/SelectTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 2383fc2..111e279 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -5,7 +5,7 @@ * * (c) FriendsOfDoctrine . * - * For the full copyright and license inflormation, please view the LICENSE + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ From 30639389ac58b82cae6442992151fdd5502deb4b Mon Sep 17 00:00:00 2001 From: Philip Date: Sat, 29 Dec 2018 18:41:14 +0300 Subject: [PATCH 4/8] Add decimal type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавление дополнительного типа decimal. --- README.md | 3 ++ src/ClickHouseSchemaManager.php | 23 +++++++++++++- src/Types/DecimalType.php | 44 +++++++++++++++++++++++++++ src/Types/NumericalClickHouseType.php | 1 + src/Types/Type.php | 6 ++-- tests/TypesTest.php | 18 +++++++++++ 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/Types/DecimalType.php diff --git a/README.md b/README.md index 918980a..74427d3 100644 --- a/README.md +++ b/README.md @@ -210,11 +210,14 @@ doctrine: int64: FOD\DBALClickHouse\Types\Int64Type float32: FOD\DBALClickHouse\Types\Float32Type float64: FOD\DBALClickHouse\Types\Float64Type + decimal: FOD\DBALClickHouse\Types\DecimalType ``` or you can override DBAL type in your code: ```php Type::overrideType(Type::BIGINT, 'FOD\DBALClickHouse\Types\Int64Type'); + +Type::overrideType(Type::DECIMAL, 'FOD\DBALClickHouse\Types\DecimalType'); ``` ### More information in Doctrine DBAL documentation: diff --git a/src/ClickHouseSchemaManager.php b/src/ClickHouseSchemaManager.php index dacd861..1ebc3fb 100644 --- a/src/ClickHouseSchemaManager.php +++ b/src/ClickHouseSchemaManager.php @@ -118,12 +118,31 @@ protected function _getPortableTableColumnDefinition($tableColumn) : Column } $unsigned = false; + if (stripos($columnType, 'uint') === 0) { + $unsigned = true; - // getting correct name of integer column + $dbType = substr($columnType, 1); } + $precision = 10; + + $scale = 0; + + if (stripos($columnType, 'decimal') === 0) { + + $unsigned = false; + + $dbType = 'decimal'; + + preg_match('/([0-9]{1,2})(, )?([0-9]{1,2})?/', $columnType, $matches); + + $precision = isset($matches[1]) ? $matches[1] : 10; + + $scale = isset($matches[3]) ? $matches[3] : 0; + } + if (! isset($tableColumn['name'])) { $tableColumn['name'] = ''; } @@ -143,6 +162,8 @@ protected function _getPortableTableColumnDefinition($tableColumn) : Column 'unsigned' => $unsigned, 'autoincrement' => false, 'comment' => null, + 'precision' => $precision, + 'scale' => $scale, ]; return new Column( diff --git a/src/Types/DecimalType.php b/src/Types/DecimalType.php new file mode 100644 index 0000000..10bbda5 --- /dev/null +++ b/src/Types/DecimalType.php @@ -0,0 +1,44 @@ +) + * + * (c) FriendsOfDoctrine . + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOD\DBALClickHouse\Types; + +/** + * Decimal Type + */ +class DecimalType extends Type +{ + + public function getBaseClickHouseType() : string + { + return NumericalClickHouseType::TYPE_DECIMAL; + } + + public function getName(): string + { + return Type::DECIMAL; + } + + /** + * {@inheritdoc} + */ + protected function getDeclaration(array $fieldDeclaration = []) : string + { + + $fieldDeclaration['precision'] = ! isset($fieldDeclaration['precision']) || empty($fieldDeclaration['precision']) + ? 10 : $fieldDeclaration['precision']; + $fieldDeclaration['scale'] = ! isset($fieldDeclaration['scale']) || empty($fieldDeclaration['scale']) + ? 0 : $fieldDeclaration['scale']; + + return $this->getBaseClickHouseType(). "({$fieldDeclaration['precision']}, {$fieldDeclaration['scale']})"; + } +} diff --git a/src/Types/NumericalClickHouseType.php b/src/Types/NumericalClickHouseType.php index 70ce5af..c47fc3e 100644 --- a/src/Types/NumericalClickHouseType.php +++ b/src/Types/NumericalClickHouseType.php @@ -18,4 +18,5 @@ interface NumericalClickHouseType extends ClickHouseType { public const TYPE_INT = 'Int'; public const TYPE_FLOAT = 'Float'; + public const TYPE_DECIMAL = 'Decimal'; } diff --git a/src/Types/Type.php b/src/Types/Type.php index e4e8c35..754763f 100644 --- a/src/Types/Type.php +++ b/src/Types/Type.php @@ -14,8 +14,6 @@ use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Platforms\AbstractPlatform; -use function array_key_exists; -use function sprintf; use function strtolower; /** @@ -30,6 +28,7 @@ abstract class Type extends \Doctrine\DBAL\Types\Type implements ClickHouseType public const INT64 = 'int64'; public const FLOAT32 = 'float32'; public const FLOAT64 = 'float64'; + public const DECIMAL = 'decimal'; protected const TYPES = [ self::INT8 => Int8Type::class, @@ -37,7 +36,8 @@ abstract class Type extends \Doctrine\DBAL\Types\Type implements ClickHouseType self::INT32 => Int32Type::class, self::INT64 => Int64Type::class, self::FLOAT32 => Float32Type::class, - self::FLOAT64 => Float64Type::class + self::FLOAT64 => Float64Type::class, + self::DECIMAL => DecimalType::class ]; /** diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 111e279..35605b3 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -209,6 +209,24 @@ public function testFloat64() $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); } + public function testDecimal() + { + + $fieldName = 'decimal'; + + $neededFieldSQLDeclaration = 'Decimal(10, 0)'; + + Type::overrideType(Type::DECIMAL, 'FOD\DBALClickHouse\Types\DecimalType'); + + $fieldSQLDeclaration = Type::getType($fieldName)->getSQLDeclaration([], $this->connection->getDatabasePlatform()); + + $this->assertEquals($fieldSQLDeclaration, $neededFieldSQLDeclaration); + + $this->createTempTable($fieldName); + + $this->assertIsNotBool(strpos($this->connection->fetchColumn("SHOW CREATE {$this->tableName}"), "field {$fieldSQLDeclaration}")); + } + protected function createTempTable($type, $options = []) { $fromSchema = $this->connection->getSchemaManager()->createSchema(); From 0a1678b3ab392845f7bd0ed2c917dee5aaa21ffc Mon Sep 17 00:00:00 2001 From: Philip Date: Sat, 29 Dec 2018 22:20:02 +0300 Subject: [PATCH 5/8] Allow to set default value for eventDateColumn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавление возможности устанавливать дефолтные значения для eventDateColumn --- src/ClickHousePlatform.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ClickHousePlatform.php b/src/ClickHousePlatform.php index ccae5f1..d6b4062 100644 --- a/src/ClickHousePlatform.php +++ b/src/ClickHousePlatform.php @@ -667,7 +667,13 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options */ $dateColumnParams = [ 'type' => Type::getType('date'), - 'default' => 'today()', + 'default' => + isset($options['eventDateColumn']) && + isset($columns[$options['eventDateColumn']]) && + isset($columns[$options['eventDateColumn']]['default']) && + $columns[$options['eventDateColumn']]['default'] ? + $columns[$options['eventDateColumn']]['default'] : + 'today()' ]; if (! empty($options['eventDateProviderColumn'])) { $options['eventDateProviderColumn'] = trim($options['eventDateProviderColumn']); From 455c4ad223c9acf0fdab2999e483e4918224d456 Mon Sep 17 00:00:00 2001 From: Artem Kaplenko Date: Mon, 10 Jun 2019 16:34:04 +0300 Subject: [PATCH 6/8] fix smi2ChClient constuctor --- src/ClickHouseConnection.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ClickHouseConnection.php b/src/ClickHouseConnection.php index 15cd109..ef892f6 100644 --- a/src/ClickHouseConnection.php +++ b/src/ClickHouseConnection.php @@ -51,10 +51,9 @@ public function __construct( 'port' => $params['port'] ?? 8123, 'username' => $username, 'password' => $password, - 'settings' => array_merge([ - 'database' => $params['dbname'] ?? 'default', - ], $params['driverOptions'] ?? []), - ]); + ], array_merge([ + 'database' => $params['dbname'] ?? 'default', + ], $params['driverOptions'] ?? [])); $this->platform = $platform; } From 4872b5936fe08070b9dc08613b0d26484079eff1 Mon Sep 17 00:00:00 2001 From: Artem Kaplenko Date: Mon, 10 Jun 2019 17:56:59 +0300 Subject: [PATCH 7/8] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0c0789a..113be3d 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "friendsofdoctrine/dbal-clickhouse", + "name": "bllem/dbal-clickhouse", "type": "library", "description": "Doctrine DBAL driver for ClickHouse", "keywords": [ From 5c140ff58cd8b6d599eb28abd07a1907e3f7fed4 Mon Sep 17 00:00:00 2001 From: Artem Kaplenko Date: Wed, 2 Oct 2019 15:02:02 +0300 Subject: [PATCH 8/8] add decimal type --- src/ClickHousePlatform.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ClickHousePlatform.php b/src/ClickHousePlatform.php index d6b4062..608b702 100644 --- a/src/ClickHousePlatform.php +++ b/src/ClickHousePlatform.php @@ -135,6 +135,7 @@ protected function initializeDoctrineTypeMappings() : void 'uint64' => 'bigint', 'float32' => 'decimal', 'float64' => 'float', + 'decimal' => 'decimal', 'string' => 'string', 'fixedstring' => 'string',