-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathInsertEditColumn.php
86 lines (75 loc) · 2.37 KB
/
InsertEditColumn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use function date;
use function md5;
use function preg_match;
use function preg_replace;
final readonly class InsertEditColumn
{
public string|null $default;
public string $md5;
/**
* trueType contains only the type (stops at first bracket)
*/
public string $trueType;
public int $length;
public bool $firstTimestamp;
public function __construct(
public string $field,
public string $type,
public bool $isNull,
public string $key,
string|null $default,
public string $extra,
int $columnLength,
public bool $isBinary,
public bool $isBlob,
public bool $isChar,
bool $insertMode,
) {
if (
$this->type === 'datetime'
&& ! $this->isNull
&& $default === null
&& $insertMode
) {
$this->default = date('Y-m-d H:i:s');
} else {
$this->default = $default;
}
$this->md5 = md5($this->field);
$this->trueType = preg_replace('@(\(.*)|(\s/.*)@s', '', $this->type);
// length is unknown for geometry fields,
// make enough space to edit very simple WKTs
if ($columnLength === -1) {
$columnLength = 30;
}
$this->length = preg_match('@float|double@', $this->type) === 1 ? 100 : $columnLength;
/**
* TODO: This property is useless at the moment.
* It seems like a long time ago before refactoring into classes,
* this kept track of how many timestamps are in the table.
*/
$this->firstTimestamp = $this->trueType === 'timestamp';
}
public function getFractionalSecondsPrecision(): int
{
if ($this->trueType !== 'timestamp' && $this->trueType !== 'datetime' && $this->trueType !== 'time') {
return 0;
}
$matches = [];
if (preg_match('@\((\d+)\)@', $this->type, $matches) === 1) {
return (int) $matches[1];
}
return 0;
}
public function getDisplayType(): string
{
// Enum and set values, and any additional options within comments, are not displayed
return match ($this->trueType) {
'set', 'enum' => $this->trueType,
default => preg_replace('@\s/.+@', '', $this->type),
};
}
}