|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* webtrees: online genealogy |
|
5
|
|
|
* Copyright (C) 2024 webtrees development team |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* You should have received a copy of the GNU General Public License |
|
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
declare(strict_types=1); |
|
19
|
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\DB; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Schema\Table as DBALTable; |
|
23
|
|
|
use Fisharebest\Webtrees\DB; |
|
24
|
|
|
|
|
25
|
|
|
use function array_filter; |
|
26
|
|
|
use function array_values; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Fluent/immutable constructors for doctrine/dbal. |
|
30
|
|
|
*/ |
|
31
|
|
|
final class Table extends DBALTable |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var array<int,Column> */ |
|
34
|
|
|
private array $columns; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array<int,Index> */ |
|
37
|
|
|
private array $indexes; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array<int,PrimaryKey> */ |
|
40
|
|
|
private array $primary_keys; |
|
41
|
|
|
|
|
42
|
|
|
/** @var array<int,UniqueIndex> */ |
|
43
|
|
|
private array $unique_indexes; |
|
44
|
|
|
|
|
45
|
|
|
/** @var array<int,ForeignKey> */ |
|
46
|
|
|
private array $foreign_keys; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(private readonly string $name, Column|Index|UniqueIndex|ForeignKey|PrimaryKey ...$components) |
|
49
|
|
|
{ |
|
50
|
|
|
$column_filter = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof Column; |
|
51
|
|
|
$foreign_key_filter = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof ForeignKey; |
|
52
|
|
|
$index_filter = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof Index; |
|
53
|
|
|
$primary_key_filter = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof PrimaryKey; |
|
54
|
|
|
$unique_index_filter = static fn (Column|Index|UniqueIndex|ForeignKey|PrimaryKey $component): bool => $component instanceof UniqueIndex; |
|
55
|
|
|
|
|
56
|
|
|
$this->columns = array_values(array: array_filter(array: $components, callback: $column_filter)); |
|
57
|
|
|
$this->foreign_keys = array_values(array: array_filter(array: $components, callback: $foreign_key_filter)); |
|
58
|
|
|
$this->indexes = array_values(array: array_filter(array: $components, callback: $index_filter)); |
|
59
|
|
|
$this->primary_keys = array_values(array: array_filter(array: $components, callback: $primary_key_filter)); |
|
60
|
|
|
$this->unique_indexes = array_values(array: array_filter(array: $components, callback: $unique_index_filter)); |
|
61
|
|
|
|
|
62
|
|
|
array_walk(array: $this->indexes, callback: self::namedIndex(...)); |
|
63
|
|
|
array_walk(array: $this->unique_indexes, callback: self::namedUniqueIndex(...)); |
|
64
|
|
|
array_walk(array: $this->foreign_keys, callback: self::namedForeignKey(...)); |
|
65
|
|
|
|
|
66
|
|
|
parent::__construct( |
|
67
|
|
|
name: DB::prefix($name), |
|
68
|
|
|
columns: $this->columns, |
|
69
|
|
|
indexes: [...$this->primary_keys, ...$this->unique_indexes, ...$this->indexes], |
|
70
|
|
|
uniqueConstraints: [], |
|
71
|
|
|
fkConstraints: $this->foreign_keys, |
|
72
|
|
|
options: [], |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function namedIndex(Index &$index, int|string $n): void |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$n = 1 + (int) $n; |
|
79
|
|
|
$name = DB::prefix($this->name . '_ix' . $n); |
|
80
|
|
|
$index = $index->name($name); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function namedUniqueIndex(UniqueIndex &$unique_index, int|string $n): void |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$n = 1 + (int) $n; |
|
86
|
|
|
$name = DB::prefix($this->name . '_ux' . $n); |
|
87
|
|
|
$unique_index = $unique_index->name($name); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function namedForeignKey(ForeignKey &$foreign_key, int|string $n): void |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$n = 1 + (int) $n; |
|
93
|
|
|
$name = DB::prefix($this->name . '_fk' . $n); |
|
94
|
|
|
$foreign_key = $foreign_key->name($name); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function dropColumn(string $name): self |
|
98
|
|
|
{ |
|
99
|
|
|
$columns = array_filter(array: $this->columns, callback: static fn (Column $column): bool => $column->getName() !== $name); |
|
100
|
|
|
|
|
101
|
|
|
return new self($this->name, ...$columns, ...$this->indexes, ...$this->primary_keys, ...$this->unique_keys, ...$this->foreign_keys); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.