From dc9669c03ebc2e6aca3ebb6d2a63c47b5b85b60e Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Thu, 13 Jul 2023 13:50:32 -0400 Subject: [PATCH 1/5] Add `autoIncrement` in `Primary Key` in `db-oracle`. (#726) --- docs/en/README.md | 2 +- tests/Provider/ColumnTypes.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/en/README.md b/docs/en/README.md index 4bd3edde4..7002fa3c0 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -19,7 +19,7 @@ Yii DB supports the following databases out of the box: - [MSSQL](https://www.microsoft.com/en-us/sql-server/sql-server-2019) of versions **2017, 2019, 2022**. - [MySQL](https://www.mysql.com/) of versions **5.7 - 8.0**. - [MariaDB](https://mariadb.org/) of versions **10.4 - 10.9**. -- [Oracle](https://www.oracle.com/database/) of versions **18c - 21c**. +- [Oracle](https://www.oracle.com/database/) of versions **12c - 21c**. - [PostgreSQL](https://www.postgresql.org/) of versions **9.6 - 15**. - [SQLite](https://www.sqlite.org/index.html) of version **3.3 and above**. diff --git a/tests/Provider/ColumnTypes.php b/tests/Provider/ColumnTypes.php index 0c1ecfed3..8312298df 100644 --- a/tests/Provider/ColumnTypes.php +++ b/tests/Provider/ColumnTypes.php @@ -472,7 +472,7 @@ public function getColumnTypes(): array 'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY CHECK (value > 5)', 'pgsql' => 'serial NOT NULL PRIMARY KEY CHECK (value > 5)', 'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL CHECK (value > 5)', - 'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY CHECK (value > 5)', + 'oci' => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY CHECK (value > 5)', 'sqlsrv' => 'int IDENTITY PRIMARY KEY CHECK (value > 5)', ], ], @@ -480,14 +480,14 @@ public function getColumnTypes(): array SchemaInterface::TYPE_PK . '(8) CHECK (value > 5)', [ 'mysql' => 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY CHECK (value > 5)', - 'oci' => 'NUMBER(8) NOT NULL PRIMARY KEY CHECK (value > 5)', + 'oci' => 'NUMBER(8) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY CHECK (value > 5)', ], ], '$this->primaryKey(8)' => [ SchemaInterface::TYPE_PK . '(8)', [ 'mysql' => 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'oci' => 'NUMBER(8) NOT NULL PRIMARY KEY', + 'oci' => 'NUMBER(8) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', ], ], '$this->primaryKey()' => [ @@ -496,7 +496,7 @@ public function getColumnTypes(): array 'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', 'pgsql' => 'serial NOT NULL PRIMARY KEY', 'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - 'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY', + 'oci' => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', 'sqlsrv' => 'int IDENTITY PRIMARY KEY', ], ], From 13ddab5cfed042960d2f42eceb3ee30596ee5fbd Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 14 Jul 2023 00:54:51 +0700 Subject: [PATCH 2/5] Typecast refactoring (#724) * Fix bit type * Typecast refactoring * Start from master * Fix test issues * Fix test issues * Update * Restart tests * Update * Update * Update --- .gitignore | 6 +-- CHANGELOG.md | 1 + src/Schema/AbstractColumnSchema.php | 70 ++++++++++------------------- 3 files changed, 28 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 7a2e9d136..c942fd526 100644 --- a/.gitignore +++ b/.gitignore @@ -31,11 +31,11 @@ composer.phar # phpunit itself is not needed phpunit.phar -.phpunit.result.cache -tests/Support/runtime/ -# local phpunit config +# local phpunit config and cache /phpunit.xml +/.phpunit.result.cache +/tests/Support/runtime/ # NPM packages /node_modules diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e5d91170..8ee3b6a8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.0.1 under development - Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw) +- Chg #724: Typecast refactoring. (@Tigrov) ## 1.0.0 April 12, 2023 diff --git a/src/Schema/AbstractColumnSchema.php b/src/Schema/AbstractColumnSchema.php index 3e84d6a3d..235f2823c 100644 --- a/src/Schema/AbstractColumnSchema.php +++ b/src/Schema/AbstractColumnSchema.php @@ -242,59 +242,37 @@ public function unsigned(bool $value): void protected function typecast(mixed $value): mixed { if ( - $value === '' - && !in_array( - $this->type, - [ - SchemaInterface::TYPE_TEXT, - SchemaInterface::TYPE_STRING, - SchemaInterface::TYPE_BINARY, - SchemaInterface::TYPE_CHAR, - ], - true - ) + $value === null + || $value === '' && !in_array($this->type, [ + SchemaInterface::TYPE_TEXT, + SchemaInterface::TYPE_STRING, + SchemaInterface::TYPE_BINARY, + SchemaInterface::TYPE_CHAR, + ], true) ) { return null; } - if ( - $value === null - || $value instanceof ExpressionInterface - || gettype($value) === $this->phpType - ) { + if ($value instanceof ExpressionInterface) { return $value; } - switch ($this->phpType) { - case SchemaInterface::PHP_TYPE_RESOURCE: - case SchemaInterface::PHP_TYPE_STRING: - if (is_resource($value)) { - return $value; - } - - if (is_float($value)) { + return match ($this->phpType) { + gettype($value) => $value, + SchemaInterface::PHP_TYPE_RESOURCE, + SchemaInterface::PHP_TYPE_STRING + => match (true) { + is_resource($value) => $value, /** ensure type cast always has . as decimal separator in all locales */ - return DbStringHelper::normalizeFloat($value); - } - - if (is_bool($value)) { - return $value ? '1' : '0'; - } - - return (string) $value; - case SchemaInterface::PHP_TYPE_INTEGER: - return (int) $value; - case SchemaInterface::PHP_TYPE_BOOLEAN: - /** - * Treating a 0-bit value as false too. - * - * @link https://github.com/yiisoft/yii2/issues/9006 - */ - return (bool) $value && $value !== "\0"; - case SchemaInterface::PHP_TYPE_DOUBLE: - return (float) $value; - } - - return $value; + is_float($value) => DbStringHelper::normalizeFloat($value), + is_bool($value) => $value ? '1' : '0', + default => (string) $value, + }, + SchemaInterface::PHP_TYPE_INTEGER => (int) $value, + /** Treating a 0-bit value as false too (@link https://github.com/yiisoft/yii2/issues/9006) */ + SchemaInterface::PHP_TYPE_BOOLEAN => $value && $value !== "\0", + SchemaInterface::PHP_TYPE_DOUBLE => (float) $value, + default => $value, + }; } } From 669fca4faa3298bb6eb5ac233a3d582ce464f519 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Mon, 24 Jul 2023 13:19:43 +0700 Subject: [PATCH 3/5] Fix #728: Refactor `AbstractSchema::getColumnPhpType()` --- CHANGELOG.md | 3 ++- src/Schema/AbstractSchema.php | 30 +++++++++--------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ee3b6a8d..80a4e4788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## 1.0.1 under development - Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw) -- Chg #724: Typecast refactoring. (@Tigrov) +- Chg #724: Typecast refactoring (@Tigrov) +- Chg #728: Refactor `AbstractSchema::getColumnPhpType()` (@Tigrov) ## 1.0.0 April 12, 2023 diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 6a22992cc..cd1c89cb0 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -402,36 +402,24 @@ protected function findTableNames(string $schema): array */ protected function getColumnPhpType(ColumnSchemaInterface $column): string { - /** @psalm-var string[] $typeMap */ - $typeMap = [ + return match ($column->getType()) { // abstract type => php type SchemaInterface::TYPE_TINYINT => SchemaInterface::PHP_TYPE_INTEGER, SchemaInterface::TYPE_SMALLINT => SchemaInterface::PHP_TYPE_INTEGER, - SchemaInterface::TYPE_INTEGER => SchemaInterface::PHP_TYPE_INTEGER, - SchemaInterface::TYPE_BIGINT => SchemaInterface::PHP_TYPE_INTEGER, + SchemaInterface::TYPE_INTEGER => PHP_INT_SIZE === 4 && $column->isUnsigned() + ? SchemaInterface::PHP_TYPE_STRING + : SchemaInterface::PHP_TYPE_INTEGER, + SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE === 8 && !$column->isUnsigned() + ? SchemaInterface::PHP_TYPE_INTEGER + : SchemaInterface::PHP_TYPE_STRING, SchemaInterface::TYPE_BOOLEAN => SchemaInterface::PHP_TYPE_BOOLEAN, SchemaInterface::TYPE_DECIMAL => SchemaInterface::PHP_TYPE_DOUBLE, SchemaInterface::TYPE_FLOAT => SchemaInterface::PHP_TYPE_DOUBLE, SchemaInterface::TYPE_DOUBLE => SchemaInterface::PHP_TYPE_DOUBLE, SchemaInterface::TYPE_BINARY => SchemaInterface::PHP_TYPE_RESOURCE, SchemaInterface::TYPE_JSON => SchemaInterface::PHP_TYPE_ARRAY, - ]; - - if (isset($typeMap[$column->getType()])) { - if ($column->getType() === SchemaInterface::TYPE_BIGINT) { - return PHP_INT_SIZE === 8 && !$column->isUnsigned() - ? SchemaInterface::PHP_TYPE_INTEGER : SchemaInterface::PHP_TYPE_STRING; - } - - if ($column->getType() === SchemaInterface::TYPE_INTEGER) { - return PHP_INT_SIZE === 4 && $column->isUnsigned() - ? SchemaInterface::PHP_TYPE_STRING : SchemaInterface::PHP_TYPE_INTEGER; - } - - return $typeMap[$column->getType()]; - } - - return SchemaInterface::PHP_TYPE_STRING; + default => SchemaInterface::PHP_TYPE_STRING, + }; } /** From e687ab4fa896500138c78ae601d38fc499cc0e14 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 25 Jul 2023 00:04:38 +0800 Subject: [PATCH 4/5] Release version 1.1.0 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a4e4788..4a5908338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Yii Database Change Log -## 1.0.1 under development +## 1.1.0 July 24, 2023 - Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw) - Chg #724: Typecast refactoring (@Tigrov) From 52a6e08fefbd18722efed6364cedc61e01b6e878 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 25 Jul 2023 00:04:54 +0800 Subject: [PATCH 5/5] Prepare for next release --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a5908338..d85c7feba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Yii Database Change Log +## 1.1.1 under development + +- no changes in this release. + ## 1.1.0 July 24, 2023 - Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw)