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 MIT license. 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
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Object representation of a database column. |
26
|
|
|
* |
27
|
|
|
* @link www.doctrine-project.org |
28
|
|
|
* @since 2.0 |
29
|
|
|
* @author Benjamin Eberlei <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Column extends AbstractAsset |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Type |
35
|
|
|
*/ |
36
|
|
|
protected $_type; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var integer|null |
40
|
|
|
*/ |
41
|
|
|
protected $_length = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var integer |
45
|
|
|
*/ |
46
|
|
|
protected $_precision = 10; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
protected $_scale = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var boolean |
55
|
|
|
*/ |
56
|
|
|
protected $_unsigned = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
protected $_fixed = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var boolean |
65
|
|
|
*/ |
66
|
|
|
protected $_notnull = true; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string|null |
70
|
|
|
*/ |
71
|
|
|
protected $_default = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var boolean |
75
|
|
|
*/ |
76
|
|
|
protected $_autoincrement = false; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
protected $_platformOptions = []; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var string|null |
85
|
|
|
*/ |
86
|
|
|
protected $_columnDefinition = null; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string|null |
90
|
|
|
*/ |
91
|
|
|
protected $_comment = null; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
protected $_customSchemaOptions = []; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Creates a new Column. |
100
|
|
|
* |
101
|
|
|
* @param string $columnName |
102
|
|
|
* @param Type $type |
103
|
|
|
* @param array $options |
104
|
|
|
*/ |
105
|
757 |
|
public function __construct($columnName, Type $type, array $options=[]) |
106
|
|
|
{ |
107
|
757 |
|
$this->_setName($columnName); |
108
|
757 |
|
$this->setType($type); |
109
|
757 |
|
$this->setOptions($options); |
110
|
757 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $options |
114
|
|
|
* |
115
|
|
|
* @return Column |
116
|
|
|
*/ |
117
|
757 |
|
public function setOptions(array $options) |
118
|
|
|
{ |
119
|
757 |
|
foreach ($options as $name => $value) { |
120
|
390 |
|
$method = "set".$name; |
121
|
390 |
|
if ( ! method_exists($this, $method)) { |
122
|
|
|
// next major: throw an exception |
123
|
1 |
|
@trigger_error(sprintf( |
124
|
|
|
'The "%s" option is not supported,'. |
125
|
1 |
|
' setting it is deprecated and will cause an error in 3.0', |
126
|
1 |
|
$name |
127
|
1 |
|
), E_USER_DEPRECATED); |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
389 |
|
$this->$method($value); |
132
|
|
|
} |
133
|
|
|
|
134
|
756 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Type $type |
139
|
|
|
* |
140
|
|
|
* @return Column |
141
|
|
|
*/ |
142
|
757 |
|
public function setType(Type $type) |
143
|
|
|
{ |
144
|
757 |
|
$this->_type = $type; |
145
|
|
|
|
146
|
757 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param integer|null $length |
151
|
|
|
* |
152
|
|
|
* @return Column |
153
|
|
|
*/ |
154
|
147 |
|
public function setLength($length) |
155
|
|
|
{ |
156
|
147 |
|
if ($length !== null) { |
157
|
118 |
|
$this->_length = (int) $length; |
158
|
|
|
} else { |
159
|
39 |
|
$this->_length = null; |
160
|
|
|
} |
161
|
|
|
|
162
|
147 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param integer $precision |
167
|
|
|
* |
168
|
|
|
* @return Column |
169
|
|
|
*/ |
170
|
63 |
|
public function setPrecision($precision) |
171
|
|
|
{ |
172
|
63 |
|
if (!is_numeric($precision)) { |
173
|
41 |
|
$precision = 10; // defaults to 10 when no valid precision is given. |
174
|
|
|
} |
175
|
|
|
|
176
|
63 |
|
$this->_precision = (int) $precision; |
177
|
|
|
|
178
|
63 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param integer $scale |
183
|
|
|
* |
184
|
|
|
* @return Column |
185
|
|
|
*/ |
186
|
62 |
|
public function setScale($scale) |
187
|
|
|
{ |
188
|
62 |
|
if (!is_numeric($scale)) { |
189
|
40 |
|
$scale = 0; |
190
|
|
|
} |
191
|
|
|
|
192
|
62 |
|
$this->_scale = (int) $scale; |
193
|
|
|
|
194
|
62 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param boolean $unsigned |
199
|
|
|
* |
200
|
|
|
* @return Column |
201
|
|
|
*/ |
202
|
43 |
|
public function setUnsigned($unsigned) |
203
|
|
|
{ |
204
|
43 |
|
$this->_unsigned = (bool) $unsigned; |
205
|
|
|
|
206
|
43 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param boolean $fixed |
211
|
|
|
* |
212
|
|
|
* @return Column |
213
|
|
|
*/ |
214
|
75 |
|
public function setFixed($fixed) |
215
|
|
|
{ |
216
|
75 |
|
$this->_fixed = (bool) $fixed; |
217
|
|
|
|
218
|
75 |
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param boolean $notnull |
223
|
|
|
* |
224
|
|
|
* @return Column |
225
|
|
|
*/ |
226
|
461 |
|
public function setNotnull($notnull) |
227
|
|
|
{ |
228
|
461 |
|
$this->_notnull = (bool) $notnull; |
229
|
|
|
|
230
|
461 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param mixed $default |
235
|
|
|
* |
236
|
|
|
* @return Column |
237
|
|
|
*/ |
238
|
87 |
|
public function setDefault($default) |
239
|
|
|
{ |
240
|
87 |
|
$this->_default = $default; |
241
|
|
|
|
242
|
87 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param array $platformOptions |
247
|
|
|
* |
248
|
|
|
* @return Column |
249
|
|
|
*/ |
250
|
9 |
|
public function setPlatformOptions(array $platformOptions) |
251
|
|
|
{ |
252
|
9 |
|
$this->_platformOptions = $platformOptions; |
253
|
|
|
|
254
|
9 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $name |
259
|
|
|
* @param mixed $value |
260
|
|
|
* |
261
|
|
|
* @return Column |
262
|
|
|
*/ |
263
|
17 |
|
public function setPlatformOption($name, $value) |
264
|
|
|
{ |
265
|
17 |
|
$this->_platformOptions[$name] = $value; |
266
|
|
|
|
267
|
17 |
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $value |
272
|
|
|
* |
273
|
|
|
* @return Column |
274
|
|
|
*/ |
275
|
1 |
|
public function setColumnDefinition($value) |
276
|
|
|
{ |
277
|
1 |
|
$this->_columnDefinition = $value; |
278
|
|
|
|
279
|
1 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return Type |
284
|
|
|
*/ |
285
|
516 |
|
public function getType() |
286
|
|
|
{ |
287
|
516 |
|
return $this->_type; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return integer|null |
292
|
|
|
*/ |
293
|
4 |
|
public function getLength() |
294
|
|
|
{ |
295
|
4 |
|
return $this->_length; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return integer |
300
|
|
|
*/ |
301
|
2 |
|
public function getPrecision() |
302
|
|
|
{ |
303
|
2 |
|
return $this->_precision; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return integer |
308
|
|
|
*/ |
309
|
2 |
|
public function getScale() |
310
|
|
|
{ |
311
|
2 |
|
return $this->_scale; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return boolean |
316
|
|
|
*/ |
317
|
3 |
|
public function getUnsigned() |
318
|
|
|
{ |
319
|
3 |
|
return $this->_unsigned; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return boolean |
324
|
|
|
*/ |
325
|
4 |
|
public function getFixed() |
326
|
|
|
{ |
327
|
4 |
|
return $this->_fixed; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return boolean |
332
|
|
|
*/ |
333
|
11 |
|
public function getNotnull() |
334
|
|
|
{ |
335
|
11 |
|
return $this->_notnull; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @return string|null |
340
|
|
|
*/ |
341
|
46 |
|
public function getDefault() |
342
|
|
|
{ |
343
|
46 |
|
return $this->_default; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @return array |
348
|
|
|
*/ |
349
|
142 |
|
public function getPlatformOptions() |
350
|
|
|
{ |
351
|
142 |
|
return $this->_platformOptions; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @param string $name |
356
|
|
|
* |
357
|
|
|
* @return boolean |
358
|
|
|
*/ |
359
|
366 |
|
public function hasPlatformOption($name) |
360
|
|
|
{ |
361
|
366 |
|
return isset($this->_platformOptions[$name]); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param string $name |
366
|
|
|
* |
367
|
|
|
* @return mixed |
368
|
|
|
*/ |
369
|
2 |
|
public function getPlatformOption($name) |
370
|
|
|
{ |
371
|
2 |
|
return $this->_platformOptions[$name]; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return string|null |
376
|
|
|
*/ |
377
|
|
|
public function getColumnDefinition() |
378
|
|
|
{ |
379
|
|
|
return $this->_columnDefinition; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @return boolean |
384
|
|
|
*/ |
385
|
41 |
|
public function getAutoincrement() |
386
|
|
|
{ |
387
|
41 |
|
return $this->_autoincrement; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @param boolean $flag |
392
|
|
|
* |
393
|
|
|
* @return Column |
394
|
|
|
*/ |
395
|
143 |
|
public function setAutoincrement($flag) |
396
|
|
|
{ |
397
|
143 |
|
$this->_autoincrement = $flag; |
398
|
|
|
|
399
|
143 |
|
return $this; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @param string $comment |
404
|
|
|
* |
405
|
|
|
* @return Column |
406
|
|
|
*/ |
407
|
128 |
|
public function setComment($comment) |
408
|
|
|
{ |
409
|
128 |
|
$this->_comment = $comment; |
410
|
|
|
|
411
|
128 |
|
return $this; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @return string|null |
416
|
|
|
*/ |
417
|
488 |
|
public function getComment() |
418
|
|
|
{ |
419
|
488 |
|
return $this->_comment; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param string $name |
424
|
|
|
* @param mixed $value |
425
|
|
|
* |
426
|
|
|
* @return Column |
427
|
|
|
*/ |
428
|
1 |
|
public function setCustomSchemaOption($name, $value) |
429
|
|
|
{ |
430
|
1 |
|
$this->_customSchemaOptions[$name] = $value; |
431
|
|
|
|
432
|
1 |
|
return $this; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @param string $name |
437
|
|
|
* |
438
|
|
|
* @return boolean |
439
|
|
|
*/ |
440
|
1 |
|
public function hasCustomSchemaOption($name) |
441
|
|
|
{ |
442
|
1 |
|
return isset($this->_customSchemaOptions[$name]); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* @param string $name |
447
|
|
|
* |
448
|
|
|
* @return mixed |
449
|
|
|
*/ |
450
|
1 |
|
public function getCustomSchemaOption($name) |
451
|
|
|
{ |
452
|
1 |
|
return $this->_customSchemaOptions[$name]; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @param array $customSchemaOptions |
457
|
|
|
* |
458
|
|
|
* @return Column |
459
|
|
|
*/ |
460
|
3 |
|
public function setCustomSchemaOptions(array $customSchemaOptions) |
461
|
|
|
{ |
462
|
3 |
|
$this->_customSchemaOptions = $customSchemaOptions; |
463
|
|
|
|
464
|
3 |
|
return $this; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @return array |
469
|
|
|
*/ |
470
|
140 |
|
public function getCustomSchemaOptions() |
471
|
|
|
{ |
472
|
140 |
|
return $this->_customSchemaOptions; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @return array |
477
|
|
|
*/ |
478
|
622 |
|
public function toArray() |
479
|
|
|
{ |
480
|
622 |
|
return array_merge([ |
481
|
622 |
|
'name' => $this->_name, |
482
|
622 |
|
'type' => $this->_type, |
483
|
622 |
|
'default' => $this->_default, |
484
|
622 |
|
'notnull' => $this->_notnull, |
485
|
622 |
|
'length' => $this->_length, |
486
|
622 |
|
'precision' => $this->_precision, |
487
|
622 |
|
'scale' => $this->_scale, |
488
|
622 |
|
'fixed' => $this->_fixed, |
489
|
622 |
|
'unsigned' => $this->_unsigned, |
490
|
622 |
|
'autoincrement' => $this->_autoincrement, |
491
|
622 |
|
'columnDefinition' => $this->_columnDefinition, |
492
|
622 |
|
'comment' => $this->_comment, |
493
|
622 |
|
], $this->_platformOptions, $this->_customSchemaOptions); |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
|