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

[5.7] Add computed support to SQL Server schema grammar #27346

Merged
merged 4 commits into from
Jan 30, 2019
Merged
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
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,18 @@ public function unsignedDecimal($column, $total = 8, $places = 2)
]);
}

/**
* Create a new generated computed column on the table.
*
* @param string $column
* @param string $expression
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function computed($column, $expression)
{
return $this->addColumn('computed', $column, compact('expression'));
}

/**
* Create a new boolean column on the table.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Database/Schema/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @method ColumnDefinition unsigned() Set the INTEGER column as UNSIGNED (MySQL)
* @method ColumnDefinition useCurrent() Set the TIMESTAMP column to use CURRENT_TIMESTAMP as default value
* @method ColumnDefinition virtualAs(string $expression) Create a virtual generated column (MySQL)
* @method ColumnDefinition persisted() Mark the computed generated column as persistent (SQL Server)
*/
class ColumnDefinition extends Fluent
{
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Schema\Grammars;

use RuntimeException;
use Illuminate\Support\Fluent;
use Doctrine\DBAL\Schema\TableDiff;
use Illuminate\Database\Connection;
Expand Down Expand Up @@ -190,6 +191,18 @@ public function prefixArray($prefix, array $values)
}, $values);
}

/**
* Create the column definition for a generated computed column type.
*
* @param \Illuminate\Support\Fluent $column
*
* @throws \RuntimeException
*/
protected function typeComputed(Fluent $column)
{
throw new RuntimeException('The database driver in use does not support the computed type.');
}

/**
* Wrap a table in keyword identifiers.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Schema\Grammars;

use RuntimeException;
use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint;
Expand Down Expand Up @@ -833,6 +834,18 @@ public function typeMultiPolygon(Fluent $column)
return 'multipolygon';
}

/**
* Create the column definition for a generated computed column type.
*
* @param \Illuminate\Support\Fluent $column
*
* @throws \RuntimeException
*/
protected function typeComputed(Fluent $column)
{
throw new RuntimeException('The database driver in use requires a type, see virtualAs/storedAs modifiers.');
}

/**
* Get the SQL for a generated virtual column modifier.
*
Expand Down
31 changes: 29 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SqlServerGrammar extends Grammar
*
* @var array
*/
protected $modifiers = ['Increment', 'Collate', 'Nullable', 'Default'];
protected $modifiers = ['Increment', 'Collate', 'Nullable', 'Default', 'Persisted'];

/**
* The columns available as serials.
Expand Down Expand Up @@ -733,6 +733,17 @@ public function typeMultiPolygon(Fluent $column)
return 'geography';
}

/**
* Create the column definition for a generated computed column type.
*
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function typeComputed(Fluent $column)
{
return "as ({$column->expression})";
}

/**
* Get the SQL for a collation column modifier.
*
Expand All @@ -756,7 +767,9 @@ protected function modifyCollate(Blueprint $blueprint, Fluent $column)
*/
protected function modifyNullable(Blueprint $blueprint, Fluent $column)
{
return $column->nullable ? ' null' : ' not null';
if ($column->type !== 'computed') {
return $column->nullable ? ' null' : ' not null';
}
}

/**
Expand Down Expand Up @@ -787,6 +800,20 @@ protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
}
}

/**
* Get the SQL for a generated stored column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function modifyPersisted(Blueprint $blueprint, Fluent $column)
{
if ($column->persisted) {
return ' persisted';
}
}

/**
* Wrap a table in keyword identifiers.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,17 @@ public function testAddingMultiPolygon()
$this->assertEquals('alter table "geo" add "coordinates" geography not null', $statements[0]);
}

public function testAddingGeneratedColumn()
{
$blueprint = new Blueprint('products');
$blueprint->integer('price');
$blueprint->computed('discounted_virtual', 'price - 5');
$blueprint->computed('discounted_stored', 'price - 5')->persisted();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertEquals('alter table "products" add "price" int not null, "discounted_virtual" as (price - 5), "discounted_stored" as (price - 5) persisted', $statements[0]);
}

public function testGrammarsAreMacroable()
{
// compileReplace macro.
Expand Down