Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for BOOLEAN column type #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DataIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public static function coerceValueToColumn(
}

switch ($php_type) {
case 'boolean':
case 'int':
return (int) $value;

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
10 changes: 10 additions & 0 deletions src/Parser/SQLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ private static function buildTokenListFromLexemes(array $tokens)
continue;
}

if ($token === 'true') {
$out[] = new Token(TokenType::NUMERIC_CONSTANT, '1', $token, $start);
continue;
}

if ($token === 'false') {
$out[] = new Token(TokenType::NUMERIC_CONSTANT, '0', $token, $start);
continue;
}

if (\preg_match('/^[0-9\.]+$/', $token)) {
$out[] = new Token(TokenType::NUMERIC_CONSTANT, $token, $token, $start);
continue;
Expand Down
6 changes: 5 additions & 1 deletion 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 @@ -239,7 +240,7 @@ private static function getDefinitionColumn(Query\MysqlColumnType $stmt) : Colum
}

/**
* @return Column\BigInt|Column\IntColumn|Column\MediumInt|Column\SmallInt|Column\TinyInt
* @return Column\BigInt|Column\IntColumn|Column\MediumInt|Column\SmallInt|Column\TinyInt|Column\Boolean
*/
private static function getIntegerDefinitionColumn(Query\MysqlColumnType $stmt)
{
Expand All @@ -248,6 +249,9 @@ private static function getIntegerDefinitionColumn(Query\MysqlColumnType $stmt)
$display_width = (int) $stmt->length;

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

case DataType::TINYINT:
return new Column\TinyInt($unsigned, $display_width);

Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getNullablePhp() : string
}

/**
* @return 'int'|'string'|'float'|'null'
* @return 'int'|'string'|'float'|'null'|'boolean'
*/
abstract public function getPhpType() : string;

Expand Down
53 changes: 53 additions & 0 deletions src/Schema/Column/Boolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;
}

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

/**
* @return string
*/
public function getPhpCode() : string
{
$default = '';

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

return '(new \\' . static::class . '('
. '))'
. $default
. $this->getNullablePhp();
}
}
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