Skip to content

Commit

Permalink
Support for BOOLEAN column type
Browse files Browse the repository at this point in the history
  • Loading branch information
SirPL committed Jun 30, 2022
1 parent 3be4c68 commit 6ffb86e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class DataType
const TIMESTAMP = 'TIMESTAMP';
const DECIMAL = 'DECIMAL';
const NUMERIC = 'NUMERIC';
const BOOLEAN = 'BOOLEAN';

private function __construct()
{
Expand Down
1 change: 1 addition & 0 deletions src/Parser/CreateTableParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ public static function parseFieldType(array &$tokens, bool $allowArbitaryType =
case 'BLOB':
case 'MEDIUMBLOB':
case 'LONGBLOB':
case 'BOOLEAN':
break;
case 'TINYINT':
case 'SMALLINT':
Expand Down
2 changes: 2 additions & 0 deletions src/Processor/CreateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private static function getDefinitionColumn(Query\MysqlColumnType $stmt) : Colum
case DataType::BIT:
case DataType::MEDIUMINT:
case DataType::BIGINT:
case DataType::BOOLEAN:
if ($stmt->null === null) {
$stmt->null = true;
}
Expand Down Expand Up @@ -249,6 +250,7 @@ private static function getIntegerDefinitionColumn(Query\MysqlColumnType $stmt)

switch (strtoupper($stmt->type)) {
case DataType::TINYINT:
case DataType::BOOLEAN:
return new Column\TinyInt($unsigned, $display_width);

case DataType::SMALLINT:
Expand Down
18 changes: 18 additions & 0 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,24 @@ public function testSelectNullableFields()
);
}

public function testBoolean()
{
$pdo = self::getConnectionToFullDB(false, false);

$query = $pdo->prepare("SELECT `id`, `enabled` FROM `enemies`");
$query->execute();

$this->assertSame(
[
['id' => 1, 'enabled' => 1],
['id' => 2, 'enabled' => 0],
['id' => 3, 'enabled' => 0],
['id' => 4, 'enabled' => 1]
],
$query->fetchAll(\PDO::FETCH_ASSOC)
);
}

private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO
{
$options = $strict_mode ? [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_ALL_TABLES"'] : [];
Expand Down
10 changes: 5 additions & 5 deletions tests/fixtures/bulk_enemy_insert.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
INSERT INTO `enemies`
(`id`, `character_id`, `enemy_id`)
(`id`, `character_id`, `enemy_id`, `enabled`)
VALUES
(1, 1, 5),
(2, 2, 5),
(3, 3, 6),
(4, 1, 11)
(1, 1, 5, true),
(2, 2, 5, false),
(3, 3, 6, false),
(4, 1, 11, true)
1 change: 1 addition & 0 deletions tests/fixtures/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CREATE TABLE `enemies` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`character_id` int(10) NOT NULL,
`enemy_id` int(10) NOT NULL,
`enabled` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`id`),
KEY `character_id` (`character_id`),
KEY `enemy_id` (`enemy_id`)
Expand Down

0 comments on commit 6ffb86e

Please sign in to comment.