1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\DBAL\Schema; |
---|
21 | |
---|
22 | use \Doctrine\DBAL\Types\Type; |
---|
23 | use Doctrine\DBAL\Schema\Visitor\Visitor; |
---|
24 | |
---|
25 | /** |
---|
26 | * Object representation of a database column |
---|
27 | * |
---|
28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
29 | * @link www.doctrine-project.org |
---|
30 | * @since 2.0 |
---|
31 | * @version $Revision$ |
---|
32 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
33 | */ |
---|
34 | class Column extends AbstractAsset |
---|
35 | { |
---|
36 | /** |
---|
37 | * @var \Doctrine\DBAL\Types\Type |
---|
38 | */ |
---|
39 | protected $_type; |
---|
40 | |
---|
41 | /** |
---|
42 | * @var int |
---|
43 | */ |
---|
44 | protected $_length = null; |
---|
45 | |
---|
46 | /** |
---|
47 | * @var int |
---|
48 | */ |
---|
49 | protected $_precision = 10; |
---|
50 | |
---|
51 | /** |
---|
52 | * @var int |
---|
53 | */ |
---|
54 | protected $_scale = 0; |
---|
55 | |
---|
56 | /** |
---|
57 | * @var bool |
---|
58 | */ |
---|
59 | protected $_unsigned = false; |
---|
60 | |
---|
61 | /** |
---|
62 | * @var bool |
---|
63 | */ |
---|
64 | protected $_fixed = false; |
---|
65 | |
---|
66 | /** |
---|
67 | * @var bool |
---|
68 | */ |
---|
69 | protected $_notnull = true; |
---|
70 | |
---|
71 | /** |
---|
72 | * @var string |
---|
73 | */ |
---|
74 | protected $_default = null; |
---|
75 | |
---|
76 | /** |
---|
77 | * @var bool |
---|
78 | */ |
---|
79 | protected $_autoincrement = false; |
---|
80 | |
---|
81 | /** |
---|
82 | * @var array |
---|
83 | */ |
---|
84 | protected $_platformOptions = array(); |
---|
85 | |
---|
86 | /** |
---|
87 | * @var string |
---|
88 | */ |
---|
89 | protected $_columnDefinition = null; |
---|
90 | |
---|
91 | /** |
---|
92 | * @var string |
---|
93 | */ |
---|
94 | protected $_comment = null; |
---|
95 | |
---|
96 | /** |
---|
97 | * @var array |
---|
98 | */ |
---|
99 | protected $_customSchemaOptions = array(); |
---|
100 | |
---|
101 | /** |
---|
102 | * Create a new Column |
---|
103 | * |
---|
104 | * @param string $columnName |
---|
105 | * @param Doctrine\DBAL\Types\Type $type |
---|
106 | * @param int $length |
---|
107 | * @param bool $notNull |
---|
108 | * @param mixed $default |
---|
109 | * @param bool $unsigned |
---|
110 | * @param bool $fixed |
---|
111 | * @param int $precision |
---|
112 | * @param int $scale |
---|
113 | * @param array $platformOptions |
---|
114 | */ |
---|
115 | public function __construct($columnName, Type $type, array $options=array()) |
---|
116 | { |
---|
117 | $this->_setName($columnName); |
---|
118 | $this->setType($type); |
---|
119 | $this->setOptions($options); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * @param array $options |
---|
124 | * @return Column |
---|
125 | */ |
---|
126 | public function setOptions(array $options) |
---|
127 | { |
---|
128 | foreach ($options AS $name => $value) { |
---|
129 | $method = "set".$name; |
---|
130 | if (method_exists($this, $method)) { |
---|
131 | $this->$method($value); |
---|
132 | } |
---|
133 | } |
---|
134 | return $this; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * @param Type $type |
---|
139 | * @return Column |
---|
140 | */ |
---|
141 | public function setType(Type $type) |
---|
142 | { |
---|
143 | $this->_type = $type; |
---|
144 | return $this; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * @param int $length |
---|
149 | * @return Column |
---|
150 | */ |
---|
151 | public function setLength($length) |
---|
152 | { |
---|
153 | if($length !== null) { |
---|
154 | $this->_length = (int)$length; |
---|
155 | } else { |
---|
156 | $this->_length = null; |
---|
157 | } |
---|
158 | return $this; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * @param int $precision |
---|
163 | * @return Column |
---|
164 | */ |
---|
165 | public function setPrecision($precision) |
---|
166 | { |
---|
167 | if (!is_numeric($precision)) { |
---|
168 | $precision = 10; // defaults to 10 when no valid precision is given. |
---|
169 | } |
---|
170 | |
---|
171 | $this->_precision = (int)$precision; |
---|
172 | return $this; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * @param int $scale |
---|
177 | * @return Column |
---|
178 | */ |
---|
179 | public function setScale($scale) |
---|
180 | { |
---|
181 | if (!is_numeric($scale)) { |
---|
182 | $scale = 0; |
---|
183 | } |
---|
184 | |
---|
185 | $this->_scale = (int)$scale; |
---|
186 | return $this; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * |
---|
191 | * @param bool $unsigned |
---|
192 | * @return Column |
---|
193 | */ |
---|
194 | public function setUnsigned($unsigned) |
---|
195 | { |
---|
196 | $this->_unsigned = (bool)$unsigned; |
---|
197 | return $this; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * |
---|
202 | * @param bool $fixed |
---|
203 | * @return Column |
---|
204 | */ |
---|
205 | public function setFixed($fixed) |
---|
206 | { |
---|
207 | $this->_fixed = (bool)$fixed; |
---|
208 | return $this; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * @param bool $notnull |
---|
213 | * @return Column |
---|
214 | */ |
---|
215 | public function setNotnull($notnull) |
---|
216 | { |
---|
217 | $this->_notnull = (bool)$notnull; |
---|
218 | return $this; |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * |
---|
223 | * @param mixed $default |
---|
224 | * @return Column |
---|
225 | */ |
---|
226 | public function setDefault($default) |
---|
227 | { |
---|
228 | $this->_default = $default; |
---|
229 | return $this; |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * |
---|
234 | * @param array $platformOptions |
---|
235 | * @return Column |
---|
236 | */ |
---|
237 | public function setPlatformOptions(array $platformOptions) |
---|
238 | { |
---|
239 | $this->_platformOptions = $platformOptions; |
---|
240 | return $this; |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * |
---|
245 | * @param string $name |
---|
246 | * @param mixed $value |
---|
247 | * @return Column |
---|
248 | */ |
---|
249 | public function setPlatformOption($name, $value) |
---|
250 | { |
---|
251 | $this->_platformOptions[$name] = $value; |
---|
252 | return $this; |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * |
---|
257 | * @param string |
---|
258 | * @return Column |
---|
259 | */ |
---|
260 | public function setColumnDefinition($value) |
---|
261 | { |
---|
262 | $this->_columnDefinition = $value; |
---|
263 | return $this; |
---|
264 | } |
---|
265 | |
---|
266 | public function getType() |
---|
267 | { |
---|
268 | return $this->_type; |
---|
269 | } |
---|
270 | |
---|
271 | public function getLength() |
---|
272 | { |
---|
273 | return $this->_length; |
---|
274 | } |
---|
275 | |
---|
276 | public function getPrecision() |
---|
277 | { |
---|
278 | return $this->_precision; |
---|
279 | } |
---|
280 | |
---|
281 | public function getScale() |
---|
282 | { |
---|
283 | return $this->_scale; |
---|
284 | } |
---|
285 | |
---|
286 | public function getUnsigned() |
---|
287 | { |
---|
288 | return $this->_unsigned; |
---|
289 | } |
---|
290 | |
---|
291 | public function getFixed() |
---|
292 | { |
---|
293 | return $this->_fixed; |
---|
294 | } |
---|
295 | |
---|
296 | public function getNotnull() |
---|
297 | { |
---|
298 | return $this->_notnull; |
---|
299 | } |
---|
300 | |
---|
301 | public function getDefault() |
---|
302 | { |
---|
303 | return $this->_default; |
---|
304 | } |
---|
305 | |
---|
306 | public function getPlatformOptions() |
---|
307 | { |
---|
308 | return $this->_platformOptions; |
---|
309 | } |
---|
310 | |
---|
311 | public function hasPlatformOption($name) |
---|
312 | { |
---|
313 | return isset($this->_platformOptions[$name]); |
---|
314 | } |
---|
315 | |
---|
316 | public function getPlatformOption($name) |
---|
317 | { |
---|
318 | return $this->_platformOptions[$name]; |
---|
319 | } |
---|
320 | |
---|
321 | public function getColumnDefinition() |
---|
322 | { |
---|
323 | return $this->_columnDefinition; |
---|
324 | } |
---|
325 | |
---|
326 | public function getAutoincrement() |
---|
327 | { |
---|
328 | return $this->_autoincrement; |
---|
329 | } |
---|
330 | |
---|
331 | public function setAutoincrement($flag) |
---|
332 | { |
---|
333 | $this->_autoincrement = $flag; |
---|
334 | return $this; |
---|
335 | } |
---|
336 | |
---|
337 | public function setComment($comment) |
---|
338 | { |
---|
339 | $this->_comment = $comment; |
---|
340 | return $this; |
---|
341 | } |
---|
342 | |
---|
343 | public function getComment() |
---|
344 | { |
---|
345 | return $this->_comment; |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * @param string $name |
---|
350 | * @param mixed $value |
---|
351 | * @return Column |
---|
352 | */ |
---|
353 | public function setCustomSchemaOption($name, $value) |
---|
354 | { |
---|
355 | $this->_customSchemaOptions[$name] = $value; |
---|
356 | return $this; |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | * @param string $name |
---|
361 | * @return boolean |
---|
362 | */ |
---|
363 | public function hasCustomSchemaOption($name) |
---|
364 | { |
---|
365 | return isset($this->_customSchemaOptions[$name]); |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * @param string $name |
---|
370 | * @return mixed |
---|
371 | */ |
---|
372 | public function getCustomSchemaOption($name) |
---|
373 | { |
---|
374 | return $this->_customSchemaOptions[$name]; |
---|
375 | } |
---|
376 | |
---|
377 | /** |
---|
378 | * @param array $customSchemaOptions |
---|
379 | * @return Column |
---|
380 | */ |
---|
381 | public function setCustomSchemaOptions(array $customSchemaOptions) |
---|
382 | { |
---|
383 | $this->_customSchemaOptions = $customSchemaOptions; |
---|
384 | return $this; |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | * @return array |
---|
389 | */ |
---|
390 | public function getCustomSchemaOptions() |
---|
391 | { |
---|
392 | return $this->_customSchemaOptions; |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * @param Visitor $visitor |
---|
397 | */ |
---|
398 | public function visit(\Doctrine\DBAL\Schema\Visitor $visitor) |
---|
399 | { |
---|
400 | $visitor->accept($this); |
---|
401 | } |
---|
402 | |
---|
403 | /** |
---|
404 | * @return array |
---|
405 | */ |
---|
406 | public function toArray() |
---|
407 | { |
---|
408 | return array_merge(array( |
---|
409 | 'name' => $this->_name, |
---|
410 | 'type' => $this->_type, |
---|
411 | 'default' => $this->_default, |
---|
412 | 'notnull' => $this->_notnull, |
---|
413 | 'length' => $this->_length, |
---|
414 | 'precision' => $this->_precision, |
---|
415 | 'scale' => $this->_scale, |
---|
416 | 'fixed' => $this->_fixed, |
---|
417 | 'unsigned' => $this->_unsigned, |
---|
418 | 'autoincrement' => $this->_autoincrement, |
---|
419 | 'columnDefinition' => $this->_columnDefinition, |
---|
420 | 'comment' => $this->_comment, |
---|
421 | ), $this->_platformOptions, $this->_customSchemaOptions); |
---|
422 | } |
---|
423 | } |
---|