diff --git a/src/Illuminate/Database/Grammar.php b/src/Illuminate/Database/Grammar.php index 243f07c30c07..5dbe5c074c96 100755 --- a/src/Illuminate/Database/Grammar.php +++ b/src/Illuminate/Database/Grammar.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index 8f26af5250c3..215cec2475e6 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -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)); } /** diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index ab03a2623195..ac06d0bbe3f3 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -512,18 +512,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) + ); } /** diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index f8a8c175a57e..577978c5318a 100755 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -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) + ); } /** diff --git a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php index 149a6ae6b163..645bd32472d5 100755 --- a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php @@ -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) + ); } /** diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 059d03753d23..ca3e6b39dafb 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -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() diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index c64fa66a977a..9d6c2ed2aa6c 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -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 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() diff --git a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php index df1a4b3c132d..5c898c14c288 100755 --- a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php +++ b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php @@ -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() diff --git a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php index 0892f0140035..aa8343c21802 100755 --- a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php +++ b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php @@ -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()