From 585d09b81ca1a5893a2d75c52950b8ad010b3b7b Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Thu, 14 Nov 2019 14:29:07 +0300 Subject: [PATCH] - added native support for UUID type --- CHANGELOG.md | 8 ++ src/Driver/MySQL/Schema/MySQLColumn.php | 1 + src/Driver/Postgres/Schema/PostgresColumn.php | 3 +- .../SQLServer/Schema/SQLServerColumn.php | 83 ++++++++++--------- src/Driver/SQLite/Schema/SQLiteColumn.php | 1 + src/Schema/AbstractColumn.php | 1 + src/Table.php | 4 +- tests/Database/ConsistencyTest.php | 21 +++++ 8 files changed, 78 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53a0bc45..223e89f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ CHANGELOG for 0.9.0 RC ====================== +2.6.2 (14.11.2019) +----- +- added native support for UUID type + +2.6.1 (05.11.2019) +----- +- force the database disconned in case of connection error + 2.6.0 (08.10.2019) ----- - minimum PHP version is set as 7.2 diff --git a/src/Driver/MySQL/Schema/MySQLColumn.php b/src/Driver/MySQL/Schema/MySQLColumn.php index 36597e4d..d3e33e5d 100644 --- a/src/Driver/MySQL/Schema/MySQLColumn.php +++ b/src/Driver/MySQL/Schema/MySQLColumn.php @@ -86,6 +86,7 @@ class MySQLColumn extends AbstractColumn //Additional types 'json' => 'text', + 'uuid' => ['type' => 'varchar', 'size' => 36], ]; /** diff --git a/src/Driver/Postgres/Schema/PostgresColumn.php b/src/Driver/Postgres/Schema/PostgresColumn.php index ae5cce0b..dec326d3 100644 --- a/src/Driver/Postgres/Schema/PostgresColumn.php +++ b/src/Driver/Postgres/Schema/PostgresColumn.php @@ -80,7 +80,8 @@ class PostgresColumn extends AbstractColumn //Additional types 'json' => 'json', - 'jsonb' => 'jsonb' + 'jsonb' => 'jsonb', + 'uuid' => 'uuid' ]; /** diff --git a/src/Driver/SQLServer/Schema/SQLServerColumn.php b/src/Driver/SQLServer/Schema/SQLServerColumn.php index 58d7ba7d..fca2cf5a 100644 --- a/src/Driver/SQLServer/Schema/SQLServerColumn.php +++ b/src/Driver/SQLServer/Schema/SQLServerColumn.php @@ -43,71 +43,72 @@ class SQLServerColumn extends AbstractColumn */ protected $mapping = [ //Primary sequences - 'primary' => ['type' => 'int', 'identity' => true, 'nullable' => false], - 'bigPrimary' => ['type' => 'bigint', 'identity' => true, 'nullable' => false], + 'primary' => ['type' => 'int', 'identity' => true, 'nullable' => false], + 'bigPrimary' => ['type' => 'bigint', 'identity' => true, 'nullable' => false], //Enum type (mapped via method) - 'enum' => 'enum', + 'enum' => 'enum', //Logical types - 'boolean' => 'bit', + 'boolean' => 'bit', //Integer types (size can always be changed with size method), longInteger has method alias //bigInteger - 'integer' => 'int', + 'integer' => 'int', 'tinyInteger' => 'tinyint', - 'bigInteger' => 'bigint', + 'bigInteger' => 'bigint', //String with specified length (mapped via method) - 'string' => 'varchar', + 'string' => 'varchar', //Generic types - 'text' => ['type' => 'varchar', 'size' => 0], - 'tinyText' => ['type' => 'varchar', 'size' => 0], - 'longText' => ['type' => 'varchar', 'size' => 0], + 'text' => ['type' => 'varchar', 'size' => 0], + 'tinyText' => ['type' => 'varchar', 'size' => 0], + 'longText' => ['type' => 'varchar', 'size' => 0], //Real types - 'double' => 'float', - 'float' => 'real', + 'double' => 'float', + 'float' => 'real', //Decimal type (mapped via method) - 'decimal' => 'decimal', + 'decimal' => 'decimal', //Date and Time types - 'datetime' => 'datetime', - 'date' => 'date', - 'time' => 'time', - 'timestamp' => 'datetime', + 'datetime' => 'datetime', + 'date' => 'date', + 'time' => 'time', + 'timestamp' => 'datetime', //Binary types - 'binary' => ['type' => 'varbinary', 'size' => 0], - 'tinyBinary' => ['type' => 'varbinary', 'size' => 0], - 'longBinary' => ['type' => 'varbinary', 'size' => 0], + 'binary' => ['type' => 'varbinary', 'size' => 0], + 'tinyBinary' => ['type' => 'varbinary', 'size' => 0], + 'longBinary' => ['type' => 'varbinary', 'size' => 0], //Additional types - 'json' => ['type' => 'varchar', 'size' => 0], + 'json' => ['type' => 'varchar', 'size' => 0], + 'uuid' => ['type' => 'varchar', 'size' => 36], ]; /** * {@inheritdoc} */ protected $reverseMapping = [ - 'primary' => [['type' => 'int', 'identity' => true]], - 'bigPrimary' => [['type' => 'bigint', 'identity' => true]], - 'enum' => ['enum'], - 'boolean' => ['bit'], - 'integer' => ['int'], + 'primary' => [['type' => 'int', 'identity' => true]], + 'bigPrimary' => [['type' => 'bigint', 'identity' => true]], + 'enum' => ['enum'], + 'boolean' => ['bit'], + 'integer' => ['int'], 'tinyInteger' => ['tinyint', 'smallint'], - 'bigInteger' => ['bigint'], - 'text' => [['type' => 'varchar', 'size' => 0]], - 'string' => ['varchar', 'char'], - 'double' => ['float'], - 'float' => ['real'], - 'decimal' => ['decimal'], - 'timestamp' => ['datetime'], - 'date' => ['date'], - 'time' => ['time'], - 'binary' => ['varbinary'], + 'bigInteger' => ['bigint'], + 'text' => [['type' => 'varchar', 'size' => 0]], + 'string' => ['varchar', 'char'], + 'double' => ['float'], + 'float' => ['real'], + 'decimal' => ['decimal'], + 'timestamp' => ['datetime'], + 'date' => ['date'], + 'time' => ['time'], + 'binary' => ['varbinary'], ]; /** @@ -227,7 +228,7 @@ public function sqlStatement(DriverInterface $driver, bool $withEnum = true): st * will be dropped separately. * * @param DriverInterface $driver - * @param AbstractColumn $initial + * @param AbstractColumn $initial * @return array */ public function alterOperations(DriverInterface $driver, AbstractColumn $initial): array @@ -291,8 +292,8 @@ public function alterOperations(DriverInterface $driver, AbstractColumn $initial } /** - * @param string $table Table name. - * @param array $schema + * @param string $table Table name. + * @param array $schema * @param DriverInterface $driver SQLServer columns are bit more complex. * * @return SQLServerColumn @@ -432,7 +433,7 @@ private function normalizeDefault(): void if ( $this->getType() != 'string' && ($this->defaultValue[0] == '(' - && $this->defaultValue[strlen($this->defaultValue) - 1] == ')') + && $this->defaultValue[strlen($this->defaultValue) - 1] == ')') ) { //Cut another braces $this->defaultValue = substr($this->defaultValue, 1, -1); @@ -443,7 +444,7 @@ private function normalizeDefault(): void * Resolve enum values if any. * * @param DriverInterface $driver - * @param array $schema + * @param array $schema * @param SQLServerColumn $column */ private static function resolveEnum( diff --git a/src/Driver/SQLite/Schema/SQLiteColumn.php b/src/Driver/SQLite/Schema/SQLiteColumn.php index 91ab327a..25171b1b 100644 --- a/src/Driver/SQLite/Schema/SQLiteColumn.php +++ b/src/Driver/SQLite/Schema/SQLiteColumn.php @@ -86,6 +86,7 @@ class SQLiteColumn extends AbstractColumn //Additional types 'json' => 'text', + 'uuid' => ['type' => 'varchar', 'size' => 36], ]; /** diff --git a/src/Schema/AbstractColumn.php b/src/Schema/AbstractColumn.php index 98e3ccba..8ac466a8 100644 --- a/src/Schema/AbstractColumn.php +++ b/src/Schema/AbstractColumn.php @@ -45,6 +45,7 @@ * @method AbstractColumn|$this tinyBinary() * @method AbstractColumn|$this longBinary() * @method AbstractColumn|$this json() + * @method AbstractColumn|$this uuid() */ abstract class AbstractColumn implements ColumnInterface, ElementInterface { diff --git a/src/Table.php b/src/Table.php index 41150dbb..ad32e86f 100644 --- a/src/Table.php +++ b/src/Table.php @@ -117,11 +117,11 @@ public function eraseData(): void * $table->insertOne(["name" => "Wolfy-J", "balance" => 10]); * * @param array $rowset - * @return int + * @return int|null * * @throws BuilderException */ - public function insertOne(array $rowset = []): int + public function insertOne(array $rowset = []): ?int { return $this->database->insert($this->name)->values($rowset)->run(); } diff --git a/tests/Database/ConsistencyTest.php b/tests/Database/ConsistencyTest.php index 4e75e2be..1a30edea 100644 --- a/tests/Database/ConsistencyTest.php +++ b/tests/Database/ConsistencyTest.php @@ -347,4 +347,25 @@ public function testTime(): void $this->assertTrue($schema->exists()); $this->assertTrue($schema->column('target')->compare($column)); } + + public function testUuid(): void + { + $schema = $this->schema('table'); + $this->assertFalse($schema->exists()); + + $column = $schema->uuid('target'); + + $schema->save(); + $schema = $this->schema('table'); + $this->assertTrue($schema->exists()); + $this->assertTrue($schema->column('target')->compare($column)); + + $this->database->table('table')->insertOne([ + 'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' + ]); + + $this->assertEquals([ + 'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' + ], $this->database->table('table')->select()->fetchAll()[0]); + } }