From d69897673195e67a4ed49573c96b6b6f3127ef93 Mon Sep 17 00:00:00 2001 From: Paulo Freitas Date: Tue, 21 Nov 2017 14:05:17 -0200 Subject: [PATCH] Complete enumeration columns support --- src/Illuminate/Database/Grammar.php | 15 +++++++++++++++ .../Database/Schema/Grammars/MySqlGrammar.php | 4 ++-- .../Database/Schema/Grammars/PostgresGrammar.php | 12 ++++++------ .../Database/Schema/Grammars/SQLiteGrammar.php | 8 ++++++-- .../Database/Schema/Grammars/SqlServerGrammar.php | 8 ++++++-- tests/Database/DatabaseMySqlSchemaGrammarTest.php | 4 ++-- .../DatabasePostgresSchemaGrammarTest.php | 4 ++-- .../Database/DatabaseSQLiteSchemaGrammarTest.php | 4 ++-- .../DatabaseSqlServerSchemaGrammarTest.php | 4 ++-- 9 files changed, 43 insertions(+), 20 deletions(-) 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 d71dbd78a0e7..24c3313d38bd 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 ecf74e522a95..9be8ca5956fe 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 11aa4ae8fd3c..7e8008c1b270 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 4202c343b77c..32b06ac82c71 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 261cf1e75c59..463404f40881 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 6b0072249726..7fe93cb53010 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 1aa538b9ccca..6023724d793b 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 30f023000551..3190104abc09 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()