Skip to content

Commit

Permalink
Complete enumeration columns support
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofreitas committed Nov 17, 2017
1 parent fded7b4 commit 32692dc
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 20 deletions.
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ public function parameterize(array $values)
return implode(', ', array_map([$this, 'parameter'], $values));
}

/**
* Quote string literals.
*
* @param string|array $value
* @return string
*/
public function quote($value)
{
if (is_array($value)) {
return implode(', ', array_map([$this, 'quote'], $value));
}

return "'$value'";
}

/**
* Get the appropriate query parameter place-holder for a value.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,14 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
return "enum('".implode("', '", $column->allowed)."')";
return sprintf('enum(%s)', $this->quote($column->allowed));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,18 +489,18 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
$allowed = array_map(function ($a) {
return "'{$a}'";
}, $column->allowed);

return "varchar(255) check (\"{$column->name}\" in (".implode(', ', $allowed).'))';
return sprintf(
'varchar(255) check ("%s" in (%s))',
$column->name,
$this->quote($column->allowed)
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,18 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
return 'varchar';
return sprintf(
'varchar check ("%s" in (%s))',
$column->name,
$this->quote($column->allowed)
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,18 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
return 'nvarchar(255)';
return sprintf(
'nvarchar(255) check ("%s" in (%s))',
$column->name,
$this->quote($column->allowed)
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table `users` add `foo` enum(\'bar\', \'baz\') not null', $statements[0]);
$this->assertEquals('alter table `users` add `role` enum(\'member\', \'admin\') not null', $statements[0]);
}

public function testAddingJson()
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "foo" varchar(255) check ("foo" in (\'bar\', \'baz\')) not null', $statements[0]);
$this->assertEquals('alter table "users" add column "role" varchar(255) check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
}

public function testAddingDate()
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "foo" varchar not null', $statements[0]);
$this->assertEquals('alter table "users" add column "role" varchar check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
}

public function testAddingJson()
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add "foo" nvarchar(255) not null', $statements[0]);
$this->assertEquals('alter table "users" add "role" nvarchar(255) check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
}

public function testAddingJson()
Expand Down

0 comments on commit 32692dc

Please sign in to comment.