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