Skip to content

Commit 203952f

Browse files
Fix type boolean in MSSQL. (#20040)
1 parent 3343fd3 commit 203952f

File tree

3 files changed

+207
-10
lines changed

3 files changed

+207
-10
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Yii Framework 2 Change Log
44
2.2 under development
55
------------------------
66

7+
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
78
- Chg #19902: Remove support for CUBRID (mtangoo)
89
- Chg #19891: Remove XCache and ZendDataCache support (mtangoo)
910

framework/db/mssql/Schema.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,28 +386,26 @@ protected function loadColumnSchema($info)
386386
$column->isComputed = (bool)$info['is_computed'];
387387
$column->unsigned = stripos($column->dbType, 'unsigned') !== false;
388388
$column->comment = $info['comment'] === null ? '' : $info['comment'];
389-
390389
$column->type = self::TYPE_STRING;
390+
391391
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
392392
$type = $matches[1];
393+
393394
if (isset($this->typeMap[$type])) {
394395
$column->type = $this->typeMap[$type];
395396
}
397+
398+
if ($type === 'bit') {
399+
$column->type = 'boolean';
400+
}
401+
396402
if (!empty($matches[2])) {
397403
$values = explode(',', $matches[2]);
398404
$column->size = $column->precision = (int) $values[0];
405+
399406
if (isset($values[1])) {
400407
$column->scale = (int) $values[1];
401408
}
402-
if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
403-
$column->type = 'boolean';
404-
} elseif ($type === 'bit') {
405-
if ($column->size > 32) {
406-
$column->type = 'bigint';
407-
} elseif ($column->size === 32) {
408-
$column->type = 'integer';
409-
}
410-
}
411409
}
412410
}
413411

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
/**
3+
* @link https://www.yiiframework.com/
4+
* @copyright Copyright (c) 2008 Yii Software LLC
5+
* @license https://www.yiiframework.com/license/
6+
*/
7+
8+
namespace yiiunit\framework\db\mssql\type;
9+
10+
use yii\db\mssql\Schema;
11+
use yiiunit\framework\db\DatabaseTestCase;
12+
13+
/**
14+
* @group db
15+
* @group mssql
16+
*/
17+
class BooleanTest extends DatabaseTestCase
18+
{
19+
protected $driverName = 'sqlsrv';
20+
21+
public function testBoolean(): void
22+
{
23+
$db = $this->getConnection(true);
24+
$schema = $db->getSchema();
25+
$tableName = '{{%boolean}}';
26+
27+
if ($db->getTableSchema($tableName)) {
28+
$db->createCommand()->dropTable($tableName)->execute();
29+
}
30+
31+
$db->createCommand()->createTable(
32+
$tableName,
33+
[
34+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
35+
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
36+
]
37+
)->execute();
38+
39+
// test type
40+
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
41+
$this->assertSame('boolean', $column->phpType);
42+
43+
// test value `false`
44+
$db->createCommand()->insert($tableName, ['bool_col' => false])->execute();
45+
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
46+
$this->assertEquals(0, $boolValue);
47+
48+
// test php typecast
49+
$phpTypeCast = $column->phpTypecast($boolValue);
50+
$this->assertFalse($phpTypeCast);
51+
52+
// test value `true`
53+
$db->createCommand()->insert($tableName, ['bool_col' => true])->execute();
54+
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar();
55+
$this->assertEquals(1, $boolValue);
56+
57+
// test php typecast
58+
$phpTypeCast = $column->phpTypecast($boolValue);
59+
$this->assertTrue($phpTypeCast);
60+
}
61+
62+
public function testBooleanWithValueInteger(): void
63+
{
64+
$db = $this->getConnection(true);
65+
$schema = $db->getSchema();
66+
$tableName = '{{%boolean}}';
67+
68+
if ($db->getTableSchema($tableName)) {
69+
$db->createCommand()->dropTable($tableName)->execute();
70+
}
71+
72+
$db->createCommand()->createTable(
73+
$tableName,
74+
[
75+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
76+
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
77+
]
78+
)->execute();
79+
80+
// test type
81+
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
82+
$this->assertSame('boolean', $column->phpType);
83+
84+
// test value 0
85+
$db->createCommand()->insert($tableName, ['bool_col' => 0])->execute();
86+
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
87+
$this->assertEquals(0, $boolValue);
88+
89+
// test php typecast
90+
$phpTypeCast = $column->phpTypecast($boolValue);
91+
$this->assertFalse($phpTypeCast);
92+
93+
// test value 1
94+
$db->createCommand()->insert($tableName, ['bool_col' => 1])->execute();
95+
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar();
96+
$this->assertEquals(1, $boolValue);
97+
98+
// test php typecast
99+
$phpTypeCast = $column->phpTypecast($boolValue);
100+
$this->assertTrue($phpTypeCast);
101+
}
102+
103+
public function testBooleanValueNegative(): void
104+
{
105+
$db = $this->getConnection(true);
106+
$schema = $db->getSchema();
107+
$tableName = '{{%boolean}}';
108+
109+
if ($db->getTableSchema($tableName)) {
110+
$db->createCommand()->dropTable($tableName)->execute();
111+
}
112+
113+
$db->createCommand()->createTable(
114+
$tableName,
115+
[
116+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
117+
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
118+
]
119+
)->execute();
120+
121+
// test type
122+
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
123+
$this->assertSame('boolean', $column->phpType);
124+
125+
// test value 2
126+
$db->createCommand()->insert($tableName, ['bool_col' => -1])->execute();
127+
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
128+
$this->assertEquals(1, $boolValue);
129+
130+
// test php typecast
131+
$phpTypeCast = $column->phpTypecast($boolValue);
132+
$this->assertTrue($phpTypeCast);
133+
}
134+
135+
public function testBooleanWithValueNull(): void
136+
{
137+
$db = $this->getConnection(true);
138+
$schema = $db->getSchema();
139+
$tableName = '{{%boolean}}';
140+
141+
if ($db->getTableSchema($tableName)) {
142+
$db->createCommand()->dropTable($tableName)->execute();
143+
}
144+
145+
$db->createCommand()->createTable(
146+
$tableName,
147+
[
148+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
149+
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
150+
]
151+
)->execute();
152+
153+
// test type
154+
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
155+
$this->assertSame('boolean', $column->phpType);
156+
157+
// test value `null`
158+
$db->createCommand()->insert($tableName, ['bool_col' => null])->execute();
159+
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
160+
$this->assertNull($boolValue);
161+
162+
// test php typecast
163+
$phpTypeCast = $column->phpTypecast($boolValue);
164+
$this->assertNull($phpTypeCast);
165+
}
166+
167+
public function testBooleanWithValueOverflow(): void
168+
{
169+
$db = $this->getConnection(true);
170+
$schema = $db->getSchema();
171+
$tableName = '{{%boolean}}';
172+
173+
if ($db->getTableSchema($tableName)) {
174+
$db->createCommand()->dropTable($tableName)->execute();
175+
}
176+
177+
$db->createCommand()->createTable(
178+
$tableName,
179+
[
180+
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
181+
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
182+
]
183+
)->execute();
184+
185+
// test type
186+
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
187+
$this->assertSame('boolean', $column->phpType);
188+
189+
// test value 2
190+
$db->createCommand()->insert($tableName, ['bool_col' => 2])->execute();
191+
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
192+
$this->assertEquals(1, $boolValue);
193+
194+
// test php typecast
195+
$phpTypeCast = $column->phpTypecast($boolValue);
196+
$this->assertTrue($phpTypeCast);
197+
}
198+
}

0 commit comments

Comments
 (0)