Skip to content

Commit

Permalink
Fix boolean type. (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Oct 25, 2023
1 parent d46f41c commit 1be5ddb
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 65 deletions.
3 changes: 1 addition & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ build:
- /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE yiitest'
- pecl channel-update pecl.php.net
- pecl install pdo_sqlsrv
- composer config preferred-install.yiisoft/db source
- composer update --no-interaction --no-progress --optimize-autoloader --ansi
- composer require yiisoft/db:dev-master --ansi

tests:
override:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.0.2 under development

- Bug #278: Remove `RECURSIVE` expression from CTE queries (@Tigrov)
- Bug #280: Fix type boolean (@terabytesoftw)

## 1.0.1 July 24, 2023

Expand Down
14 changes: 4 additions & 10 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface
$column->type($this->typeMap[$type]);
}

if ($type === 'bit') {
$column->type(self::TYPE_BOOLEAN);
}

if (!empty($matches[2])) {
$values = explode(',', $matches[2]);
$column->precision((int) $values[0]);
Expand All @@ -452,16 +456,6 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface
if (isset($values[1])) {
$column->scale((int) $values[1]);
}

if ($column->getSize() === 1 && ($type === 'tinyint' || $type === 'bit')) {
$column->type(self::TYPE_BOOLEAN);
} elseif ($type === 'bit') {
if ($column->getSize() > 32) {
$column->type(self::TYPE_BIGINT);
} elseif ($column->getSize() === 32) {
$column->type(self::TYPE_INTEGER);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testPhpTypeCast(): void
$this->assertSame(1.234, $floatColPhpType);
$this->assertSame("\x10\x11\x12", $blobColPhpType);
$this->assertSame('2023-07-11 14:50:00.123', $datetimeColPhpType);
$this->assertEquals(false, $boolColPhpType);
$this->assertFalse($boolColPhpType);

$db->close();
}
Expand Down
16 changes: 0 additions & 16 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@ public function testAlterColumn(): void
$this->assertSame('ntext', $columns['email']->getDbType());
}

/**
* @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\CommandProvider::batchInsert
*
* @throws Throwable
*/
public function testBatchInsert(
string $table,
array $columns,
array $values,
string $expected,
array $expectedParams = [],
int $insertedRow = 1
): void {
parent::testBatchInsert($table, $columns, $values, $expected, $expectedParams, $insertedRow);
}

/**
* @throws Exception
* @throws InvalidConfigException
Expand Down
16 changes: 0 additions & 16 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@ final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider

protected static string $driverName = 'sqlsrv';

public static function batchInsert(): array
{
$batchInsert = parent::batchInsert();

$batchInsert['multirow']['expectedParams'][':qp3'] = 1;
$batchInsert['multirow']['expectedParams'][':qp7'] = 0;

$batchInsert['issue11242']['expectedParams'][':qp3'] = 1;

$batchInsert['wrongBehavior']['expectedParams'][':qp3'] = 0;

$batchInsert['batchInsert binds params from expression']['expectedParams'][':qp3'] = 0;

return $batchInsert;
}

/**
* @throws JsonException
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static function insertWithReturningPks(): array
['{{%type}}.[[related_id]]' => null, '[[time]]' => new Expression('now()')],
[],
<<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([int_col] int , [int_col2] int NULL, [tinyint_col] tinyint NULL, [smallint_col] smallint NULL, [char_col] char(100) , [char_col2] varchar(100) NULL, [char_col3] text NULL, [float_col] decimal(4,3) , [float_col2] float NULL, [blob_col] varbinary(MAX) NULL, [numeric_col] decimal(5,2) NULL, [datetime_col] datetime , [bool_col] tinyint , [bool_col2] tinyint NULL);INSERT INTO {{%type}} ([related_id], [time]) OUTPUT INSERTED.[int_col],INSERTED.[int_col2],INSERTED.[tinyint_col],INSERTED.[smallint_col],INSERTED.[char_col],INSERTED.[char_col2],INSERTED.[char_col3],INSERTED.[float_col],INSERTED.[float_col2],INSERTED.[blob_col],INSERTED.[numeric_col],INSERTED.[datetime_col],INSERTED.[bool_col],INSERTED.[bool_col2] INTO @temporary_inserted VALUES (:qp0, now());SELECT * FROM @temporary_inserted;
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([int_col] int , [int_col2] int NULL, [tinyint_col] tinyint NULL, [smallint_col] smallint NULL, [char_col] char(100) , [char_col2] varchar(100) NULL, [char_col3] text NULL, [float_col] decimal(4,3) , [float_col2] float NULL, [blob_col] varbinary(MAX) NULL, [numeric_col] decimal(5,2) NULL, [datetime_col] datetime , [bool_col] bit , [bool_col2] bit NULL);INSERT INTO {{%type}} ([related_id], [time]) OUTPUT INSERTED.[int_col],INSERTED.[int_col2],INSERTED.[tinyint_col],INSERTED.[smallint_col],INSERTED.[char_col],INSERTED.[char_col2],INSERTED.[char_col3],INSERTED.[float_col],INSERTED.[float_col2],INSERTED.[blob_col],INSERTED.[numeric_col],INSERTED.[datetime_col],INSERTED.[bool_col],INSERTED.[bool_col2] INTO @temporary_inserted VALUES (:qp0, now());SELECT * FROM @temporary_inserted;
SQL,
[':qp0' => null],
],
Expand Down
14 changes: 7 additions & 7 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ public static function columns(): array
'defaultValue' => '2002-01-01 00:00:00',
],
'bool_col' => [
'type' => 'tinyint',
'dbType' => 'tinyint',
'phpType' => 'integer',
'type' => 'boolean',
'dbType' => 'bit',
'phpType' => 'boolean',
'primaryKey' => false,
'allowNull' => false,
'autoIncrement' => false,
Expand All @@ -184,17 +184,17 @@ public static function columns(): array
'defaultValue' => null,
],
'bool_col2' => [
'type' => 'tinyint',
'dbType' => 'tinyint',
'phpType' => 'integer',
'type' => 'boolean',
'dbType' => 'bit',
'phpType' => 'boolean',
'primaryKey' => false,
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => [],
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => 1,
'defaultValue' => true,
],
],
'tableName' => 'type',
Expand Down
5 changes: 2 additions & 3 deletions tests/Provider/Type/BitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ final class BitProvider
public static function columns(): array
{
return [
['Mybit1', 'bit', 'integer', 0],
['Mybit2', 'bit', 'integer', 1],
['Mybit3', 'bit', 'integer', 2],
['Mybit1', 'bit', 'boolean', false],
['Mybit2', 'bit', 'boolean', true],
];
}
}
1 change: 0 additions & 1 deletion tests/Support/Fixture/Type/bit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ CREATE TABLE [dbo].[bit_default] (
[id] [int] IDENTITY NOT NULL,
[Mybit1] [bit] NOT NULL DEFAULT 0,
[Mybit2] [bit] NOT NULL DEFAULT 1,
[Mybit3] [bit] NOT NULL DEFAULT 2,
CONSTRAINT [PK_bit_default_pk] PRIMARY KEY CLUSTERED (
[id] ASC
) ON [PRIMARY]
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/Fixture/mssql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ CREATE TABLE [dbo].[type] (
[blob_col] [varbinary](MAX),
[numeric_col] [decimal](5,2) DEFAULT '33.22',
[datetime_col] [datetime] NOT NULL DEFAULT '2002-01-01 00:00:00',
[bool_col] [tinyint] NOT NULL,
[bool_col2] [tinyint] DEFAULT '1'
[bool_col] [bit] NOT NULL,
[bool_col2] [bit] DEFAULT '1'
);

CREATE TABLE [dbo].[animal] (
Expand Down
10 changes: 4 additions & 6 deletions tests/Type/BitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testCreateTableWithDefaultValue(
string $column,
string $dbType,
string $phpType,
int $defaultValue
bool $defaultValue
): void {
$db = $this->buildTable();

Expand Down Expand Up @@ -89,7 +89,7 @@ public function testDefaultValue(
string $column,
string $dbType,
string $phpType,
int $defaultValue
bool $defaultValue
): void {
$this->setFixture('Type/bit.sql');

Expand Down Expand Up @@ -198,7 +198,7 @@ public function testMaxValue(): void
$this->assertSame(
[
'id' => '2',
'Mybit1' => '0',
'Mybit1' => '1',
'Mybit2' => '1',
'Mybit3' => '1',
],
Expand Down Expand Up @@ -251,7 +251,7 @@ public function testMinValue(): void
[
'id' => '2',
'Mybit1' => null,
'Mybit2' => '0',
'Mybit2' => '1',
'Mybit3' => '1',
],
$command->setSql(
Expand Down Expand Up @@ -280,7 +280,6 @@ private function buildTable(): ConnectionInterface
'id' => 'INT IDENTITY NOT NULL',
'Mybit1' => 'BIT DEFAULT 0', // Min value
'Mybit2' => 'BIT DEFAULT 1', // Max value
'Mybit3' => 'BIT DEFAULT 2', // Max value
],
)->execute();

Expand All @@ -293,7 +292,6 @@ private function getColumns(): array
'id' => '1',
'Mybit1' => '0',
'Mybit2' => '1',
'Mybit3' => '1',
];
}
}

0 comments on commit 1be5ddb

Please sign in to comment.