mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Fix type boolean in MSSQL
. (#20040)
This commit is contained in:
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
||||
2.2 under development
|
||||
------------------------
|
||||
|
||||
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
|
||||
- Chg #19902: Remove support for CUBRID (mtangoo)
|
||||
- Chg #19891: Remove XCache and ZendDataCache support (mtangoo)
|
||||
|
||||
|
@ -386,28 +386,26 @@ SQL;
|
||||
$column->isComputed = (bool)$info['is_computed'];
|
||||
$column->unsigned = stripos($column->dbType, 'unsigned') !== false;
|
||||
$column->comment = $info['comment'] === null ? '' : $info['comment'];
|
||||
|
||||
$column->type = self::TYPE_STRING;
|
||||
|
||||
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
|
||||
$type = $matches[1];
|
||||
|
||||
if (isset($this->typeMap[$type])) {
|
||||
$column->type = $this->typeMap[$type];
|
||||
}
|
||||
|
||||
if ($type === 'bit') {
|
||||
$column->type = 'boolean';
|
||||
}
|
||||
|
||||
if (!empty($matches[2])) {
|
||||
$values = explode(',', $matches[2]);
|
||||
$column->size = $column->precision = (int) $values[0];
|
||||
|
||||
if (isset($values[1])) {
|
||||
$column->scale = (int) $values[1];
|
||||
}
|
||||
if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
|
||||
$column->type = 'boolean';
|
||||
} elseif ($type === 'bit') {
|
||||
if ($column->size > 32) {
|
||||
$column->type = 'bigint';
|
||||
} elseif ($column->size === 32) {
|
||||
$column->type = 'integer';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
198
tests/framework/db/mssql/type/BooleanTest.php
Normal file
198
tests/framework/db/mssql/type/BooleanTest.php
Normal file
@ -0,0 +1,198 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yiiunit\framework\db\mssql\type;
|
||||
|
||||
use yii\db\mssql\Schema;
|
||||
use yiiunit\framework\db\DatabaseTestCase;
|
||||
|
||||
/**
|
||||
* @group db
|
||||
* @group mssql
|
||||
*/
|
||||
class BooleanTest extends DatabaseTestCase
|
||||
{
|
||||
protected $driverName = 'sqlsrv';
|
||||
|
||||
public function testBoolean(): void
|
||||
{
|
||||
$db = $this->getConnection(true);
|
||||
$schema = $db->getSchema();
|
||||
$tableName = '{{%boolean}}';
|
||||
|
||||
if ($db->getTableSchema($tableName)) {
|
||||
$db->createCommand()->dropTable($tableName)->execute();
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable(
|
||||
$tableName,
|
||||
[
|
||||
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
|
||||
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
|
||||
]
|
||||
)->execute();
|
||||
|
||||
// test type
|
||||
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
|
||||
$this->assertSame('boolean', $column->phpType);
|
||||
|
||||
// test value `false`
|
||||
$db->createCommand()->insert($tableName, ['bool_col' => false])->execute();
|
||||
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
|
||||
$this->assertEquals(0, $boolValue);
|
||||
|
||||
// test php typecast
|
||||
$phpTypeCast = $column->phpTypecast($boolValue);
|
||||
$this->assertFalse($phpTypeCast);
|
||||
|
||||
// test value `true`
|
||||
$db->createCommand()->insert($tableName, ['bool_col' => true])->execute();
|
||||
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar();
|
||||
$this->assertEquals(1, $boolValue);
|
||||
|
||||
// test php typecast
|
||||
$phpTypeCast = $column->phpTypecast($boolValue);
|
||||
$this->assertTrue($phpTypeCast);
|
||||
}
|
||||
|
||||
public function testBooleanWithValueInteger(): void
|
||||
{
|
||||
$db = $this->getConnection(true);
|
||||
$schema = $db->getSchema();
|
||||
$tableName = '{{%boolean}}';
|
||||
|
||||
if ($db->getTableSchema($tableName)) {
|
||||
$db->createCommand()->dropTable($tableName)->execute();
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable(
|
||||
$tableName,
|
||||
[
|
||||
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
|
||||
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
|
||||
]
|
||||
)->execute();
|
||||
|
||||
// test type
|
||||
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
|
||||
$this->assertSame('boolean', $column->phpType);
|
||||
|
||||
// test value 0
|
||||
$db->createCommand()->insert($tableName, ['bool_col' => 0])->execute();
|
||||
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
|
||||
$this->assertEquals(0, $boolValue);
|
||||
|
||||
// test php typecast
|
||||
$phpTypeCast = $column->phpTypecast($boolValue);
|
||||
$this->assertFalse($phpTypeCast);
|
||||
|
||||
// test value 1
|
||||
$db->createCommand()->insert($tableName, ['bool_col' => 1])->execute();
|
||||
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 2")->queryScalar();
|
||||
$this->assertEquals(1, $boolValue);
|
||||
|
||||
// test php typecast
|
||||
$phpTypeCast = $column->phpTypecast($boolValue);
|
||||
$this->assertTrue($phpTypeCast);
|
||||
}
|
||||
|
||||
public function testBooleanValueNegative(): void
|
||||
{
|
||||
$db = $this->getConnection(true);
|
||||
$schema = $db->getSchema();
|
||||
$tableName = '{{%boolean}}';
|
||||
|
||||
if ($db->getTableSchema($tableName)) {
|
||||
$db->createCommand()->dropTable($tableName)->execute();
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable(
|
||||
$tableName,
|
||||
[
|
||||
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
|
||||
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
|
||||
]
|
||||
)->execute();
|
||||
|
||||
// test type
|
||||
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
|
||||
$this->assertSame('boolean', $column->phpType);
|
||||
|
||||
// test value 2
|
||||
$db->createCommand()->insert($tableName, ['bool_col' => -1])->execute();
|
||||
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
|
||||
$this->assertEquals(1, $boolValue);
|
||||
|
||||
// test php typecast
|
||||
$phpTypeCast = $column->phpTypecast($boolValue);
|
||||
$this->assertTrue($phpTypeCast);
|
||||
}
|
||||
|
||||
public function testBooleanWithValueNull(): void
|
||||
{
|
||||
$db = $this->getConnection(true);
|
||||
$schema = $db->getSchema();
|
||||
$tableName = '{{%boolean}}';
|
||||
|
||||
if ($db->getTableSchema($tableName)) {
|
||||
$db->createCommand()->dropTable($tableName)->execute();
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable(
|
||||
$tableName,
|
||||
[
|
||||
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
|
||||
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
|
||||
]
|
||||
)->execute();
|
||||
|
||||
// test type
|
||||
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
|
||||
$this->assertSame('boolean', $column->phpType);
|
||||
|
||||
// test value `null`
|
||||
$db->createCommand()->insert($tableName, ['bool_col' => null])->execute();
|
||||
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
|
||||
$this->assertNull($boolValue);
|
||||
|
||||
// test php typecast
|
||||
$phpTypeCast = $column->phpTypecast($boolValue);
|
||||
$this->assertNull($phpTypeCast);
|
||||
}
|
||||
|
||||
public function testBooleanWithValueOverflow(): void
|
||||
{
|
||||
$db = $this->getConnection(true);
|
||||
$schema = $db->getSchema();
|
||||
$tableName = '{{%boolean}}';
|
||||
|
||||
if ($db->getTableSchema($tableName)) {
|
||||
$db->createCommand()->dropTable($tableName)->execute();
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable(
|
||||
$tableName,
|
||||
[
|
||||
'id' => $schema->createColumnSchemaBuilder(Schema::TYPE_PK),
|
||||
'bool_col' => $schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN),
|
||||
]
|
||||
)->execute();
|
||||
|
||||
// test type
|
||||
$column = $db->getTableSchema($tableName)->getColumn('bool_col');
|
||||
$this->assertSame('boolean', $column->phpType);
|
||||
|
||||
// test value 2
|
||||
$db->createCommand()->insert($tableName, ['bool_col' => 2])->execute();
|
||||
$boolValue = $db->createCommand("SELECT bool_col FROM $tableName WHERE id = 1")->queryScalar();
|
||||
$this->assertEquals(1, $boolValue);
|
||||
|
||||
// test php typecast
|
||||
$phpTypeCast = $column->phpTypecast($boolValue);
|
||||
$this->assertTrue($phpTypeCast);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user