Skip to content

Commit 3946b49

Browse files
authored
Use StringColumn for decimal type by default (#1125)
1 parent 2df41d3 commit 3946b49

4 files changed

Lines changed: 22 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
- Enh #865: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov, @vjik)
4242
- Enh #798: Allow `QueryInterface::one()` and `QueryInterface::all()` to return objects (@darkdef, @Tigrov)
4343
- Enh #872: Use `#[\SensitiveParameter]` attribute to mark sensitive parameters (@heap-s)
44-
- New #864, #897, #898, #950, #1009: Realize column factory (@Tigrov, @vjik)
44+
- New #864, #897, #898, #950, #1009, #1125: Realize column factory (@Tigrov, @vjik)
4545
- Enh #875: Ignore "Packets out of order..." warnings in `AbstractPdoCommand::internalExecute()` method (@Tigrov)
4646
- Enh #877: Separate column type constants (@Tigrov)
4747
- New #878, #1095: Realize `ColumnBuilder` class (@Tigrov, @vjik)

src/Schema/Column/AbstractColumnFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ protected function getColumnClass(string $type, array $info = []): string
243243
ColumnType::BIGINT => PHP_INT_SIZE !== 8 || !empty($info['unsigned'])
244244
? BigIntColumn::class
245245
: IntegerColumn::class,
246-
ColumnType::DECIMAL => DoubleColumn::class,
246+
ColumnType::DECIMAL => StringColumn::class,
247247
ColumnType::FLOAT => DoubleColumn::class,
248248
ColumnType::DOUBLE => DoubleColumn::class,
249249
ColumnType::BINARY => BinaryColumn::class,

tests/Common/CommonCommandTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,27 +2103,27 @@ protected function internalExecute(): void {}
21032103

21042104
public function testDecimalValue(): void
21052105
{
2106-
$decimalValue = 10.0;
21072106
$db = $this->getSharedConnection();
21082107
$this->loadFixture();
21092108

2110-
$inserted = $db->createCommand()
2109+
$inserted = $db
2110+
->createCommand()
21112111
->insertReturningPks(
21122112
'{{%order}}',
2113-
['customer_id' => 1, 'created_at' => 0, 'total' => $decimalValue],
2113+
['customer_id' => 1, 'created_at' => 0, 'total' => 10.0],
21142114
);
21152115

2116-
$result = $db->createCommand(
2117-
'select * from {{%order}} where [[id]]=:id',
2118-
['id' => $inserted['id']],
2119-
)->queryOne();
2116+
$result = $db
2117+
->createCommand(
2118+
'select * from {{%order}} where [[id]]=:id',
2119+
['id' => $inserted['id']],
2120+
)
2121+
->queryOne();
21202122

21212123
$column = $db->getTableSchema('{{%order}}')->getColumn('total');
21222124
$phpTypecastValue = $column->phpTypecast($result['total']);
21232125

2124-
$this->assertSame($decimalValue, $phpTypecastValue);
2125-
2126-
$db->close();
2126+
$this->assertSame('10', $phpTypecastValue);
21272127
}
21282128

21292129
public function testInsertReturningPksEmptyValues()
@@ -2179,9 +2179,9 @@ public function testInsertReturningPksWithPhpTypecasting(): void
21792179

21802180
$result = $db->createCommand()
21812181
->withPhpTypecasting()
2182-
->insertReturningPks('notauto_pk', ['id_1' => 1, 'id_2' => 2.5, 'type' => 'test1']);
2182+
->insertReturningPks('notauto_pk', ['id_1' => 1, 'id_2' => '2.5', 'type' => 'test1']);
21832183

2184-
$this->assertSame(['id_1' => 1, 'id_2' => 2.5], $result);
2184+
$this->assertSame(['id_1' => 1, 'id_2' => '2.5'], $result);
21852185

21862186
$db->close();
21872187
}
@@ -2333,21 +2333,21 @@ public function testUpsertReturningPksWithPhpTypecasting(): void
23332333

