1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Driver\MySQL\Schema; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\DriverInterface; |
15
|
|
|
use Cycle\Database\Exception\DefaultValueException; |
16
|
|
|
use Cycle\Database\Exception\SchemaException; |
17
|
|
|
use Cycle\Database\Injection\Fragment; |
18
|
|
|
use Cycle\Database\Injection\FragmentInterface; |
19
|
|
|
use Cycle\Database\Schema\AbstractColumn; |
|
|
|
|
20
|
|
|
use Cycle\Database\Schema\Attribute\ColumnAttribute; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Attention! You can use only one timestamp or datetime with DATETIME_NOW setting! Thought, it will |
24
|
|
|
* work on multiple fields with MySQL 5.6.6+ version. |
25
|
|
|
* |
26
|
|
|
* @method $this|AbstractColumn primary(int $size, bool $unsigned = false, $zerofill = false) |
27
|
|
|
* @method $this|AbstractColumn smallPrimary(int $size, bool $unsigned = false, $zerofill = false) |
28
|
|
|
* @method $this|AbstractColumn bigPrimary(int $size, bool $unsigned = false, $zerofill = false) |
29
|
|
|
* @method $this|AbstractColumn integer(int $size, bool $unsigned = false, $zerofill = false) |
30
|
|
|
* @method $this|AbstractColumn tinyInteger(int $size, bool $unsigned = false, $zerofill = false) |
31
|
|
|
* @method $this|AbstractColumn smallInteger(int $size, bool $unsigned = false, $zerofill = false) |
32
|
|
|
* @method $this|AbstractColumn bigInteger(int $size, bool $unsigned = false, $zerofill = false) |
33
|
|
|
* @method $this|AbstractColumn unsigned(bool $value) |
34
|
|
|
* @method $this|AbstractColumn zerofill(bool $value) |
35
|
|
|
*/ |
36
|
|
|
class MySQLColumn extends AbstractColumn |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Default timestamp expression (driver specific). |
40
|
|
|
*/ |
41
|
|
|
public const DATETIME_NOW = 'CURRENT_TIMESTAMP'; |
42
|
|
|
|
43
|
|
|
public const EXCLUDE_FROM_COMPARE = ['size', 'timezone', 'userType', 'attributes']; |
44
|
|
|
|
45
|
|
|
protected const INTEGER_TYPES = ['tinyint', 'smallint', 'mediumint', 'int', 'bigint']; |
46
|
|
|
|
47
|
|
|
protected array $mapping = [ |
48
|
|
|
//Primary sequences |
49
|
|
|
'primary' => [ |
50
|
|
|
'type' => 'int', |
51
|
|
|
'size' => 11, |
52
|
|
|
'autoIncrement' => true, |
53
|
|
|
'nullable' => false, |
54
|
|
|
], |
55
|
|
|
'smallPrimary' => [ |
56
|
|
|
'type' => 'smallint', |
57
|
|
|
'size' => 6, |
58
|
|
|
'autoIncrement' => true, |
59
|
|
|
'nullable' => false, |
60
|
|
|
], |
61
|
|
|
'bigPrimary' => [ |
62
|
|
|
'type' => 'bigint', |
63
|
|
|
'size' => 20, |
64
|
|
|
'autoIncrement' => true, |
65
|
|
|
'nullable' => false, |
66
|
|
|
], |
67
|
|
|
|
68
|
|
|
//Enum type (mapped via method) |
69
|
|
|
'enum' => 'enum', |
70
|
|
|
|
71
|
|
|
//Set type (mapped via method) |
72
|
|
|
'set' => 'set', |
73
|
|
|
|
74
|
|
|
//Logical types |
75
|
|
|
'boolean' => ['type' => 'tinyint', 'size' => 1], |
76
|
|
|
|
77
|
|
|
//Integer types (size can always be changed with size method), longInteger has method alias |
78
|
|
|
//bigInteger |
79
|
|
|
'integer' => ['type' => 'int', 'size' => 11, 'unsigned' => false, 'zerofill' => false], |
80
|
|
|
'tinyInteger' => ['type' => 'tinyint', 'size' => 4, 'unsigned' => false, 'zerofill' => false], |
81
|
|
|
'smallInteger'=> ['type' => 'smallint', 'size' => 6, 'unsigned' => false, 'zerofill' => false], |
82
|
|
|
'bigInteger' => ['type' => 'bigint', 'size' => 20, 'unsigned' => false, 'zerofill' => false], |
83
|
|
|
|
84
|
|
|
//String with specified length (mapped via method) |
85
|
|
|
'string' => ['type' => 'varchar', 'size' => 255], |
86
|
|
|
|
87
|
|
|
//Generic types |
88
|
|
|
'text' => 'text', |
89
|
|
|
'tinyText' => 'tinytext', |
90
|
|
|
'longText' => 'longtext', |
91
|
|
|
|
92
|
|
|
//Real types |
93
|
|
|
'double' => 'double', |
94
|
|
|
'float' => 'float', |
95
|
|
|
|
96
|
|
|
//Decimal type (mapped via method) |
97
|
|
|
'decimal' => 'decimal', |
98
|
|
|
|
99
|
|
|
//Date and Time types |
100
|
|
|
'datetime' => 'datetime', |
101
|
|
|
'date' => 'date', |
102
|
|
|
'time' => 'time', |
103
|
|
|
'timestamp' => ['type' => 'timestamp', 'defaultValue' => null], |
104
|
|
|
|
105
|
|
|
//Binary types |
106
|
|
|
'binary' => 'blob', |
107
|
|
|
'tinyBinary' => 'tinyblob', |
108
|
|
|
'longBinary' => 'longblob', |
109
|
|
|
|
110
|
|
|
//Additional types |
111
|
|
|
'json' => 'json', |
112
|
|
|
'uuid' => ['type' => 'varchar', 'size' => 36], |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
protected array $reverseMapping = [ |
116
|
|
|
'primary' => [['type' => 'int', 'autoIncrement' => true]], |
117
|
|
|
'bigPrimary' => ['serial', ['type' => 'bigint', 'size' => 20, 'autoIncrement' => true]], |
118
|
|
|
'enum' => ['enum'], |
119
|
|
|
'set' => ['set'], |
120
|
|
|
'boolean' => ['bool', 'boolean', ['type' => 'tinyint', 'size' => 1]], |
121
|
|
|
'integer' => ['int', 'integer', 'mediumint'], |
122
|
|
|
'tinyInteger' => ['tinyint'], |
123
|
|
|
'smallInteger'=> ['smallint'], |
124
|
|
|
'bigInteger' => ['bigint'], |
125
|
|
|
'string' => ['varchar', 'char'], |
126
|
|
|
'text' => ['text', 'mediumtext'], |
127
|
|
|
'tinyText' => ['tinytext'], |
128
|
|
|
'longText' => ['longtext'], |
129
|
|
|
'double' => ['double'], |
130
|
|
|
'float' => ['float', 'real'], |
131
|
|
|
'decimal' => ['decimal'], |
132
|
|
|
'datetime' => ['datetime'], |
133
|
|
|
'date' => ['date'], |
134
|
474 |
|
'time' => ['time'], |
135
|
|
|
'timestamp' => ['timestamp'], |
136
|
474 |
|
'binary' => ['blob', 'binary', 'varbinary'], |
137
|
|
|
'tinyBinary' => ['tinyblob'], |
138
|
474 |
|
'longBinary' => ['longblob'], |
139
|
|
|
'json' => ['json'], |
140
|
214 |
|
]; |
141
|
|
|
|
142
|
|
|
/** |
143
|
474 |
|
* List of types forbids default value set. |
144
|
|
|
*/ |
145
|
474 |
|
protected array $forbiddenDefaults = [ |
146
|
474 |
|
'text', |
147
|
332 |
|
'mediumtext', |
148
|
|
|
'tinytext', |
149
|
|
|
'longtext', |
150
|
456 |
|
'blob', |
151
|
|
|
'tinyblob', |
152
|
|
|
'longblob', |
153
|
|
|
'json', |
154
|
|
|
]; |
155
|
|
|
|
156
|
470 |
|
#[ColumnAttribute( |
157
|
|
|
['int', 'tinyint', 'smallint', 'bigint', 'varchar', 'varbinary', 'time', 'datetime', 'timestamp'] |
158
|
470 |
|
)] |
159
|
|
|
protected int $size = 0; |
160
|
470 |
|
|
161
|
470 |
|
/** |
162
|
470 |
|
* Column is auto incremental. |
163
|
470 |
|
*/ |
164
|
|
|
#[ColumnAttribute(self::INTEGER_TYPES)] |
165
|
|
|
protected bool $autoIncrement = false; |
166
|
470 |
|
|
167
|
470 |
|
/** |
168
|
470 |
|
* Unsigned integer type. Related to {@see INTEGER_TYPES} only. |
169
|
|
|
*/ |
170
|
|
|
#[ColumnAttribute(self::INTEGER_TYPES)] |
171
|
|
|
protected bool $unsigned = false; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Zerofill option. Related to {@see INTEGER_TYPES} only. |
175
|
|
|
*/ |
176
|
470 |
|
#[ColumnAttribute(self::INTEGER_TYPES)] |
177
|
|
|
protected bool $zerofill = false; |
178
|
470 |
|
|
179
|
470 |
|
/** |
180
|
280 |
|
* @psalm-return non-empty-string |
181
|
|
|
*/ |
182
|
280 |
|
public function sqlStatement(DriverInterface $driver): string |
183
|
162 |
|
{ |
184
|
162 |
|
if (\in_array($this->type, self::INTEGER_TYPES, true)) { |
185
|
|
|
return $this->sqlStatementInteger($driver); |
186
|
262 |
|
} |
187
|
|
|
|
188
|
|
|
$defaultValue = $this->defaultValue; |
189
|
|
|
|
190
|
|
|
if (\in_array($this->type, $this->forbiddenDefaults, true)) { |
191
|
470 |
|
//Flushing default value for forbidden types |
192
|
446 |
|
$this->defaultValue = null; |
|
|
|
|
193
|
446 |
|
} |
194
|
338 |
|
|
195
|
338 |
|
$statement = parent::sqlStatement($driver); |
196
|
320 |
|
|
197
|
6 |
|
$this->defaultValue = $defaultValue; |
198
|
6 |
|
if ($this->autoIncrement) { |
199
|
316 |
|
return "{$statement} AUTO_INCREMENT"; |
200
|
2 |
|
} |
201
|
2 |
|
|
202
|
|
|
return $statement; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @psalm-param non-empty-string $table |
207
|
470 |
|
*/ |
208
|
152 |
|
public static function createInstance(string $table, array $schema, \DateTimeZone $timezone = null): self |
209
|
|
|
{ |
210
|
152 |
|
$column = new self($table, $schema['Field'], $timezone); |
211
|
|
|
|
212
|
|
|
$column->type = $schema['Type']; |
|
|
|
|
213
|
|
|
$column->nullable = strtolower($schema['Null']) === 'yes'; |
|
|
|
|
214
|
462 |
|
$column->defaultValue = $schema['Default']; |
|
|
|
|
215
|
|
|
$column->autoIncrement = stripos($schema['Extra'], 'auto_increment') !== false; |
216
|
|
|
|
217
|
|
|
if ( |
218
|
|
|
!preg_match( |
219
|
|
|
'/^(?P<type>[a-z]+)(?:\((?P<options>[^)]+)\))?(?: (?P<attr>[a-z ]+))?/', |
220
|
462 |
|
$column->type, |
221
|
462 |
|
$matches |
222
|
|
|
) |
223
|
|
|
) { |
224
|
|
|
//No extra definitions |
225
|
|
|
return $column; |
226
|
|
|
} |
227
|
462 |
|
|
228
|
|
|
$column->type = $matches['type']; |
229
|
|
|
|
230
|
|
|
$options = []; |
231
|
|
|
if (!empty($matches['options'])) { |
232
|
|
|
$options = \explode(',', $matches['options']); |
233
|
|
|
|
234
|
|
|
if (count($options) > 1) { |
235
|
|
|
$column->precision = (int)$options[0]; |
|
|
|
|
236
|
|
|
$column->scale = (int)$options[1]; |
|
|
|
|
237
|
170 |
|
} else { |
238
|
|
|
$column->size = (int)$options[0]; |
239
|
|
|
} |
240
|
|
|
} |
241
|
170 |
|
|
242
|
|
|
if (!empty($matches['attr'])) { |
243
|
|
|
if (\in_array($column->type, self::INTEGER_TYPES, true)) { |
244
|
|
|
$intAttr = array_map('trim', explode(' ', $matches['attr'])); |
245
|
170 |
|
if (\in_array('unsigned', $intAttr, true)) { |
246
|
|
|
$column->unsigned = true; |
247
|
|
|
} |
248
|
|
|
if (\in_array('zerofill', $intAttr, true)) { |
249
|
|
|
$column->zerofill = true; |
250
|
|
|
} |
251
|
|
|
unset($intAttr); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// since 8.0 database does not provide size for some columns |
256
|
|
|
if ($column->size === 0) { |
257
|
|
|
switch ($column->type) { |
258
|
|
|
case 'int': |
259
|
|
|
$column->size = 11; |
260
|
|
|
break; |
261
|
|
|
case 'bigint': |
262
|
|
|
$column->size = 20; |
263
|
|
|
break; |
264
|
|
|
case 'tinyint': |
265
|
|
|
$column->size = 4; |
266
|
|
|
break; |
267
|
|
|
case 'smallint': |
268
|
|
|
$column->size = 6; |
269
|
|
|
break; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
//Fetching enum and set values |
274
|
|
|
if ($options !== [] && static::isEnum($column)) { |
275
|
|
|
$column->enumValues = \array_map(static fn ($value) => trim($value, $value[0]), $options); |
|
|
|
|
276
|
|
|
|
277
|
|
|
return $column; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
//Default value conversions |
281
|
|
|
if ($column->type === 'bit' && $column->hasDefaultValue()) { |
282
|
|
|
//Cutting b\ and ' |
283
|
|
|
$column->defaultValue = new Fragment($column->defaultValue); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if ( |
287
|
|
|
$column->defaultValue === '0000-00-00 00:00:00' |
288
|
|
|
&& $column->getAbstractType() === 'timestamp' |
289
|
|
|
) { |
290
|
|
|
//Normalizing default value for timestamps |
291
|
|
|
$column->defaultValue = 0; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $column; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function compare(AbstractColumn $initial): bool |
298
|
|
|
{ |
299
|
|
|
$result = parent::compare($initial); |
300
|
|
|
|
301
|
|
|
if ($this->type === 'varchar' || $this->type === 'varbinary') { |
302
|
|
|
return $result && $this->size === $initial->size; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $result; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function isUnsigned(): bool |
309
|
|
|
{ |
310
|
|
|
return $this->unsigned; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function isZerofill(): bool |
314
|
|
|
{ |
315
|
|
|
return $this->zerofill; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function set(string|array $values): self |
319
|
|
|
{ |
320
|
|
|
$this->type('set'); |
321
|
|
|
$this->enumValues = array_map('strval', is_array($values) ? $values : func_get_args()); |
|
|
|
|
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param int<0, max> $size |
328
|
|
|
*/ |
329
|
|
|
public function varbinary(int $size = 255): self |
330
|
|
|
{ |
331
|
|
|
$this->type('varbinary'); |
332
|
|
|
|
333
|
|
|
$size < 0 && throw new SchemaException('Invalid varbinary size value'); |
334
|
|
|
|
335
|
|
|
$this->size = $size; |
336
|
|
|
|
337
|
|
|
return $this; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* If a size is provided, a varbinary column of the specified size will be created. |
342
|
|
|
* Otherwise, a blob type column will be created. |
343
|
|
|
* |
344
|
|
|
* @param int<0, max> $size |
345
|
|
|
*/ |
346
|
|
|
public function binary(int $size = 0): self |
347
|
|
|
{ |
348
|
|
|
if ($size > 0) { |
349
|
|
|
return $this->varbinary($size); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
$this->type('blob'); |
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Ensure that datetime fields are correctly formatted. |
359
|
|
|
* |
360
|
|
|
* @psalm-param non-empty-string $type |
361
|
|
|
* |
362
|
|
|
* @throws DefaultValueException |
363
|
|
|
*/ |
364
|
|
|
protected function formatDatetime( |
365
|
|
|
string $type, |
366
|
|
|
string|int|\DateTimeInterface $value |
367
|
|
|
): \DateTimeInterface|FragmentInterface|string { |
368
|
|
|
if ($value === 'current_timestamp()') { |
369
|
|
|
$value = self::DATETIME_NOW; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return parent::formatDatetime($type, $value); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
protected static function isEnum(AbstractColumn $column): bool |
376
|
|
|
{ |
377
|
|
|
return $column->getAbstractType() === 'enum' || $column->getAbstractType() === 'set'; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
private function sqlStatementInteger(DriverInterface $driver): string |
381
|
|
|
{ |
382
|
|
|
return \sprintf( |
383
|
|
|
'%s %s(%s)%s%s%s%s%s', |
384
|
|
|
$driver->identifier($this->name), |
|
|
|
|
385
|
|
|
$this->type, |
386
|
|
|
$this->size, |
387
|
|
|
$this->unsigned ? ' UNSIGNED' : '', |
388
|
|
|
$this->zerofill ? ' ZEROFILL' : '', |
389
|
|
|
$this->nullable ? ' NULL' : ' NOT NULL', |
390
|
|
|
$this->defaultValue !== null ? " DEFAULT {$this->quoteDefault($driver)}" : '', |
391
|
|
|
$this->autoIncrement ? ' AUTO_INCREMENT' : '' |
392
|
|
|
); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths