|
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
|
23981 |
|
public function __construct($columnName, Type $type, array $options = []) |
|
68
|
|
|
{ |
|
69
|
23981 |
|
$this->_setName($columnName); |
|
70
|
23981 |
|
$this->setType($type); |
|
71
|
23981 |
|
$this->setOptions($options); |
|
72
|
23981 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param mixed[] $options |
|
76
|
|
|
* |
|
77
|
|
|
* @return Column |
|
78
|
|
|
*/ |
|
79
|
23981 |
|
public function setOptions(array $options) |
|
80
|
|
|
{ |
|
81
|
23981 |
|
foreach ($options as $name => $value) { |
|
82
|
23006 |
|
$method = 'set' . $name; |
|
83
|
23006 |
|
if (! method_exists($this, $method)) { |
|
84
|
|
|
// next major: throw an exception |
|
85
|
2861 |
|
@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
|
2861 |
|
$name |
|
89
|
2861 |
|
), E_USER_DEPRECATED); |
|
90
|
|
|
|
|
91
|
2861 |
|
continue; |
|
92
|
|
|
} |
|
93
|
23006 |
|
$this->$method($value); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
23981 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return Column |
|
101
|
|
|
*/ |
|
102
|
23981 |
|
public function setType(Type $type) |
|
103
|
|
|
{ |
|
104
|
23981 |
|
$this->_type = $type; |
|
105
|
|
|
|
|
106
|
23981 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param int|null $length |
|
111
|
|
|
* |
|
112
|
|
|
* @return Column |
|
113
|
|
|
*/ |
|
114
|
21874 |
|
public function setLength($length) |
|
115
|
|
|
{ |
|
116
|
21874 |
|
if ($length !== null) { |
|
117
|
21822 |
|
$this->_length = (int) $length; |
|
118
|
|
|
} else { |
|
119
|
21475 |
|
$this->_length = null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
21874 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param int $precision |
|
127
|
|
|
* |
|
128
|
|
|
* @return Column |
|
129
|
|
|
*/ |
|
130
|
21522 |
|
public function setPrecision($precision) |
|
131
|
|
|
{ |
|
132
|
21522 |
|
if (! is_numeric($precision)) { |
|
|
|
|
|
|
133
|
19949 |
|
$precision = 10; // defaults to 10 when no valid precision is given. |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
21522 |
|
$this->_precision = (int) $precision; |
|
137
|
|
|
|
|
138
|
21522 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param int $scale |
|
143
|
|
|
* |
|
144
|
|
|
* @return Column |
|
145
|
|
|
*/ |
|
146
|
21520 |
|
public function setScale($scale) |
|
147
|
|
|
{ |
|
148
|
21520 |
|
if (! is_numeric($scale)) { |
|
|
|
|
|
|
149
|
19893 |
|
$scale = 0; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
21520 |
|
$this->_scale = (int) $scale; |
|
153
|
|
|
|
|
154
|
21520 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param bool $unsigned |
|
159
|
|
|
* |
|
160
|
|
|
* @return Column |
|
161
|
|
|
*/ |
|
162
|
21481 |
|
public function setUnsigned($unsigned) |
|
163
|
|
|
{ |
|
164
|
21481 |
|
$this->_unsigned = (bool) $unsigned; |
|
165
|
|
|
|
|
166
|
21481 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param bool $fixed |
|
171
|
|
|
* |
|
172
|
|
|
* @return Column |
|
173
|
|
|
*/ |
|
174
|
21725 |
|
public function setFixed($fixed) |
|
175
|
|
|
{ |
|
176
|
21725 |
|
$this->_fixed = (bool) $fixed; |
|
177
|
|
|
|
|
178
|
21725 |
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param bool $notnull |
|
183
|
|
|
* |
|
184
|
|
|
* @return Column |
|
185
|
|
|
*/ |
|
186
|
23297 |
|
public function setNotnull($notnull) |
|
187
|
|
|
{ |
|
188
|
23297 |
|
$this->_notnull = (bool) $notnull; |
|
189
|
|
|
|
|
190
|
23297 |
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param mixed $default |
|
195
|
|
|
* |
|
196
|
|
|
* @return Column |
|
197
|
|
|
*/ |
|
198
|
21803 |
|
public function setDefault($default) |
|
199
|
|
|
{ |
|
200
|
21803 |
|
if (is_float($default)) { |
|
201
|
18815 |
|
$localeInfo = localeconv(); |
|
202
|
18815 |
|
$decimal = $localeInfo['decimal_point'] ?? '.'; |
|
203
|
18815 |
|
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
|
|
|
$default = strval($default); |
|
207
|
|
|
$default = str_replace($decimal, '.', $default); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
21803 |
|
$this->_default = $default; |
|
211
|
|
|
|
|
212
|
21803 |
|
return $this; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param mixed[] $platformOptions |
|
217
|
|
|
* |
|
218
|
|
|
* @return Column |
|
219
|
|
|
*/ |
|
220
|
17888 |
|
public function setPlatformOptions(array $platformOptions) |
|
221
|
|
|
{ |
|
222
|
17888 |
|
$this->_platformOptions = $platformOptions; |
|
223
|
|
|
|
|
224
|
17888 |
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param string $name |
|
229
|
|
|
* @param mixed $value |
|
230
|
|
|
* |
|
231
|
|
|
* @return Column |
|
232
|
|
|
*/ |
|
233
|
21029 |
|
public function setPlatformOption($name, $value) |
|
234
|
|
|
{ |
|
235
|
21029 |
|
$this->_platformOptions[$name] = $value; |
|
236
|
|
|
|
|
237
|
21029 |
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @param string $value |
|
242
|
|
|
* |
|
243
|
|
|
* @return Column |
|
244
|
|
|
*/ |
|
245
|
11800 |
|
public function setColumnDefinition($value) |
|
246
|
|
|
{ |
|
247
|
11800 |
|
$this->_columnDefinition = $value; |
|
248
|
|
|
|
|
249
|
11800 |
|
return $this; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return Type |
|
254
|
|
|
*/ |
|
255
|
23447 |
|
public function getType() |
|
256
|
|
|
{ |
|
257
|
23447 |
|
return $this->_type; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @return int|null |
|
262
|
|
|
*/ |
|
263
|
20321 |
|
public function getLength() |
|
264
|
|
|
{ |
|
265
|
20321 |
|
return $this->_length; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return int |
|
270
|
|
|
*/ |
|
271
|
20310 |
|
public function getPrecision() |
|
272
|
|
|
{ |
|
273
|
20310 |
|
return $this->_precision; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @return int |
|
278
|
|
|
*/ |
|
279
|
20310 |
|
public function getScale() |
|
280
|
|
|
{ |
|
281
|
20310 |
|
return $this->_scale; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @return bool |
|
286
|
|
|
*/ |
|
287
|
20426 |
|
public function getUnsigned() |
|
288
|
|
|
{ |
|
289
|
20426 |
|
return $this->_unsigned; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @return bool |
|
294
|
|
|
*/ |
|
295
|
20418 |
|
public function getFixed() |
|
296
|
|
|
{ |
|
297
|
20418 |
|
return $this->_fixed; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @return bool |
|
302
|
|
|
*/ |
|
303
|
20486 |
|
public function getNotnull() |
|
304
|
|
|
{ |
|
305
|
20486 |
|
return $this->_notnull; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @return string|null |
|
310
|
|
|
*/ |
|
311
|
21223 |
|
public function getDefault() |
|
312
|
|
|
{ |
|
313
|
21223 |
|
return $this->_default; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @return mixed[] |
|
318
|
|
|
*/ |
|
319
|
21667 |
|
public function getPlatformOptions() |
|
320
|
|
|
{ |
|
321
|
21667 |
|
return $this->_platformOptions; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param string $name |
|
326
|
|
|
* |
|
327
|
|
|
* @return bool |
|
328
|
|
|
*/ |
|
329
|
23103 |
|
public function hasPlatformOption($name) |
|
330
|
|
|
{ |
|
331
|
23103 |
|
return isset($this->_platformOptions[$name]); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @param string $name |
|
336
|
|
|
* |
|
337
|
|
|
* @return mixed |
|
338
|
|
|
*/ |
|
339
|
18484 |
|
public function getPlatformOption($name) |
|
340
|
|
|
{ |
|
341
|
18484 |
|
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
|
21233 |
|
public function getAutoincrement() |
|
356
|
|
|
{ |
|
357
|
21233 |
|
return $this->_autoincrement; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* @param bool $flag |
|
362
|
|
|
* |
|
363
|
|
|
* @return Column |
|
364
|
|
|
*/ |
|
365
|
21933 |
|
public function setAutoincrement($flag) |
|
366
|
|
|
{ |
|
367
|
21933 |
|
$this->_autoincrement = $flag; |
|
368
|
|
|
|
|
369
|
21933 |
|
return $this; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* @param string|null $comment |
|
374
|
|
|
* |
|
375
|
|
|
* @return Column |
|
376
|
|
|
*/ |
|
377
|
21727 |
|
public function setComment($comment) |
|
378
|
|
|
{ |
|
379
|
21727 |
|
$this->_comment = $comment; |
|
380
|
|
|
|
|
381
|
21727 |
|
return $this; |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* @return string|null |
|
386
|
|
|
*/ |
|
387
|
23425 |
|
public function getComment() |
|
388
|
|
|
{ |
|
389
|
23425 |
|
return $this->_comment; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* @param string $name |
|
394
|
|
|
* @param mixed $value |
|
395
|
|
|
* |
|
396
|
|
|
* @return Column |
|
397
|
|
|
*/ |
|
398
|
2436 |
|
public function setCustomSchemaOption($name, $value) |
|
399
|
|
|
{ |
|
400
|
2436 |
|
$this->_customSchemaOptions[$name] = $value; |
|
401
|
|
|
|
|
402
|
2436 |
|
return $this; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* @param string $name |
|
407
|
|
|
* |
|
408
|
|
|
* @return bool |
|
409
|
|
|
*/ |
|
410
|
2911 |
|
public function hasCustomSchemaOption($name) |
|
411
|
|
|
{ |
|
412
|
2911 |
|
return isset($this->_customSchemaOptions[$name]); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* @param string $name |
|
417
|
|
|
* |
|
418
|
|
|
* @return mixed |
|
419
|
|
|
*/ |
|
420
|
2911 |
|
public function getCustomSchemaOption($name) |
|
421
|
|
|
{ |
|
422
|
2911 |
|
return $this->_customSchemaOptions[$name]; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* @param mixed[] $customSchemaOptions |
|
427
|
|
|
* |
|
428
|
|
|
* @return Column |
|
429
|
|
|
*/ |
|
430
|
2915 |
|
public function setCustomSchemaOptions(array $customSchemaOptions) |
|
431
|
|
|
{ |
|
432
|
2915 |
|
$this->_customSchemaOptions = $customSchemaOptions; |
|
433
|
|
|
|
|
434
|
2915 |
|
return $this; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* @return mixed[] |
|
439
|
|
|
*/ |
|
440
|
21665 |
|
public function getCustomSchemaOptions() |
|
441
|
|
|
{ |
|
442
|
21665 |
|
return $this->_customSchemaOptions; |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/** |
|
446
|
|
|
* @return mixed[] |
|
447
|
|
|
*/ |
|
448
|
23659 |
|
public function toArray() |
|
449
|
|
|
{ |
|
450
|
23659 |
|
return array_merge([ |
|
451
|
23659 |
|
'name' => $this->_name, |
|
452
|
23659 |
|
'type' => $this->_type, |
|
453
|
23659 |
|
'default' => $this->_default, |
|
454
|
23659 |
|
'notnull' => $this->_notnull, |
|
455
|
23659 |
|
'length' => $this->_length, |
|
456
|
23659 |
|
'precision' => $this->_precision, |
|
457
|
23659 |
|
'scale' => $this->_scale, |
|
458
|
23659 |
|
'fixed' => $this->_fixed, |
|
459
|
23659 |
|
'unsigned' => $this->_unsigned, |
|
460
|
23659 |
|
'autoincrement' => $this->_autoincrement, |
|
461
|
23659 |
|
'columnDefinition' => $this->_columnDefinition, |
|
462
|
23659 |
|
'comment' => $this->_comment, |
|
463
|
23659 |
|
], $this->_platformOptions, $this->_customSchemaOptions); |
|
464
|
|
|
} |
|
465
|
|
|
} |
|
466
|
|
|
|