1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Facades\Traits; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableEntity; |
6
|
|
|
|
7
|
|
|
use function implode; |
8
|
|
|
use function array_keys; |
9
|
|
|
use function in_array; |
10
|
|
|
|
11
|
|
|
trait TableDumpTrait |
12
|
|
|
{ |
13
|
|
|
use TableDataDumpTrait; |
14
|
|
|
|
15
|
|
|
// Temp vars for table dumps |
16
|
|
|
private $views = []; |
17
|
|
|
private $fkeys = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $table |
21
|
|
|
* @param string $style |
22
|
|
|
* @param int $tableType |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
private function getCreateQuery(string $table, string $style, int $tableType): string |
27
|
|
|
{ |
28
|
|
|
if ($tableType !== 2) { |
29
|
|
|
return $this->driver->getCreateTableQuery($table, $this->options['autoIncrement'], $style); |
30
|
|
|
} |
31
|
|
|
$fields = []; |
32
|
|
|
foreach ($this->driver->fields($table) as $name => $field) { |
33
|
|
|
$fields[] = $this->driver->escapeId($name) . ' ' . $field->fullType; |
34
|
|
|
} |
35
|
|
|
return 'CREATE TABLE ' . $this->driver->escapeTableName($table) . ' (' . implode(', ', $fields) . ')'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Export table structure |
40
|
|
|
* |
41
|
|
|
* @param string $table |
42
|
|
|
* @param string $style |
43
|
|
|
* @param int $tableType 0 table, 1 view, 2 temporary view table |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
private function addCreateQuery(string $table, string $style, int $tableType) |
48
|
|
|
{ |
49
|
|
|
$create = $this->getCreateQuery($table, $style, $tableType); |
50
|
|
|
$this->driver->setUtf8mb4($create); |
51
|
|
|
if (!$create) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
if ($style === 'DROP+CREATE' || $tableType === 1) { |
55
|
|
|
$this->queries[] = 'DROP ' . ($tableType === 2 ? 'VIEW' : 'TABLE') . |
56
|
|
|
' IF EXISTS ' . $this->driver->escapeTableName($table) . ';'; |
57
|
|
|
} |
58
|
|
|
if ($tableType === 1) { |
59
|
|
|
$create = $this->driver->removeDefiner($create); |
60
|
|
|
} |
61
|
|
|
$this->queries[] = $create . ';'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Export table structure |
66
|
|
|
* |
67
|
|
|
* @param string $table |
68
|
|
|
* @param string $style |
69
|
|
|
* @param int $tableType 0 table, 1 view, 2 temporary view table |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
private function dumpCreateTableOrView(string $table, string $style, int $tableType = 0) |
74
|
|
|
{ |
75
|
|
|
// From adminer.inc.php |
76
|
|
|
if ($this->options['format'] !== 'sql') { |
77
|
|
|
$this->queries[] = "\xef\xbb\xbf"; // UTF-8 byte order mark |
78
|
|
|
if ($style) { |
79
|
|
|
$this->dumpCsv(array_keys($this->driver->fields($table))); |
80
|
|
|
} |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
if (!$style) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->addCreateQuery($table, $style, $tableType); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $table |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
private function dumpTableTriggers(string $table) |
96
|
|
|
{ |
97
|
|
|
if (($triggers = $this->driver->getCreateTriggerQuery($table))) { |
98
|
|
|
$this->queries[] = 'DELIMITER ;'; |
99
|
|
|
$this->queries[] = $triggers; |
100
|
|
|
$this->queries[] = 'DELIMITER ;'; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $table |
106
|
|
|
* @param bool $dumpTable |
107
|
|
|
* @param bool $dumpData |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
private function dumpTable(string $table, bool $dumpTable, bool $dumpData) |
112
|
|
|
{ |
113
|
|
|
$this->dumpCreateTableOrView($table, ($dumpTable ? $this->options['table_style'] : '')); |
114
|
|
|
if ($dumpData) { |
115
|
|
|
$this->dumpTableData($table); |
116
|
|
|
} |
117
|
|
|
if ($this->options['is_sql'] && $this->options['triggers'] && $dumpTable) { |
118
|
|
|
$this->dumpTableTriggers($table); |
119
|
|
|
} |
120
|
|
|
if ($this->options['is_sql']) { |
121
|
|
|
$this->queries[] = ''; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $table |
127
|
|
|
* @param TableEntity $tableStatus |
128
|
|
|
* @param bool $dbDumpTable |
129
|
|
|
* @param bool $dbDumpData |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
private function dumpTableOnly(string $table, TableEntity $tableStatus, bool $dbDumpTable, bool $dbDumpData) |
134
|
|
|
{ |
135
|
|
|
if ($this->driver->isView($tableStatus)) { |
136
|
|
|
// The views will be dumped after the tables |
137
|
|
|
$this->views[] = $table; |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
$this->fkeys[] = $table; |
141
|
|
|
$dumpTable = $dbDumpTable || in_array($table, $this->tables['list']); |
142
|
|
|
$dumpData = $dbDumpData || in_array($table, $this->tables['data']); |
143
|
|
|
if ($dumpTable || $dumpData) { |
144
|
|
|
$this->dumpTable($table, $dumpTable, $dumpData); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $database The database name |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
private function dumpTables(string $database) |
154
|
|
|
{ |
155
|
|
|
$dbDumpTable = $this->tables['list'] === '*' && in_array($database, $this->databases['list']); |
156
|
|
|
$dbDumpData = in_array($database, $this->databases['data']); |
157
|
|
|
$this->views = []; // View names |
158
|
|
|
$this->fkeys = []; // Table names for foreign keys |
159
|
|
|
$dbTables = $this->driver->tableStatuses(true); |
160
|
|
|
foreach ($dbTables as $table => $tableStatus) { |
161
|
|
|
$this->dumpTableOnly($table, $tableStatus, $dbDumpTable, $dbDumpData); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|