Skip to content

Commit

Permalink
Refactor, conversion to lower case db types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 21, 2025
1 parent cf8bace commit d23c5b4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/Column/BinaryColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class BinaryColumn extends BaseBinaryColumn
{
public function dbTypecast(mixed $value): mixed
{
if ($this->getDbType() === 'BLOB') {
if ($this->getDbType() === 'blob') {
if ($value instanceof ParamInterface && is_string($value->getValue())) {
/** @var string */
$value = $value->getValue();
Expand Down
4 changes: 2 additions & 2 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
'number',
'float',
'timestamp',
'interval day to second',
'interval day(0) to second',
'raw',
'urowid',
];
Expand Down Expand Up @@ -94,7 +94,7 @@ protected function getDbType(ColumnInterface $column): string
ColumnType::DATETIME => 'timestamp',
ColumnType::TIMESTAMP => 'timestamp',
ColumnType::DATE => 'date',
ColumnType::TIME => 'interval day(0) to second' . ($size !== null ? "($size)" : ''),
ColumnType::TIME => 'interval day(0) to second',
ColumnType::ARRAY => 'clob',
ColumnType::STRUCTURED => 'clob',
ColumnType::JSON => 'clob',
Expand Down
24 changes: 4 additions & 20 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnInterface;

use function in_array;
use function preg_replace;
use function rtrim;
use function strtolower;

final class ColumnFactory extends AbstractColumnFactory
{
Expand Down Expand Up @@ -41,10 +38,11 @@ final class ColumnFactory extends AbstractColumnFactory
'binary_double' => ColumnType::DOUBLE, // 64 bit
'float' => ColumnType::DOUBLE, // 126 bit
'date' => ColumnType::DATE,
'interval day to second' => ColumnType::TIME,
'timestamp' => ColumnType::TIMESTAMP,
'timestamp with time zone' => ColumnType::TIMESTAMP,
'timestamp with local time zone' => ColumnType::TIMESTAMP,
'interval day to second' => ColumnType::STRING,
'interval year to month' => ColumnType::STRING,

/** Deprecated */
'long' => ColumnType::TEXT,
Expand All @@ -57,8 +55,6 @@ protected function columnDefinitionParser(): ColumnDefinitionParser

protected function getType(string $dbType, array $info = []): string
{
$dbType = strtolower($dbType);

if ($dbType === 'number') {
return match ($info['scale'] ?? null) {
null => ColumnType::DOUBLE,
Expand All @@ -67,20 +63,8 @@ protected function getType(string $dbType, array $info = []): string
};
}

$dbType = preg_replace('/\([^)]+\)/', '', $dbType);

if (in_array($dbType, [
'timestamp',
'timestamp with time zone',
'timestamp with local time zone',
'interval day to second',
'interval year to month',
], true)) {
[$info['size'], $info['scale']] = [$info['scale'] ?? null, $info['size'] ?? null];

if ($dbType === 'interval day to second' && $info['scale'] > 0) {
return ColumnType::STRING;
}
if ($dbType === 'interval day to second' && isset($info['scale']) && $info['scale'] === 0) {
return ColumnType::TIME;
}

return parent::getType($dbType, $info);
Expand Down
15 changes: 14 additions & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
use function implode;
use function is_array;
use function md5;
use function preg_replace;
use function serialize;
use function strtolower;

/**
* Implements the Oracle Server specific schema, supporting Oracle Server 11C and above.
Expand Down Expand Up @@ -437,7 +439,18 @@ protected function getTableSequenceName(string $tableName): string|null
*/
private function loadColumn(array $info): ColumnInterface
{
return $this->getColumnFactory()->fromDbType($info['data_type'], [
$dbType = strtolower(preg_replace('/\([^)]+\)/', '', $info['data_type']));

match ($dbType) {
'timestamp',
'timestamp with time zone',
'timestamp with local time zone',
'interval day to second',
'interval year to month' => [$info['size'], $info['data_scale']] = [$info['data_scale'], $info['size']],
default => null,
};

return $this->getColumnFactory()->fromDbType($dbType, [
'autoIncrement' => $info['identity_column'] === 'YES',
'comment' => $info['column_comment'],
'defaultValueRaw' => $info['data_default'],
Expand Down
2 changes: 1 addition & 1 deletion tests/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testDbTypecastColumns(string $className, array $values): void
public function testBinaryColumn(): void
{
$binaryCol = new BinaryColumn();
$binaryCol->dbType('BLOB');
$binaryCol->dbType('blob');

$this->assertInstanceOf(Expression::class, $binaryCol->dbTypecast("\x10\x11\x12"));
$this->assertInstanceOf(
Expand Down
14 changes: 12 additions & 2 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public static function dbTypes(): array
['binary_double', ColumnType::DOUBLE, DoubleColumn::class],
['float', ColumnType::DOUBLE, DoubleColumn::class],
['date', ColumnType::DATE, StringColumn::class],
['interval day(0) to second', ColumnType::TIME, StringColumn::class],
['timestamp', ColumnType::TIMESTAMP, StringColumn::class],
['timestamp with time zone', ColumnType::TIMESTAMP, StringColumn::class],
['timestamp with local time zone', ColumnType::TIMESTAMP, StringColumn::class],
['timestamp with local time zone', ColumnType::TIMESTAMP, StringColumn::class],
['interval day to second', ColumnType::STRING, StringColumn::class],
['interval year to month', ColumnType::STRING, StringColumn::class],
];
}

Expand All @@ -52,7 +54,15 @@ public static function definitions(): array

unset($definitions['bigint UNSIGNED']);

return $definitions;
return [
...$definitions,
['interval day to second', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval day to second']],
['interval day(0) to second', ColumnType::TIME, StringColumn::class, ['getDbType' => 'interval day to second', 'getScale' => 0]],
['interval day (0) to second(6)', ColumnType::TIME, StringColumn::class, ['getDbType' => 'interval day to second', 'getScale' => 0, 'getSize' => 6]],
['interval day to second (0)', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval day to second', 'getSize' => 0]],
['interval year to month', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval year to month']],
['interval year (2) to month', ColumnType::STRING, StringColumn::class, ['getDbType' => 'interval year to month', 'getScale' => 2]],
];
}

public static function defaultValueRaw(): array
Expand Down
54 changes: 27 additions & 27 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function columns(): array
[
'int_col' => [
'type' => 'integer',
'dbType' => 'NUMBER',
'dbType' => 'number',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -29,7 +29,7 @@ public static function columns(): array
],
'int_col2' => [
'type' => 'integer',
'dbType' => 'NUMBER',
'dbType' => 'number',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -41,7 +41,7 @@ public static function columns(): array
],
'tinyint_col' => [
'type' => 'integer',
'dbType' => 'NUMBER',
'dbType' => 'number',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -53,7 +53,7 @@ public static function columns(): array
],
'smallint_col' => [
'type' => 'integer',
'dbType' => 'NUMBER',
'dbType' => 'number',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -65,7 +65,7 @@ public static function columns(): array
],
'char_col' => [
'type' => 'char',
'dbType' => 'CHAR',
'dbType' => 'char',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -77,7 +77,7 @@ public static function columns(): array
],
'char_col2' => [
'type' => 'string',
'dbType' => 'VARCHAR2',
'dbType' => 'varchar2',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -89,7 +89,7 @@ public static function columns(): array
],
'char_col3' => [
'type' => 'string',
'dbType' => 'VARCHAR2',
'dbType' => 'varchar2',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -101,7 +101,7 @@ public static function columns(): array
],
'nvarchar_col' => [
'type' => 'string',
'dbType' => 'NVARCHAR2',
'dbType' => 'nvarchar2',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -113,7 +113,7 @@ public static function columns(): array
],
'float_col' => [
'type' => 'double',
'dbType' => 'FLOAT',
'dbType' => 'float',
'phpType' => 'float',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -125,7 +125,7 @@ public static function columns(): array
],
'float_col2' => [
'type' => 'double',
'dbType' => 'FLOAT',
'dbType' => 'float',
'phpType' => 'float',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -137,7 +137,7 @@ public static function columns(): array
],
'blob_col' => [
'type' => 'binary',
'dbType' => 'BLOB',
'dbType' => 'blob',
'phpType' => 'mixed',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -149,7 +149,7 @@ public static function columns(): array
],
'numeric_col' => [
'type' => 'decimal',
'dbType' => 'NUMBER',
'dbType' => 'number',
'phpType' => 'float',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -161,19 +161,19 @@ public static function columns(): array
],
'timestamp_col' => [
'type' => 'timestamp',
'dbType' => 'TIMESTAMP(6)',
'dbType' => 'timestamp',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'scale' => 6,
'size' => 6,
'scale' => null,
'defaultValue' => new Expression("to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')"),
],
'time_col' => [
'type' => 'time',
'dbType' => 'INTERVAL DAY(0) TO SECOND(0)',
'dbType' => 'interval day to second',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -185,19 +185,19 @@ public static function columns(): array
],
'interval_day_col' => [
'type' => 'string',
'dbType' => 'INTERVAL DAY(1) TO SECOND(0)',
'dbType' => 'interval day to second',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => 1,
'scale' => 0,
'size' => 0,
'scale' => 1,
'defaultValue' => new Expression("INTERVAL '2 04:56:12' DAY(1) TO SECOND(0)"),
],
'bool_col' => [
'type' => 'char',
'dbType' => 'CHAR',
'dbType' => 'char',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -209,7 +209,7 @@ public static function columns(): array
],
'bool_col2' => [
'type' => 'char',
'dbType' => 'CHAR',
'dbType' => 'char',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => false,
Expand All @@ -221,19 +221,19 @@ public static function columns(): array
],
'ts_default' => [
'type' => 'timestamp',
'dbType' => 'TIMESTAMP(6)',
'dbType' => 'timestamp',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'scale' => 6,
'size' => 6,
'scale' => null,
'defaultValue' => new Expression('CURRENT_TIMESTAMP'),
],
'bit_col' => [
'type' => 'integer',
'dbType' => 'NUMBER',
'dbType' => 'number',
'phpType' => 'int',
'primaryKey' => false,
'notNull' => true,
Expand All @@ -250,7 +250,7 @@ public static function columns(): array
[
'id' => [
'type' => 'integer',
'dbType' => 'NUMBER',
'dbType' => 'number',
'phpType' => 'int',
'primaryKey' => true,
'notNull' => true,
Expand All @@ -262,7 +262,7 @@ public static function columns(): array
],
'type' => [
'type' => 'string',
'dbType' => 'VARCHAR2',
'dbType' => 'varchar2',
'phpType' => 'string',
'primaryKey' => false,
'notNull' => true,
Expand Down

0 comments on commit d23c5b4

Please sign in to comment.