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