Skip to content

Commit e46bdbe

Browse files
authored
Merge pull request #261 from kitloong/feature/generation
Generate generated column with built-in `generation`
2 parents d04d666 + 324c155 commit e46bdbe

File tree

4 files changed

+7
-28
lines changed

4 files changed

+7
-28
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"mockery/mockery": "^1.0",
3030
"orchestra/testbench": "^9.0|^10.0",
3131
"phpmd/phpmd": "^2.10",
32-
"phpstan/phpstan-mockery": "^2.0",
32+
"phpstan/phpstan-mockery": "^1.0|^2.0",
3333
"slevomat/coding-standard": "^8.0",
3434
"squizlabs/php_codesniffer": "^3.5"
3535
},

src/Database/Models/DatabaseColumn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public function __construct(string $table, array $column)
7979
$this->presetValues = [];
8080
$this->onUpdateCurrentTimestamp = false;
8181
$this->rawDefault = false;
82-
$this->virtualDefinition = null;
83-
$this->storedDefinition = null;
82+
$this->virtualDefinition = $column['generation'] !== null && $column['generation']['type'] === 'virtual' ? $column['generation']['expression'] : null;
83+
$this->storedDefinition = $column['generation'] !== null && $column['generation']['type'] === 'stored' ? $column['generation']['expression'] : null ;
8484
$this->spatialSubType = null;
8585
$this->spatialSrID = null;
8686

src/Database/Models/MySQL/MySQLColumn.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,31 +222,27 @@ private function isJson(): bool
222222
*/
223223
private function setVirtualDefinition(): void
224224
{
225-
$virtualDefinition = $this->mysqlRepository->getVirtualDefinition($this->tableName, $this->name);
226-
227-
if ($virtualDefinition === null) {
225+
if ($this->virtualDefinition === null) {
228226
return;
229227
}
230228

231229
// The definition of MySQL8 returned `concat(string,_utf8mb4\' \',string_255)`.
232230
// Replace `\'` to `'` here to avoid double escape.
233-
$this->virtualDefinition = str_replace("\'", "'", $virtualDefinition);
231+
$this->virtualDefinition = str_replace("\'", "'", $this->virtualDefinition);
234232
}
235233

236234
/**
237235
* Set stored definition if the column is stored.
238236
*/
239237
private function setStoredDefinition(): void
240238
{
241-
$storedDefinition = $this->mysqlRepository->getStoredDefinition($this->tableName, $this->name);
242-
243-
if ($storedDefinition === null) {
239+
if ($this->storedDefinition === null) {
244240
return;
245241
}
246242

247243
// The definition of MySQL8 returned `concat(string,_utf8mb4\' \',string_255)`.
248244
// Replace `\'` to `'` here to avoid double escape.
249-
$this->storedDefinition = str_replace("\'", "'", $storedDefinition);
245+
$this->storedDefinition = str_replace("\'", "'", $this->storedDefinition);
250246
}
251247

252248
/**

src/Database/Models/PgSQL/PgSQLColumn.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public function __construct(string $table, array $column)
5757

5858
default:
5959
}
60-
61-
$this->setStoredDefinition();
6260
}
6361

6462
/**
@@ -181,19 +179,4 @@ private function getEnumPresetValues(): array
181179

182180
return $presetValues;
183181
}
184-
185-
/**
186-
* Set stored definition if the column is stored.
187-
*/
188-
private function setStoredDefinition(): void
189-
{
190-
$this->storedDefinition = $this->repository->getStoredDefinition($this->tableName, $this->name);
191-
192-
// A generated column cannot have a column default or an identity definition.
193-
if ($this->storedDefinition === null) {
194-
return;
195-
}
196-
197-
$this->default = null;
198-
}
199182
}

0 commit comments

Comments
 (0)