1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use function array_keys; |
8
|
|
|
use function array_map; |
9
|
|
|
use function is_string; |
10
|
|
|
use function strtolower; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class for a unique constraint. |
14
|
|
|
*/ |
15
|
|
|
class UniqueConstraint extends AbstractAsset implements Constraint |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Asset identifier instances of the column names the unique constraint is associated with. |
19
|
|
|
* array($columnName => Identifier) |
20
|
|
|
* |
21
|
|
|
* @var Identifier[] |
22
|
|
|
*/ |
23
|
|
|
protected $columns = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Platform specific flags |
27
|
|
|
* array($flagName => true) |
28
|
|
|
* |
29
|
|
|
* @var true[] |
30
|
|
|
*/ |
31
|
|
|
protected $flags = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Platform specific options |
35
|
|
|
* |
36
|
|
|
* @var mixed[] |
37
|
|
|
*/ |
38
|
|
|
private $options = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $indexName |
42
|
|
|
* @param string[] $columns |
43
|
|
|
* @param string[] $flags |
44
|
|
|
* @param mixed[] $options |
45
|
|
|
*/ |
46
|
598 |
|
public function __construct($indexName, array $columns, array $flags = [], array $options = []) |
47
|
|
|
{ |
48
|
598 |
|
$this->_setName($indexName); |
49
|
|
|
|
50
|
598 |
|
$this->options = $options; |
51
|
|
|
|
52
|
598 |
|
foreach ($columns as $column) { |
53
|
575 |
|
$this->_addColumn($column); |
54
|
|
|
} |
55
|
|
|
|
56
|
598 |
|
foreach ($flags as $flag) { |
57
|
23 |
|
$this->addFlag($flag); |
58
|
|
|
} |
59
|
598 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
598 |
|
public function getColumns() |
65
|
|
|
{ |
66
|
598 |
|
return array_keys($this->columns); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getQuotedColumns(AbstractPlatform $platform) |
73
|
|
|
{ |
74
|
|
|
$columns = []; |
75
|
|
|
|
76
|
|
|
foreach ($this->columns as $column) { |
77
|
|
|
$columns[] = $column->getQuotedName($platform); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $columns; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string[] |
85
|
|
|
*/ |
86
|
|
|
public function getUnquotedColumns() |
87
|
|
|
{ |
88
|
|
|
return array_map([$this, 'trimQuotes'], $this->getColumns()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns platform specific flags for unique constraint. |
93
|
|
|
* |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
|
|
public function getFlags() |
97
|
|
|
{ |
98
|
|
|
return array_keys($this->flags); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Adds flag for a unique constraint that translates to platform specific handling. |
103
|
|
|
* |
104
|
|
|
* @param string $flag |
105
|
|
|
* |
106
|
|
|
* @return self |
107
|
|
|
* |
108
|
|
|
* @example $uniqueConstraint->addFlag('CLUSTERED') |
109
|
|
|
*/ |
110
|
23 |
|
public function addFlag($flag) |
111
|
|
|
{ |
112
|
23 |
|
$this->flags[strtolower($flag)] = true; |
113
|
|
|
|
114
|
23 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Does this unique constraint have a specific flag? |
119
|
|
|
* |
120
|
|
|
* @param string $flag |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
575 |
|
public function hasFlag($flag) |
125
|
|
|
{ |
126
|
575 |
|
return isset($this->flags[strtolower($flag)]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Removes a flag. |
131
|
|
|
* |
132
|
|
|
* @param string $flag |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function removeFlag($flag) |
137
|
|
|
{ |
138
|
|
|
unset($this->flags[strtolower($flag)]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $name |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function hasOption($name) |
147
|
|
|
{ |
148
|
|
|
return isset($this->options[strtolower($name)]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $name |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public function getOption($name) |
157
|
|
|
{ |
158
|
|
|
return $this->options[strtolower($name)]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return mixed[] |
163
|
|
|
*/ |
164
|
|
|
public function getOptions() |
165
|
|
|
{ |
166
|
|
|
return $this->options; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $column |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
* |
174
|
|
|
* @throws InvalidArgumentException |
175
|
|
|
*/ |
176
|
575 |
|
protected function _addColumn($column) |
177
|
|
|
{ |
178
|
575 |
|
if (! is_string($column)) { |
|
|
|
|
179
|
|
|
throw new InvalidArgumentException('Expecting a string as Index Column'); |
180
|
|
|
} |
181
|
|
|
|
182
|
575 |
|
$this->columns[$column] = new Identifier($column); |
183
|
575 |
|
} |
184
|
|
|
} |
185
|
|
|
|