1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright (c) 2017-present brian ridley |
5
|
|
|
* @author brian ridley <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ptlis\GrepDb\Metadata\MySQL; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* DTO storing column metadata. |
13
|
|
|
*/ |
14
|
|
|
final class ColumnMetadata |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $databaseName; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $tableName; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $columnName; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $type; |
27
|
|
|
|
28
|
|
|
/** @var int|null */ |
29
|
|
|
private $maxLength; |
30
|
|
|
|
31
|
|
|
/** @var bool */ |
32
|
|
|
private $primaryKey; |
33
|
|
|
|
34
|
|
|
/** @var bool */ |
35
|
|
|
private $nullable; |
36
|
|
|
|
37
|
|
|
/** @var bool */ |
38
|
|
|
private $indexed; |
39
|
|
|
|
40
|
|
|
|
41
|
33 |
|
public function __construct( |
42
|
|
|
string $databaseName, |
43
|
|
|
string $tableName, |
44
|
|
|
string $columnName, |
45
|
|
|
string $type, |
46
|
|
|
?int $maxLength, |
47
|
|
|
bool $primaryKey, |
48
|
|
|
bool $nullable, |
49
|
|
|
bool $indexed |
50
|
|
|
) { |
51
|
33 |
|
$this->databaseName = $databaseName; |
52
|
33 |
|
$this->tableName = $tableName; |
53
|
33 |
|
$this->columnName = $columnName; |
54
|
33 |
|
$this->type = $type; |
55
|
33 |
|
$this->maxLength = $maxLength; |
56
|
33 |
|
$this->primaryKey = $primaryKey; |
57
|
33 |
|
$this->nullable = $nullable; |
58
|
33 |
|
$this->indexed = $indexed; |
59
|
33 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function getDatabaseName(): string |
62
|
|
|
{ |
63
|
1 |
|
return $this->databaseName; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function getTableName(): string |
67
|
|
|
{ |
68
|
1 |
|
return $this->tableName; |
69
|
|
|
} |
70
|
|
|
|
71
|
15 |
|
public function getColumnName(): string |
72
|
|
|
{ |
73
|
15 |
|
return $this->columnName; |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
public function getType(): string |
77
|
|
|
{ |
78
|
6 |
|
return $this->type; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function getMaxLength(): ?int |
82
|
|
|
{ |
83
|
1 |
|
return $this->maxLength; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function isPrimaryKey(): bool |
87
|
|
|
{ |
88
|
2 |
|
return $this->primaryKey; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function isNullable(): bool |
92
|
|
|
{ |
93
|
1 |
|
return $this->nullable; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function isIndexed(): bool |
97
|
|
|
{ |
98
|
1 |
|
return $this->indexed; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns true if the column is a string type. |
103
|
|
|
* |
104
|
|
|
* List validated against https://dev.mysql.com/doc/refman/5.7/en/string-types.html |
105
|
|
|
*/ |
106
|
5 |
|
public function isStringType() |
107
|
|
|
{ |
108
|
|
|
$stringTypeList = [ |
109
|
5 |
|
'char', |
110
|
|
|
'varchar', |
111
|
|
|
'tinyblob', |
112
|
|
|
'blob', |
113
|
|
|
'mediumblob', |
114
|
|
|
'longblob', |
115
|
|
|
'tinytext', |
116
|
|
|
'text', |
117
|
|
|
'mediumtext', |
118
|
|
|
'longtext' |
119
|
|
|
]; |
120
|
5 |
|
$isString = false; |
121
|
5 |
|
$type = trim(strtolower($this->getType())); |
122
|
5 |
|
foreach ($stringTypeList as $stringType) { |
123
|
|
|
// Column type has string type prefix (e.g. VARCHAR(100), TEXT) |
124
|
5 |
|
if (substr($type, 0, strlen($stringType)) === $stringType) { |
125
|
3 |
|
$isString = true; |
126
|
|
|
} |
127
|
|
|
} |
128
|
5 |
|
return $isString; |
129
|
|
|
} |
130
|
|
|
} |