1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Schema; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
8
|
|
|
use Yiisoft\Db\Schema\Column\ColumnInterface; |
9
|
|
|
|
10
|
|
|
use function array_keys; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Represents the metadata of a database table. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractTableSchema implements TableSchemaInterface |
16
|
|
|
{ |
17
|
|
|
private string $schemaName = ''; |
18
|
|
|
private string $name = ''; |
19
|
|
|
private string|null $comment = null; |
20
|
|
|
private string|null $sequenceName = null; |
21
|
|
|
/** @psalm-var string[] */ |
22
|
|
|
private array $primaryKey = []; |
23
|
|
|
/** @psalm-var array<array-key, array> */ |
24
|
|
|
protected array $foreignKeys = []; |
25
|
|
|
protected string|null $createSql = null; |
26
|
|
|
private string|null $catalogName = null; |
27
|
|
|
private string|null $serverName = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ColumnInterface[] $columns |
31
|
|
|
* |
32
|
|
|
* @psalm-param array<string, ColumnInterface> $columns |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
private string $fullName = '', |
36
|
|
|
private array $columns = [], |
37
|
|
|
) { |
38
|
|
|
$values = explode('.', $this->fullName, 2); |
39
|
|
|
|
40
|
|
|
if (count($values) === 2) { |
41
|
|
|
[$this->schemaName, $this->name] = $values; |
42
|
|
|
} else { |
43
|
|
|
$this->name = $this->fullName; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ($columns as $columnName => $column) { |
47
|
|
|
if ($column->isPrimaryKey()) { |
48
|
|
|
$this->primaryKey[] = $columnName; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getColumn(string $name): ColumnInterface|null |
54
|
|
|
{ |
55
|
|
|
return $this->columns[$name] ?? null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getColumnNames(): array |
59
|
|
|
{ |
60
|
|
|
return array_keys($this->columns); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getSchemaName(): string|null |
64
|
|
|
{ |
65
|
|
|
return $this->schemaName; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getName(): string |
69
|
|
|
{ |
70
|
|
|
return $this->name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getFullName(): string|null |
74
|
|
|
{ |
75
|
|
|
return $this->fullName; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getSequenceName(): string|null |
79
|
|
|
{ |
80
|
|
|
return $this->sequenceName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPrimaryKey(): array |
84
|
|
|
{ |
85
|
|
|
return $this->primaryKey; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getColumns(): array |
89
|
|
|
{ |
90
|
|
|
return $this->columns; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getComment(): string|null |
94
|
|
|
{ |
95
|
|
|
return $this->comment; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function schemaName(string|null $value): void |
99
|
|
|
{ |
100
|
|
|
$this->schemaName = $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function name(string $value): void |
104
|
|
|
{ |
105
|
|
|
$this->name = $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function fullName(string|null $value): void |
109
|
|
|
{ |
110
|
|
|
$this->fullName = $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function comment(string|null $value): void |
114
|
|
|
{ |
115
|
|
|
$this->comment = $value; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function sequenceName(string|null $value): void |
119
|
|
|
{ |
120
|
|
|
$this->sequenceName = $value; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function primaryKey(string $value): void |
124
|
|
|
{ |
125
|
|
|
$this->primaryKey[] = $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function column(string $name, ColumnInterface $value): void |
129
|
|
|
{ |
130
|
|
|
$this->columns[$name] = $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getCatalogName(): string|null |
134
|
|
|
{ |
135
|
|
|
return $this->catalogName; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function catalogName(string|null $value): void |
139
|
|
|
{ |
140
|
|
|
$this->catalogName = $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getServerName(): string|null |
144
|
|
|
{ |
145
|
|
|
return $this->serverName; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function serverName(string|null $value): void |
149
|
|
|
{ |
150
|
|
|
$this->serverName = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getCreateSql(): string|null |
154
|
|
|
{ |
155
|
|
|
return $this->createSql; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function createSql(string $sql): void |
159
|
|
|
{ |
160
|
|
|
$this->createSql = $sql; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getForeignKeys(): array |
164
|
|
|
{ |
165
|
|
|
return $this->foreignKeys; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function foreignKeys(array $value): void |
169
|
|
|
{ |
170
|
|
|
$this->foreignKeys = $value; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function foreignKey(string|int $id, array $to): void |
174
|
|
|
{ |
175
|
|
|
$this->foreignKeys[$id] = $to; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function compositeForeignKey(int $id, string $from, string $to): void |
179
|
|
|
{ |
180
|
|
|
throw new NotSupportedException(static::class . ' does not support composite FK.'); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|