diff --git a/CHANGELOG.md b/CHANGELOG.md index e56ddf1f..55c1f0e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - Chg #339: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov) - New #342, #430: Add JSON overlaps condition builder (@Tigrov) - Enh #344: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov) -- New #346, #361: Implement `ColumnFactory` class (@Tigrov) +- New #346, #361, #454: Implement `ColumnFactory` class (@Tigrov, @vjik) - Enh #347, #353: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - Bug #349, #352: Restore connection if closed by connection timeout (@Tigrov) - Enh #354: Separate column type constants (@Tigrov) diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 94a2fc24..8b9ec518 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -75,6 +75,7 @@ protected function getColumnClass(string $type, array $info = []): string ColumnType::TIME => DateTimeColumn::class, ColumnType::TIMETZ => DateTimeColumn::class, ColumnType::DATE => DateTimeColumn::class, + ColumnType::DECIMAL => StringColumn::class, default => parent::getColumnClass($type, $info), }; } diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index d02a6832..9bd8a2c8 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -50,7 +50,7 @@ public function testSelectWithPhpTypecasting(): void $expected = [ 'null' => null, 1 => 1, - '2.5' => 2.5, + '2.5' => '2.5', 'true' => 1, 'false' => 0, 'string' => 'string', @@ -78,13 +78,13 @@ public function testSelectWithPhpTypecasting(): void ->withPhpTypecasting() ->queryScalar(); - $this->assertSame(2.5, $result); + $this->assertSame('2.5', $result); $result = $db->createCommand('SELECT 2.5 UNION SELECT 3.3') ->withPhpTypecasting() ->queryColumn(); - $this->assertSame([2.5, 3.3], $result); + $this->assertSame(['2.5', '3.3'], $result); $db->close(); } diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 18d6da66..92c3872e 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -32,8 +32,8 @@ public static function dbTypes(): array ['float', ColumnType::FLOAT, DoubleColumn::class], ['real', ColumnType::FLOAT, DoubleColumn::class], ['double', ColumnType::DOUBLE, DoubleColumn::class], - ['decimal', ColumnType::DECIMAL, DoubleColumn::class], - ['numeric', ColumnType::DECIMAL, DoubleColumn::class], + ['decimal', ColumnType::DECIMAL, StringColumn::class], + ['numeric', ColumnType::DECIMAL, StringColumn::class], ['char', ColumnType::CHAR, StringColumn::class], ['varchar', ColumnType::STRING, StringColumn::class], ['string', ColumnType::STRING, StringColumn::class], @@ -63,7 +63,7 @@ public static function definitions(): array $definitions['text NOT NULL'][1] = new StringColumn(ColumnType::TEXT, dbType: 'text', notNull: true); $definitions['char(1)'][1] = new StringColumn(ColumnType::CHAR, dbType: 'char', size: 1); $definitions['string(126)[][]'][1] = new ArrayColumn(size: 126, dimension: 2, column: new StringColumn(size: 126)); - + $definitions['decimal(10,2)'][1] = new StringColumn(ColumnType::DECIMAL, dbType: 'decimal', scale: 2, size: 10); $definitions[] = ['bit(1)', new BooleanColumn(dbType: 'bit', size: 1)]; return $definitions; diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index cd315c4d..1ba50f68 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -178,9 +178,9 @@ public static function upsertReturning(): array true, ['id_1', 'id_2'], 'INSERT INTO `notauto_pk` (`id_1`, `id_2`, `type`) SELECT `id_1`, `id_2`, `type`' - . ' FROM (SELECT 1 AS `id_1`, 2.5 AS `id_2`, :qp0 AS `type`) AS EXCLUDED' + . ' FROM (SELECT 1 AS `id_1`, :qp0 AS `id_2`, :qp1 AS `type`) AS EXCLUDED' . ' ON DUPLICATE KEY UPDATE `type`=EXCLUDED.`type`;SELECT 1 AS `id_1`, 2.5 AS `id_2`', - [':qp0' => new Param('Test', DataType::STRING)], + [':qp0' => new Param('2.5', DataType::STRING), ':qp1' => new Param('Test', DataType::STRING)], ], 'no return columns' => [ 'type', diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index 49ec335d..c575a797 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -102,12 +102,12 @@ public static function columns(): array 'blob_col' => new BinaryColumn( dbType: 'blob', ), - 'numeric_col' => new DoubleColumn( + 'numeric_col' => new StringColumn( ColumnType::DECIMAL, dbType: 'decimal', - size: 5, scale: 2, - defaultValue: 33.22, + size: 5, + defaultValue: '33.22', ), 'timestamp_col' => new DateTimeColumn( ColumnType::TIMESTAMP, @@ -358,7 +358,7 @@ public static function resultColumns(): array 'len' => 4, 'precision' => 3, ]], - [new DoubleColumn(ColumnType::DECIMAL, dbType: 'decimal', name: 'numeric_col', notNull: false, size: 5, scale: 2), [ + [new StringColumn(ColumnType::DECIMAL, dbType: 'decimal', name: 'numeric_col', notNull: false, scale: 2, size: 5), [ 'native_type' => 'NEWDECIMAL', 'pdo_type' => 2, 'flags' => [], @@ -412,7 +412,7 @@ public static function resultColumns(): array 'len' => 1, 'precision' => 0, ]], - [new DoubleColumn(ColumnType::DECIMAL, dbType: 'decimal', name: '2.5', notNull: true, size: 2, scale: 1), [ + [new StringColumn(ColumnType::DECIMAL, dbType: 'decimal', name: '2.5', notNull: true, scale: 1, size: 2), [ 'native_type' => 'NEWDECIMAL', 'pdo_type' => 2, 'flags' => ['not_null'],