23342334
$result = $db->createCommand()
23352335
->withPhpTypecasting()
2336-
->upsertReturningPks('notauto_pk', ['id_1' => 1, 'id_2' => 2.5, 'type' => 'test1']);
2336+
->upsertReturningPks('notauto_pk', ['id_1' => 1, 'id_2' => '2.5', 'type' => 'test1']);
23372337

2338-
$this->assertSame(['id_1' => 1, 'id_2' => 2.5], $result);
2338+
$this->assertSame(['id_1' => 1, 'id_2' => '2.5'], $result);
23392339

23402340
$result = $db->createCommand()
23412341
->withPhpTypecasting()
2342-
->upsertReturningPks('notauto_pk', ['id_1' => 2, 'id_2' => 2.5, 'type' => 'test2']);
2342+
->upsertReturningPks('notauto_pk', ['id_1' => 2, 'id_2' => '2.5', 'type' => 'test2']);
23432343

2344-
$this->assertSame(['id_1' => 2, 'id_2' => 2.5], $result);
2344+
$this->assertSame(['id_1' => 2, 'id_2' => '2.5'], $result);
23452345

23462346
$result = $db->createCommand()
23472347
->withPhpTypecasting()
2348-
->upsertReturningPks('notauto_pk', ['id_1' => 2, 'id_2' => 2.5, 'type' => 'test3']);
2348+
->upsertReturningPks('notauto_pk', ['id_1' => 2, 'id_2' => '2.5', 'type' => 'test3']);
23492349

2350-
$this->assertSame(['id_1' => 2, 'id_2' => 2.5], $result);
2350+
$this->assertSame(['id_1' => 2, 'id_2' => '2.5'], $result);
23512351

23522352
$db->close();
23532353
}

tests/Provider/ColumnFactoryProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function definitions(): array
2929
'text' => ['text', new StringColumn(ColumnType::TEXT, dbType: 'text')],
3030
'text NOT NULL' => ['text NOT NULL', new StringColumn(ColumnType::TEXT, dbType: 'text', notNull: true)],
3131
'char(1)' => ['char(1)', new StringColumn(ColumnType::CHAR, dbType: 'char', size: 1)],
32-
'decimal(10,2)' => ['decimal(10,2)', new DoubleColumn(ColumnType::DECIMAL, dbType: 'decimal', size: 10, scale: 2)],
32+
'decimal(10,2)' => ['decimal(10,2)', new StringColumn(ColumnType::DECIMAL, dbType: 'decimal', scale: 2, size: 10)],
3333
'bigint UNSIGNED' => ['bigint UNSIGNED', new BigIntColumn(dbType: 'bigint', unsigned: true)],
3434
'integer[]' => ['integer[]', new ArrayColumn(dbType: 'integer', column: new IntegerColumn(dbType: 'integer'))],
3535
'string(126)[][]' => ['string(126)[][]', new ArrayColumn(size: 126, dimension: 2, column: new StringColumn(size: 126))],
@@ -65,7 +65,7 @@ public static function types(): array
6565
'bigint' => [ColumnType::BIGINT, ColumnType::BIGINT, IntegerColumn::class],
6666
'float' => [ColumnType::FLOAT, ColumnType::FLOAT, DoubleColumn::class],
6767
'double' => [ColumnType::DOUBLE, ColumnType::DOUBLE, DoubleColumn::class],
68-
'decimal' => [ColumnType::DECIMAL, ColumnType::DECIMAL, DoubleColumn::class],
68+
'decimal' => [ColumnType::DECIMAL, ColumnType::DECIMAL, StringColumn::class],
6969
'money' => [ColumnType::MONEY, ColumnType::MONEY, StringColumn::class],
7070
'timestamp' => [ColumnType::TIMESTAMP, ColumnType::TIMESTAMP, DateTimeColumn::class],
7171
'datetime' => [ColumnType::DATETIME, ColumnType::DATETIME, DateTimeColumn::class],

0 commit comments

Comments
 (0)