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