Skip to content

Commit ef476f8

Browse files
authored
Use StringColumn for decimal type by default (#454)
1 parent 8121670 commit ef476f8

File tree

6 files changed

+15
-14
lines changed

6 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- Chg #339: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov)
1010
- New #342, #430: Add JSON overlaps condition builder (@Tigrov)
1111
- Enh #344: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
12-
- New #346, #361: Implement `ColumnFactory` class (@Tigrov)
12+
- New #346, #361, #454: Implement `ColumnFactory` class (@Tigrov, @vjik)
1313
- Enh #347, #353: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
1414
- Bug #349, #352: Restore connection if closed by connection timeout (@Tigrov)
1515
- Enh #354: Separate column type constants (@Tigrov)

src/Column/ColumnFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ protected function getColumnClass(string $type, array $info = []): string
7575
ColumnType::TIME => DateTimeColumn::class,
7676
ColumnType::TIMETZ => DateTimeColumn::class,
7777
ColumnType::DATE => DateTimeColumn::class,
78+
ColumnType::DECIMAL => StringColumn::class,
7879
default => parent::getColumnClass($type, $info),
7980
};
8081
}

tests/ColumnTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testSelectWithPhpTypecasting(): void
5050
$expected = [
5151
'null' => null,
5252
1 => 1,
53-
'2.5' => 2.5,
53+
'2.5' => '2.5',
5454
'true' => 1,
5555
'false' => 0,
5656
'string' => 'string',
@@ -78,13 +78,13 @@ public function testSelectWithPhpTypecasting(): void
7878
->withPhpTypecasting()
7979
->queryScalar();
8080

81-
$this->assertSame(2.5, $result);
81+
$this->assertSame('2.5', $result);
8282

8383
$result = $db->createCommand('SELECT 2.5 UNION SELECT 3.3')
8484
->withPhpTypecasting()
8585
->queryColumn();
8686

87-
$this->assertSame([2.5, 3.3], $result);
87+
$this->assertSame(['2.5', '3.3'], $result);
8888

8989
$db->close();
9090
}

tests/Provider/ColumnFactoryProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public static function dbTypes(): array
3232
['float', ColumnType::FLOAT, DoubleColumn::class],
3333
['real', ColumnType::FLOAT, DoubleColumn::class],
3434
['double', ColumnType::DOUBLE, DoubleColumn::class],
35-
['decimal', ColumnType::DECIMAL, DoubleColumn::class],
36-
['numeric', ColumnType::DECIMAL, DoubleColumn::class],
35+
['decimal', ColumnType::DECIMAL, StringColumn::class],
36+
['numeric', ColumnType::DECIMAL, StringColumn::class],
3737
['char', ColumnType::CHAR, StringColumn::class],
3838
['varchar', ColumnType::STRING, StringColumn::class],
3939
['string', ColumnType::STRING, StringColumn::class],
@@ -63,7 +63,7 @@ public static function definitions(): array
6363
$definitions['text NOT NULL'][1] = new StringColumn(ColumnType::TEXT, dbType: 'text', notNull: true);
6464
$definitions['char(1)'][1] = new StringColumn(ColumnType::CHAR, dbType: 'char', size: 1);
6565
$definitions['string(126)[][]'][1] = new ArrayColumn(size: 126, dimension: 2, column: new StringColumn(size: 126));
66-
66+
$definitions['decimal(10,2)'][1] = new StringColumn(ColumnType::DECIMAL, dbType: 'decimal', scale: 2, size: 10);
6767
$definitions[] = ['bit(1)', new BooleanColumn(dbType: 'bit', size: 1)];
6868

6969
return $definitions;

tests/Provider/QueryBuilderProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ public static function upsertReturning(): array
178178
true,
179179
['id_1', 'id_2'],
180180
'INSERT INTO `notauto_pk` (`id_1`, `id_2`, `type`) SELECT `id_1`, `id_2`, `type`'
181-
. ' FROM (SELECT 1 AS `id_1`, 2.5 AS `id_2`, :qp0 AS `type`) AS EXCLUDED'
181+
. ' FROM (SELECT 1 AS `id_1`, :qp0 AS `id_2`, :qp1 AS `type`) AS EXCLUDED'
182182
. ' ON DUPLICATE KEY UPDATE `type`=EXCLUDED.`type`;SELECT 1 AS `id_1`, 2.5 AS `id_2`',
183-
[':qp0' => new Param('Test', DataType::STRING)],
183+
[':qp0' => new Param('2.5', DataType::STRING), ':qp1' => new Param('Test', DataType::STRING)],
184184
],
185185
'no return columns' => [
186186
'type',

tests/Provider/SchemaProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public static function columns(): array
102102
'blob_col' => new BinaryColumn(
103103
dbType: 'blob',
104104
),
105-
'numeric_col' => new DoubleColumn(
105+
'numeric_col' => new StringColumn(
106106
ColumnType::DECIMAL,
107107
dbType: 'decimal',
108-
size: 5,
109108
scale: 2,
110-
defaultValue: 33.22,
109+
size: 5,
110+
defaultValue: '33.22',
111111
),
112112
'timestamp_col' => new DateTimeColumn(
113113
ColumnType::TIMESTAMP,
@@ -358,7 +358,7 @@ public static function resultColumns(): array
358358
'len' => 4,
359359
'precision' => 3,
360360
]],
361-
[new DoubleColumn(ColumnType::DECIMAL, dbType: 'decimal', name: 'numeric_col', notNull: false, size: 5, scale: 2), [
361+
[new StringColumn(ColumnType::DECIMAL, dbType: 'decimal', name: 'numeric_col', notNull: false, scale: 2, size: 5), [
362362
'native_type' => 'NEWDECIMAL',
363363
'pdo_type' => 2,
364364
'flags' => [],
@@ -412,7 +412,7 @@ public static function resultColumns(): array
412412
'len' => 1,
413413
'precision' => 0,
414414
]],
415-
[new DoubleColumn(ColumnType::DECIMAL, dbType: 'decimal', name: '2.5', notNull: true, size: 2, scale: 1), [
415+
[new StringColumn(ColumnType::DECIMAL, dbType: 'decimal', name: '2.5', notNull: true, scale: 1, size: 2), [
416416
'native_type' => 'NEWDECIMAL',
417417
'pdo_type' => 2,
418418
'flags' => ['not_null'],

0 commit comments

Comments
 (0)