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 table metadata. |
13
|
|
|
*/ |
14
|
|
|
final class TableMetadata |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $databaseName; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $tableName; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $engine; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $collation; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $charset; |
30
|
|
|
|
31
|
|
|
/** @var int */ |
32
|
|
|
private $rowCount; |
33
|
|
|
|
34
|
|
|
/** @var ColumnMetadata[] */ |
35
|
|
|
private $columnMetadataList = []; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $databaseName |
40
|
|
|
* @param string $tableName |
41
|
|
|
* @param string $engine |
42
|
|
|
* @param string $collation |
43
|
|
|
* @param string $charset |
44
|
|
|
* @param int $rowCount |
45
|
|
|
* @param ColumnMetadata[] $columnMetadataList |
46
|
|
|
*/ |
47
|
14 |
|
public function __construct( |
48
|
|
|
string $databaseName, |
49
|
|
|
string $tableName, |
50
|
|
|
string $engine, |
51
|
|
|
string $collation, |
52
|
|
|
string $charset, |
53
|
|
|
int $rowCount, |
54
|
|
|
array $columnMetadataList |
55
|
|
|
) { |
56
|
14 |
|
$this->databaseName = $databaseName; |
57
|
14 |
|
$this->tableName = $tableName; |
58
|
14 |
|
$this->engine = $engine; |
59
|
14 |
|
$this->collation = $collation; |
60
|
14 |
|
$this->rowCount = $rowCount; |
61
|
14 |
|
$this->charset = $charset; |
62
|
|
|
|
63
|
14 |
|
foreach ($columnMetadataList as $columnMetadata) { |
64
|
14 |
|
$this->columnMetadataList[$columnMetadata->getColumnName()] = $columnMetadata; |
65
|
|
|
} |
66
|
14 |
|
} |
67
|
|
|
|
68
|
1 |
|
public function getDatabaseName(): string |
69
|
|
|
{ |
70
|
1 |
|
return $this->databaseName; |
71
|
|
|
} |
72
|
|
|
|
73
|
7 |
|
public function getTableName(): string |
74
|
|
|
{ |
75
|
7 |
|
return $this->tableName; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getEngine(): string |
79
|
|
|
{ |
80
|
1 |
|
return $this->engine; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function getCollation(): string |
84
|
|
|
{ |
85
|
1 |
|
return $this->collation; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function getCharset(): string |
89
|
|
|
{ |
90
|
1 |
|
return $this->charset; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function getRowCount(): int |
94
|
|
|
{ |
95
|
1 |
|
return $this->rowCount; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the metadata for a single column. |
100
|
|
|
* |
101
|
|
|
* @throws \RuntimeException when the column does not exist. |
102
|
|
|
*/ |
103
|
2 |
|
public function getColumnMetadata(string $columnName): ColumnMetadata |
104
|
|
|
{ |
105
|
2 |
|
if (!array_key_exists($columnName, $this->columnMetadataList)) { |
106
|
1 |
|
throw new \RuntimeException('Table "' . $this->tableName . '" doesn\'t contain column "' . $columnName . '"'); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return $this->columnMetadataList[$columnName]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the metadata for all columns. |
114
|
|
|
* |
115
|
|
|
* @return ColumnMetadata[] |
116
|
|
|
*/ |
117
|
1 |
|
public function getAllColumnMetadata(): array |
118
|
|
|
{ |
119
|
1 |
|
return $this->columnMetadataList; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns true if the table has at least one column that is a string type. |
124
|
|
|
*/ |
125
|
2 |
|
public function hasStringTypeColumn(): bool |
126
|
|
|
{ |
127
|
2 |
|
$hasStringType = false; |
128
|
2 |
|
foreach ($this->columnMetadataList as $columnMetadata) { |
129
|
2 |
|
$hasStringType = $hasStringType || $columnMetadata->isStringType(); |
130
|
|
|
} |
131
|
2 |
|
return $hasStringType; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the primary key column metadata. |
136
|
|
|
*/ |
137
|
1 |
|
public function getPrimaryKeyMetadata(): ?ColumnMetadata |
138
|
|
|
{ |
139
|
|
|
$filteredColumnList = array_filter($this->columnMetadataList, function (ColumnMetadata $columnMetadata) { |
140
|
1 |
|
return $columnMetadata->isPrimaryKey(); |
141
|
1 |
|
}); |
142
|
|
|
|
143
|
1 |
|
return (count($filteredColumnList)) ? current($filteredColumnList) : null; |
144
|
|
|
} |
145
|
|
|
} |