Skip to content

Commit

Permalink
Merge branch 'master' into split-debug
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz authored Jul 28, 2023
2 parents d3787c1 + 52a6e08 commit fae7aab
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 76 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Yii Database Change Log

## 1.1.0 under development
## 1.1.1 under development

- no changes in this release.

## 1.1.0 July 24, 2023

- Enh #617: Add debug collector for yiisoft/yii-debug (@xepozz)
- Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw)
- Chg #724: Typecast refactoring (@Tigrov)
- Chg #728: Refactor `AbstractSchema::getColumnPhpType()` (@Tigrov)

## 1.0.0 April 12, 2023

Expand Down
2 changes: 1 addition & 1 deletion docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**.

Expand Down
70 changes: 24 additions & 46 deletions src/Schema/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}
30 changes: 9 additions & 21 deletions src/Schema/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Provider/ColumnTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,22 +472,22 @@ 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)',
],
],
'$this->primaryKey(8)->check(\'value > 5\')' => [
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()' => [
Expand All @@ -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',
],
],
Expand Down

0 comments on commit fae7aab

Please sign in to comment.