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 cdd38cb
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/DataIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ public static function coerceValueToColumn(

return (string) $value;

case 'boolean':
return (int) $value;

default:
throw new \Exception(
"DataIntegrity::coerceValueToSchema found unknown type for field: '{$php_type}'"
Expand Down
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
3 changes: 3 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 @@ -248,6 +249,8 @@ private static function getIntegerDefinitionColumn(Query\MysqlColumnType $stmt)
$display_width = (int) $stmt->length;

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

Expand Down
52 changes: 52 additions & 0 deletions src/Schema/Column/Boolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Vimeo\MysqlEngine\Schema\Column;

class Boolean extends \Vimeo\MysqlEngine\Schema\Column implements NumberColumn, Defaultable
{
use MySqlDefaultTrait;

/**
* @return int
*/
public function getMaxValue()
{
return 1;
}

/**
* @return int
*/
public function getMinValue()
{
return 0;
}

public function getPhpType(): string
{
return 'boolean';
}

public function getPhpCode(): string
{
$default = '';

if ($this instanceof Defaultable && $this->hasDefault()) {
$default = '->setDefault('
. ($this->getDefault() === null
? 'null'
: '\'' . $this->getDefault() . '\'')
. ')';
}

$output = '(new \\' . static::class . '('
. ($this->unsigned ? 'true' : 'false')
. ', ' . $this->integer_display_width
. '))'
. $default
. $this->getNullablePhp();

var_export($output);
return $output;
}
}
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 cdd38cb

Please sign in to comment.