Skip to content

Commit cdd38cb

Browse files
committed
Support for BOOLEAN column type
1 parent 3be4c68 commit cdd38cb

File tree

8 files changed

+84
-5
lines changed

8 files changed

+84
-5
lines changed

src/DataIntegrity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ public static function coerceValueToColumn(
251251

252252
return (string) $value;
253253

254+
case 'boolean':
255+
return (int) $value;
256+
254257
default:
255258
throw new \Exception(
256259
"DataIntegrity::coerceValueToSchema found unknown type for field: '{$php_type}'"

src/DataType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class DataType
3737
const TIMESTAMP = 'TIMESTAMP';
3838
const DECIMAL = 'DECIMAL';
3939
const NUMERIC = 'NUMERIC';
40+
const BOOLEAN = 'BOOLEAN';
4041

4142
private function __construct()
4243
{

src/Parser/CreateTableParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ public static function parseFieldType(array &$tokens, bool $allowArbitaryType =
478478
case 'BLOB':
479479
case 'MEDIUMBLOB':
480480
case 'LONGBLOB':
481+
case 'BOOLEAN':
481482
break;
482483
case 'TINYINT':
483484
case 'SMALLINT':

src/Processor/CreateProcessor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ private static function getDefinitionColumn(Query\MysqlColumnType $stmt) : Colum
147147
case DataType::BIT:
148148
case DataType::MEDIUMINT:
149149
case DataType::BIGINT:
150+
case DataType::BOOLEAN:
150151
if ($stmt->null === null) {
151152
$stmt->null = true;
152153
}
@@ -248,6 +249,8 @@ private static function getIntegerDefinitionColumn(Query\MysqlColumnType $stmt)
248249
$display_width = (int) $stmt->length;
249250

250251
switch (strtoupper($stmt->type)) {
252+
case DataType::BOOLEAN:
253+
return new Column\Boolean($unsigned, $display_width);
251254
case DataType::TINYINT:
252255
return new Column\TinyInt($unsigned, $display_width);
253256

src/Schema/Column/Boolean.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Vimeo\MysqlEngine\Schema\Column;
4+
5+
class Boolean extends \Vimeo\MysqlEngine\Schema\Column implements NumberColumn, Defaultable
6+
{
7+
use MySqlDefaultTrait;
8+
9+
/**
10+
* @return int
11+
*/
12+
public function getMaxValue()
13+
{
14+
return 1;
15+
}
16+
17+
/**
18+
* @return int
19+
*/
20+
public function getMinValue()
21+
{
22+
return 0;
23+
}
24+
25+
public function getPhpType(): string
26+
{
27+
return 'boolean';
28+
}
29+
30+
public function getPhpCode(): string
31+
{
32+
$default = '';
33+
34+
if ($this instanceof Defaultable && $this->hasDefault()) {
35+
$default = '->setDefault('
36+
. ($this->getDefault() === null
37+
? 'null'
38+
: '\'' . $this->getDefault() . '\'')
39+
. ')';
40+
}
41+
42+
$output = '(new \\' . static::class . '('
43+
. ($this->unsigned ? 'true' : 'false')
44+
. ', ' . $this->integer_display_width
45+
. '))'
46+
. $default
47+
. $this->getNullablePhp();
48+
49+
var_export($output);
50+
return $output;
51+
}
52+
}

tests/EndToEndTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,24 @@ public function testSelectNullableFields()
10921092
);
10931093
}
10941094

1095+
public function testBoolean()
1096+
{
1097+
$pdo = self::getConnectionToFullDB(false, false);
1098+
1099+
$query = $pdo->prepare("SELECT `id`, `enabled` FROM `enemies`");
1100+
$query->execute();
1101+
1102+
$this->assertSame(
1103+
[
1104+
['id' => 1, 'enabled' => 1],
1105+
['id' => 2, 'enabled' => 0],
1106+
['id' => 3, 'enabled' => 0],
1107+
['id' => 4, 'enabled' => 1]
1108+
],
1109+
$query->fetchAll(\PDO::FETCH_ASSOC)
1110+
);
1111+
}
1112+
10951113
private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO
10961114
{
10971115
$options = $strict_mode ? [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_ALL_TABLES"'] : [];

tests/fixtures/bulk_enemy_insert.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
INSERT INTO `enemies`
2-
(`id`, `character_id`, `enemy_id`)
2+
(`id`, `character_id`, `enemy_id`, `enabled`)
33
VALUES
4-
(1, 1, 5),
5-
(2, 2, 5),
6-
(3, 3, 6),
7-
(4, 1, 11)
4+
(1, 1, 5, true),
5+
(2, 2, 5, false),
6+
(3, 3, 6, false),
7+
(4, 1, 11, true)

tests/fixtures/create_table.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CREATE TABLE `enemies` (
2828
`id` int(10) NOT NULL AUTO_INCREMENT,
2929
`character_id` int(10) NOT NULL,
3030
`enemy_id` int(10) NOT NULL,
31+
`enabled` boolean NOT NULL DEFAULT true,
3132
PRIMARY KEY (`id`),
3233
KEY `character_id` (`character_id`),
3334
KEY `enemy_id` (`enemy_id`)

0 commit comments

Comments
 (0)