1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use function array_keys;
use function array_map;
use function strtolower;
/**
* Class for a unique constraint.
*/
class UniqueConstraint extends AbstractAsset
{
/**
* Asset identifier instances of the column names the unique constraint is associated with.
*
* @var array<string, Identifier>
*/
protected array $columns = [];
/**
* Platform specific flags
*
* @var array<string, true>
*/
protected array $flags = [];
/**
* @param array<string> $columns
* @param array<string> $flags
* @param array<string, mixed> $options
*/
public function __construct(
string $name,
array $columns,
array $flags = [],
private readonly array $options = [],
) {
$this->_setName($name);
foreach ($columns as $column) {
$this->addColumn($column);
}
foreach ($flags as $flag) {
$this->addFlag($flag);
}
}
/**
* Returns the names of the referencing table columns the constraint is associated with.
*
* @return list<string>
*/
public function getColumns(): array
{
return array_keys($this->columns);
}
/**
* Returns the quoted representation of the column names the constraint is associated with.
*
* But only if they were defined with one or a column name
* is a keyword reserved by the platform.
* Otherwise, the plain unquoted value as inserted is returned.
*
* @param AbstractPlatform $platform The platform to use for quotation.
*
* @return list<string>
*/
public function getQuotedColumns(AbstractPlatform $platform): array
{
$columns = [];
foreach ($this->columns as $column) {
$columns[] = $column->getQuotedName($platform);
}
return $columns;
}
/** @return array<int, string> */
public function getUnquotedColumns(): array
{
return array_map($this->trimQuotes(...), $this->getColumns());
}
/**
* Returns platform specific flags for unique constraint.
*
* @return array<int, string>
*/
public function getFlags(): array
{
return array_keys($this->flags);
}
/**
* Adds flag for a unique constraint that translates to platform specific handling.
*
* @return $this
*
* @example $uniqueConstraint->addFlag('CLUSTERED')
*/
public function addFlag(string $flag): self
{
$this->flags[strtolower($flag)] = true;
return $this;
}
/**
* Does this unique constraint have a specific flag?
*/
public function hasFlag(string $flag): bool
{
return isset($this->flags[strtolower($flag)]);
}
/**
* Removes a flag.
*/
public function removeFlag(string $flag): void
{
unset($this->flags[strtolower($flag)]);
}
public function hasOption(string $name): bool
{
return isset($this->options[strtolower($name)]);
}
public function getOption(string $name): mixed
{
return $this->options[strtolower($name)];
}
/** @return array<string, mixed> */
public function getOptions(): array
{
return $this->options;
}
protected function addColumn(string $column): void
{
$this->columns[$column] = new Identifier($column);
}
}
|