|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\MySql\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableEntity; |
|
6
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TriggerEntity; |
|
7
|
|
|
|
|
8
|
|
|
use Lagdo\DbAdmin\Driver\Db\Table as AbstractTable; |
|
9
|
|
|
|
|
10
|
|
|
class Table extends AbstractTable |
|
11
|
|
|
{ |
|
12
|
|
|
use Traits\TableFieldTrait; |
|
13
|
|
|
use Traits\TableIndexTrait; |
|
14
|
|
|
use Traits\TableTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param bool $fast |
|
18
|
|
|
* @param string $table |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
private function queryStatus(bool $fast, string $table = '') |
|
23
|
|
|
{ |
|
24
|
|
|
$query = ($fast && $this->driver->minVersion(5)) ? |
|
25
|
|
|
"SELECT TABLE_NAME AS Name, ENGINE AS Engine, TABLE_COMMENT AS Comment " . |
|
26
|
|
|
"FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() " . |
|
27
|
|
|
($table != "" ? "AND TABLE_NAME = " . $this->driver->quote($table) : "ORDER BY Name") : |
|
28
|
|
|
"SHOW TABLE STATUS" . ($table != "" ? " LIKE " . $this->driver->quote(addcslashes($table, "%_\\")) : ""); |
|
29
|
|
|
return $this->driver->rows($query); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $row |
|
34
|
|
|
* |
|
35
|
|
|
* @return TableEntity |
|
36
|
|
|
*/ |
|
37
|
|
|
private function makeStatus(array $row) |
|
38
|
|
|
{ |
|
39
|
|
|
$status = new TableEntity($row['Name']); |
|
40
|
|
|
$status->engine = $row['Engine']; |
|
41
|
|
|
if ($row["Engine"] == "InnoDB") { |
|
42
|
|
|
// ignore internal comment, unnecessary since MySQL 5.1.21 |
|
43
|
|
|
$status->comment = preg_replace('~(?:(.+); )?InnoDB free: .*~', '\1', $row["Comment"]); |
|
44
|
|
|
} |
|
45
|
|
|
// if (!isset($row["Engine"])) { |
|
46
|
|
|
// $row["Comment"] = ""; |
|
47
|
|
|
// } |
|
48
|
|
|
|
|
49
|
|
|
return $status; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function tableStatus(string $table, bool $fast = false) |
|
56
|
|
|
{ |
|
57
|
|
|
$rows = $this->queryStatus($fast, $table); |
|
58
|
|
|
if (!($row = reset($rows))) { |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
return $this->makeStatus($row); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritDoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function tableStatuses(bool $fast = false) |
|
68
|
|
|
{ |
|
69
|
|
|
$tables = []; |
|
70
|
|
|
$rows = $this->queryStatus($fast); |
|
71
|
|
|
foreach ($rows as $row) { |
|
72
|
|
|
$tables[$row["Name"]] = $this->makeStatus($row); |
|
73
|
|
|
} |
|
74
|
|
|
return $tables; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function tableNames() |
|
81
|
|
|
{ |
|
82
|
|
|
$tables = []; |
|
83
|
|
|
$rows = $this->queryStatus(true); |
|
84
|
|
|
foreach ($rows as $row) { |
|
85
|
|
|
$tables[] = $row["Name"]; |
|
86
|
|
|
} |
|
87
|
|
|
return $tables; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritDoc |
|
92
|
|
|
*/ |
|
93
|
|
|
public function isView(TableEntity $tableStatus) |
|
94
|
|
|
{ |
|
95
|
|
|
return $tableStatus->engine === null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritDoc |
|
100
|
|
|
*/ |
|
101
|
|
|
public function trigger(string $name, string $table = '') |
|
102
|
|
|
{ |
|
103
|
|
|
if ($name == "") { |
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
$rows = $this->driver->rows("SHOW TRIGGERS WHERE `Trigger` = " . $this->driver->quote($name)); |
|
107
|
|
|
if (!($row = reset($rows))) { |
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
return new TriggerEntity($row["Timing"], $row["Event"], '', '', $row["Trigger"]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @inheritDoc |
|
115
|
|
|
*/ |
|
116
|
|
|
public function triggers(string $table) |
|
117
|
|
|
{ |
|
118
|
|
|
$triggers = []; |
|
119
|
|
|
foreach ($this->driver->rows("SHOW TRIGGERS LIKE " . $this->driver->quote(addcslashes($table, "%_\\"))) as $row) { |
|
120
|
|
|
$triggers[$row["Trigger"]] = new TriggerEntity($row["Timing"], $row["Event"], '', '', $row["Trigger"]); |
|
121
|
|
|
} |
|
122
|
|
|
return $triggers; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
|
|
*/ |
|
128
|
|
|
public function triggerOptions() |
|
129
|
|
|
{ |
|
130
|
|
|
return [ |
|
131
|
|
|
"Timing" => ["BEFORE", "AFTER"], |
|
132
|
|
|
"Event" => ["INSERT", "UPDATE", "DELETE"], |
|
133
|
|
|
"Type" => ["FOR EACH ROW"], |
|
134
|
|
|
]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @inheritDoc |
|
139
|
|
|
*/ |
|
140
|
|
|
public function tableHelp(string $name) |
|
141
|
|
|
{ |
|
142
|
|
|
$maria = preg_match('~MariaDB~', $this->driver->serverInfo()); |
|
143
|
|
|
if ($this->driver->isInformationSchema($this->driver->database())) { |
|
144
|
|
|
return strtolower(($maria ? "information-schema-$name-table/" : str_replace("_", "-", $name) . "-table.html")); |
|
145
|
|
|
} |
|
146
|
|
|
if ($this->driver->database() == "mysql") { |
|
147
|
|
|
return ($maria ? "mysql$name-table/" : "system-database.html"); //! more precise link |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|