Skip to content

Commit 91e7a7a

Browse files
committed
Merge branch 'master' of https://github.com/iamgergo/framework into iamgergo-master
2 parents 61d8d36 + f112e41 commit 91e7a7a

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

src/Illuminate/Database/Schema/ColumnDefinition.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Illuminate\Database\Schema;
44

5-
use Illuminate\Database\Query\Expression;
65
use Illuminate\Support\Fluent;
76

87
/**
@@ -21,12 +20,12 @@
2120
* @method $this persisted() Mark the computed generated column as persistent (SQL Server)
2221
* @method $this primary() Add a primary index
2322
* @method $this spatialIndex() Add a spatial index
24-
* @method $this storedAs(string $expression) Create a stored generated column (MySQL)
23+
* @method $this storedAs(string $expression) Create a stored generated column (MySQL/SQLite)
2524
* @method $this type(string $type) Specify a type for the column
2625
* @method $this unique(string $indexName = null) Add a unique index
2726
* @method $this unsigned() Set the INTEGER column as UNSIGNED (MySQL)
2827
* @method $this useCurrent() Set the TIMESTAMP column to use CURRENT_TIMESTAMP as default value
29-
* @method $this virtualAs(string $expression) Create a virtual generated column (MySQL)
28+
* @method $this virtualAs(string $expression) Create a virtual generated column (MySQL/SQLite)
3029
*/
3130
class ColumnDefinition extends Fluent
3231
{

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SQLiteGrammar extends Grammar
1616
*
1717
* @var array
1818
*/
19-
protected $modifiers = ['Nullable', 'Default', 'Increment'];
19+
protected $modifiers = ['VirtualAs', 'StoredAs', 'Nullable', 'Default', 'Increment'];
2020

2121
/**
2222
* The columns available as serials.
@@ -137,7 +137,9 @@ public function compileAdd(Blueprint $blueprint, Fluent $command)
137137
{
138138
$columns = $this->prefixArray('add column', $this->getColumns($blueprint));
139139

140-
return collect($columns)->map(function ($column) use ($blueprint) {
140+
return collect($columns)->reject(function ($column) {
141+
return preg_match('/as \(.*\) stored/', $column) > 0;
142+
})->map(function ($column) use ($blueprint) {
141143
return 'alter table '.$this->wrapTable($blueprint).' '.$column;
142144
})->all();
143145
}
@@ -822,6 +824,47 @@ public function typeMultiPolygon(Fluent $column)
822824
return 'multipolygon';
823825
}
824826

827+
/**
828+
* Create the column definition for a generated, computed column type.
829+
*
830+
* @param \Illuminate\Support\Fluent $column
831+
* @return void
832+
*
833+
* @throws \RuntimeException
834+
*/
835+
protected function typeComputed(Fluent $column)
836+
{
837+
throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.');
838+
}
839+
840+
/**
841+
* Get the SQL for a generated virtual column modifier.
842+
*
843+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
844+
* @param \Illuminate\Support\Fluent $column
845+
* @return string|null
846+
*/
847+
protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column)
848+
{
849+
if (! is_null($column->virtualAs)) {
850+
return " as ({$column->virtualAs})";
851+
}
852+
}
853+
854+
/**
855+
* Get the SQL for a generated stored column modifier.
856+
*
857+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
858+
* @param \Illuminate\Support\Fluent $column
859+
* @return string|null
860+
*/
861+
protected function modifyStoredAs(Blueprint $blueprint, Fluent $column)
862+
{
863+
if (! is_null($column->storedAs)) {
864+
return " as ({$column->storedAs}) stored";
865+
}
866+
}
867+
825868
/**
826869
* Get the SQL for a nullable column modifier.
827870
*
@@ -831,7 +874,13 @@ public function typeMultiPolygon(Fluent $column)
831874
*/
832875
protected function modifyNullable(Blueprint $blueprint, Fluent $column)
833876
{
834-
return $column->nullable ? ' null' : ' not null';
877+
if (is_null($column->virtualAs) && is_null($column->storedAs)) {
878+
return $column->nullable ? ' null' : ' not null';
879+
}
880+
881+
if ($column->nullable === false) {
882+
return ' not null';
883+
}
835884
}
836885

837886
/**
@@ -843,7 +892,7 @@ protected function modifyNullable(Blueprint $blueprint, Fluent $column)
843892
*/
844893
protected function modifyDefault(Blueprint $blueprint, Fluent $column)
845894
{
846-
if (! is_null($column->default)) {
895+
if (! is_null($column->default) && is_null($column->virtualAs) && is_null($column->storedAs)) {
847896
return ' default '.$this->getDefaultValue($column->default);
848897
}
849898
}

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,32 @@ public function testAddingMultiPolygon()
831831
$this->assertSame('alter table "geo" add column "coordinates" multipolygon not null', $statements[0]);
832832
}
833833

834+
public function testAddingGeneratedColumn()
835+
{
836+
$blueprint = new Blueprint('products');
837+
$blueprint->create();
838+
$blueprint->integer('price');
839+
$blueprint->integer('discounted_virtual')->virtualAs('"price" - 5');
840+
$blueprint->integer('discounted_stored')->storedAs('"price" - 5');
841+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
842+
843+
$this->assertCount(1, $statements);
844+
$this->assertSame('create table "products" ("price" integer not null, "discounted_virtual" integer as ("price" - 5), "discounted_stored" integer as ("price" - 5) stored)', $statements[0]);
845+
846+
$blueprint = new Blueprint('products');
847+
$blueprint->integer('price');
848+
$blueprint->integer('discounted_virtual')->virtualAs('"price" - 5')->nullable(false);
849+
$blueprint->integer('discounted_stored')->storedAs('"price" - 5')->nullable(false);
850+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
851+
852+
$this->assertCount(2, $statements);
853+
$expected = [
854+
'alter table "products" add column "price" integer not null',
855+
'alter table "products" add column "discounted_virtual" integer as ("price" - 5) not null',
856+
];
857+
$this->assertSame($expected, $statements);
858+
}
859+
834860
public function testGrammarsAreMacroable()
835861
{
836862
// compileReplace macro.

0 commit comments

Comments
 (0)