1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
use function array_keys; |
7
|
|
|
use function array_map; |
8
|
|
|
use function in_array; |
9
|
|
|
use function strrpos; |
10
|
|
|
use function strtolower; |
11
|
|
|
use function strtoupper; |
12
|
|
|
use function substr; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* An abstraction class for a foreign key constraint. |
16
|
|
|
*/ |
17
|
|
|
class ForeignKeyConstraint extends AbstractAsset implements Constraint |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Instance of the referencing table the foreign key constraint is associated with. |
21
|
|
|
* |
22
|
|
|
* @var Table |
23
|
|
|
*/ |
24
|
|
|
protected $_localTable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Asset identifier instances of the referencing table column names the foreign key constraint is associated with. |
28
|
|
|
* array($columnName => Identifier) |
29
|
|
|
* |
30
|
|
|
* @var Identifier[] |
31
|
|
|
*/ |
32
|
|
|
protected $_localColumnNames; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Table or asset identifier instance of the referenced table name the foreign key constraint is associated with. |
36
|
|
|
* |
37
|
|
|
* @var Table|Identifier |
38
|
|
|
*/ |
39
|
|
|
protected $_foreignTableName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Asset identifier instances of the referenced table column names the foreign key constraint is associated with. |
43
|
|
|
* array($columnName => Identifier) |
44
|
|
|
* |
45
|
|
|
* @var Identifier[] |
46
|
|
|
*/ |
47
|
|
|
protected $_foreignColumnNames; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Options associated with the foreign key constraint. |
51
|
|
|
* |
52
|
|
|
* @var mixed[] |
53
|
|
|
*/ |
54
|
|
|
protected $_options; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initializes the foreign key constraint. |
58
|
|
|
* |
59
|
|
|
* @param string[] $localColumnNames Names of the referencing table columns. |
60
|
|
|
* @param Table|string $foreignTableName Referenced table. |
61
|
|
|
* @param string[] $foreignColumnNames Names of the referenced table columns. |
62
|
|
|
* @param string|null $name Name of the foreign key constraint. |
63
|
|
|
* @param mixed[] $options Options associated with the foreign key constraint. |
64
|
|
|
*/ |
65
|
6179 |
|
public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name = null, array $options = []) |
66
|
|
|
{ |
67
|
6179 |
|
if ($name !== null) { |
68
|
6179 |
|
$this->_setName($name); |
69
|
|
|
} |
70
|
|
|
|
71
|
6179 |
|
$this->_localColumnNames = $this->createIdentifierMap($localColumnNames); |
72
|
|
|
|
73
|
6179 |
|
if ($foreignTableName instanceof Table) { |
74
|
6135 |
|
$this->_foreignTableName = $foreignTableName; |
75
|
|
|
} else { |
76
|
6095 |
|
$this->_foreignTableName = new Identifier($foreignTableName); |
77
|
|
|
} |
78
|
|
|
|
79
|
6179 |
|
$this->_foreignColumnNames = $this->createIdentifierMap($foreignColumnNames); |
80
|
6179 |
|
$this->_options = $options; |
81
|
6179 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string[] $names |
85
|
|
|
* |
86
|
|
|
* @return Identifier[] |
87
|
|
|
*/ |
88
|
6179 |
|
private function createIdentifierMap(array $names) : array |
89
|
|
|
{ |
90
|
6179 |
|
$identifiers = []; |
91
|
|
|
|
92
|
6179 |
|
foreach ($names as $name) { |
93
|
6179 |
|
$identifiers[$name] = new Identifier($name); |
94
|
|
|
} |
95
|
|
|
|
96
|
6179 |
|
return $identifiers; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the name of the referencing table |
101
|
|
|
* the foreign key constraint is associated with. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1050 |
|
public function getLocalTableName() |
106
|
|
|
{ |
107
|
1050 |
|
return $this->_localTable->getName(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the Table instance of the referencing table |
112
|
|
|
* the foreign key constraint is associated with. |
113
|
|
|
* |
114
|
|
|
* @param Table $table Instance of the referencing table. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
6178 |
|
public function setLocalTable(Table $table) |
119
|
|
|
{ |
120
|
6178 |
|
$this->_localTable = $table; |
121
|
6178 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return Table |
125
|
|
|
*/ |
126
|
575 |
|
public function getLocalTable() |
127
|
|
|
{ |
128
|
575 |
|
return $this->_localTable; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the names of the referencing table columns |
133
|
|
|
* the foreign key constraint is associated with. |
134
|
|
|
* |
135
|
|
|
* @return string[] |
136
|
|
|
*/ |
137
|
6178 |
|
public function getLocalColumns() |
138
|
|
|
{ |
139
|
6178 |
|
return array_keys($this->_localColumnNames); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the quoted representation of the referencing table column names |
144
|
|
|
* the foreign key constraint is associated with. |
145
|
|
|
* |
146
|
|
|
* But only if they were defined with one or the referencing table column name |
147
|
|
|
* is a keyword reserved by the platform. |
148
|
|
|
* Otherwise the plain unquoted value as inserted is returned. |
149
|
|
|
* |
150
|
|
|
* @param AbstractPlatform $platform The platform to use for quotation. |
151
|
|
|
* |
152
|
|
|
* @return string[] |
153
|
|
|
*/ |
154
|
6178 |
|
public function getQuotedLocalColumns(AbstractPlatform $platform) |
155
|
|
|
{ |
156
|
6178 |
|
$columns = []; |
157
|
|
|
|
158
|
6178 |
|
foreach ($this->_localColumnNames as $column) { |
159
|
6178 |
|
$columns[] = $column->getQuotedName($platform); |
160
|
|
|
} |
161
|
|
|
|
162
|
6178 |
|
return $columns; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns unquoted representation of local table column names for comparison with other FK |
167
|
|
|
* |
168
|
|
|
* @return string[] |
169
|
|
|
*/ |
170
|
5817 |
|
public function getUnquotedLocalColumns() |
171
|
|
|
{ |
172
|
5817 |
|
return array_map([$this, 'trimQuotes'], $this->getLocalColumns()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns unquoted representation of foreign table column names for comparison with other FK |
177
|
|
|
* |
178
|
|
|
* @return string[] |
179
|
|
|
*/ |
180
|
5800 |
|
public function getUnquotedForeignColumns() |
181
|
|
|
{ |
182
|
5800 |
|
return array_map([$this, 'trimQuotes'], $this->getForeignColumns()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
* |
188
|
|
|
* @see getLocalColumns |
189
|
|
|
*/ |
190
|
6178 |
|
public function getColumns() |
191
|
|
|
{ |
192
|
6178 |
|
return $this->getLocalColumns(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns the quoted representation of the referencing table column names |
197
|
|
|
* the foreign key constraint is associated with. |
198
|
|
|
* |
199
|
|
|
* But only if they were defined with one or the referencing table column name |
200
|
|
|
* is a keyword reserved by the platform. |
201
|
|
|
* Otherwise the plain unquoted value as inserted is returned. |
202
|
|
|
* |
203
|
|
|
* @see getQuotedLocalColumns |
204
|
|
|
* |
205
|
|
|
* @param AbstractPlatform $platform The platform to use for quotation. |
206
|
|
|
* |
207
|
|
|
* @return string[] |
208
|
|
|
*/ |
209
|
5800 |
|
public function getQuotedColumns(AbstractPlatform $platform) |
210
|
|
|
{ |
211
|
5800 |
|
return $this->getQuotedLocalColumns($platform); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns the name of the referenced table |
216
|
|
|
* the foreign key constraint is associated with. |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
6178 |
|
public function getForeignTableName() |
221
|
|
|
{ |
222
|
6178 |
|
return $this->_foreignTableName->getName(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns the non-schema qualified foreign table name. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
5800 |
|
public function getUnqualifiedForeignTableName() |
231
|
|
|
{ |
232
|
5800 |
|
$name = $this->_foreignTableName->getName(); |
233
|
5800 |
|
$position = strrpos($name, '.'); |
234
|
|
|
|
235
|
5800 |
|
if ($position !== false) { |
236
|
1075 |
|
$name = substr($name, $position); |
237
|
|
|
} |
238
|
|
|
|
239
|
5800 |
|
return strtolower($name); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Returns the quoted representation of the referenced table name |
244
|
|
|
* the foreign key constraint is associated with. |
245
|
|
|
* |
246
|
|
|
* But only if it was defined with one or the referenced table name |
247
|
|
|
* is a keyword reserved by the platform. |
248
|
|
|
* Otherwise the plain unquoted value as inserted is returned. |
249
|
|
|
* |
250
|
|
|
* @param AbstractPlatform $platform The platform to use for quotation. |
251
|
|
|
* |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
6178 |
|
public function getQuotedForeignTableName(AbstractPlatform $platform) |
255
|
|
|
{ |
256
|
6178 |
|
return $this->_foreignTableName->getQuotedName($platform); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns the names of the referenced table columns |
261
|
|
|
* the foreign key constraint is associated with. |
262
|
|
|
* |
263
|
|
|
* @return string[] |
264
|
|
|
*/ |
265
|
6178 |
|
public function getForeignColumns() |
266
|
|
|
{ |
267
|
6178 |
|
return array_keys($this->_foreignColumnNames); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns the quoted representation of the referenced table column names |
272
|
|
|
* the foreign key constraint is associated with. |
273
|
|
|
* |
274
|
|
|
* But only if they were defined with one or the referenced table column name |
275
|
|
|
* is a keyword reserved by the platform. |
276
|
|
|
* Otherwise the plain unquoted value as inserted is returned. |
277
|
|
|
* |
278
|
|
|
* @param AbstractPlatform $platform The platform to use for quotation. |
279
|
|
|
* |
280
|
|
|
* @return string[] |
281
|
|
|
*/ |
282
|
6178 |
|
public function getQuotedForeignColumns(AbstractPlatform $platform) |
283
|
|
|
{ |
284
|
6178 |
|
$columns = []; |
285
|
|
|
|
286
|
6178 |
|
foreach ($this->_foreignColumnNames as $column) { |
287
|
6178 |
|
$columns[] = $column->getQuotedName($platform); |
288
|
|
|
} |
289
|
|
|
|
290
|
6178 |
|
return $columns; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Returns whether or not a given option |
295
|
|
|
* is associated with the foreign key constraint. |
296
|
|
|
* |
297
|
|
|
* @param string $name Name of the option to check. |
298
|
|
|
* |
299
|
|
|
* @return bool |
300
|
|
|
*/ |
301
|
6178 |
|
public function hasOption($name) |
302
|
|
|
{ |
303
|
6178 |
|
return isset($this->_options[$name]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns an option associated with the foreign key constraint. |
308
|
|
|
* |
309
|
|
|
* @param string $name Name of the option the foreign key constraint is associated with. |
310
|
|
|
* |
311
|
|
|
* @return mixed |
312
|
|
|
*/ |
313
|
6022 |
|
public function getOption($name) |
314
|
|
|
{ |
315
|
6022 |
|
return $this->_options[$name]; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Returns the options associated with the foreign key constraint. |
320
|
|
|
* |
321
|
|
|
* @return mixed[] |
322
|
|
|
*/ |
323
|
1579 |
|
public function getOptions() |
324
|
|
|
{ |
325
|
1579 |
|
return $this->_options; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Returns the referential action for UPDATE operations |
330
|
|
|
* on the referenced table the foreign key constraint is associated with. |
331
|
|
|
* |
332
|
|
|
* @return string|null |
333
|
|
|
*/ |
334
|
5800 |
|
public function onUpdate() |
335
|
|
|
{ |
336
|
5800 |
|
return $this->onEvent('onUpdate'); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns the referential action for DELETE operations |
341
|
|
|
* on the referenced table the foreign key constraint is associated with. |
342
|
|
|
* |
343
|
|
|
* @return string|null |
344
|
|
|
*/ |
345
|
5798 |
|
public function onDelete() |
346
|
|
|
{ |
347
|
5798 |
|
return $this->onEvent('onDelete'); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Returns the referential action for a given database operation |
352
|
|
|
* on the referenced table the foreign key constraint is associated with. |
353
|
|
|
* |
354
|
|
|
* @param string $event Name of the database operation/event to return the referential action for. |
355
|
|
|
* |
356
|
|
|
* @return string|null |
357
|
|
|
*/ |
358
|
5800 |
|
private function onEvent($event) |
359
|
|
|
{ |
360
|
5800 |
|
if (isset($this->_options[$event])) { |
361
|
1175 |
|
$onEvent = strtoupper($this->_options[$event]); |
362
|
|
|
|
363
|
1175 |
|
if (! in_array($onEvent, ['NO ACTION', 'RESTRICT'])) { |
364
|
1175 |
|
return $onEvent; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
368
|
5800 |
|
return false; |
|
|
|
|
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Checks whether this foreign key constraint intersects the given index columns. |
373
|
|
|
* |
374
|
|
|
* Returns `true` if at least one of this foreign key's local columns |
375
|
|
|
* matches one of the given index's columns, `false` otherwise. |
376
|
|
|
* |
377
|
|
|
* @param Index $index The index to be checked against. |
378
|
|
|
* |
379
|
|
|
* @return bool |
380
|
|
|
*/ |
381
|
5610 |
|
public function intersectsIndexColumns(Index $index) |
382
|
|
|
{ |
383
|
5610 |
|
foreach ($index->getColumns() as $indexColumn) { |
384
|
5610 |
|
foreach ($this->_localColumnNames as $localColumn) { |
385
|
5610 |
|
if (strtolower($indexColumn) === strtolower($localColumn->getName())) { |
386
|
5610 |
|
return true; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
5400 |
|
return false; |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|