|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Query |
|
4
|
|
|
* |
|
5
|
|
|
* SQL Query Builder / Database Abstraction Layer |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7.1 |
|
8
|
|
|
* |
|
9
|
|
|
* @package Query |
|
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
|
11
|
|
|
* @copyright 2012 - 2018 Timothy J. Warren |
|
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
13
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace Query\Drivers\Mysql; |
|
16
|
|
|
|
|
17
|
|
|
use PDO; |
|
18
|
|
|
use Query\Drivers\AbstractUtil; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* MySQL-specific backup, import and creation methods |
|
22
|
|
|
*/ |
|
23
|
|
|
class Util extends AbstractUtil { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create an SQL backup file for the current database's structure |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function backupStructure(): string |
|
31
|
|
|
{ |
|
32
|
|
|
$string = []; |
|
33
|
|
|
|
|
34
|
|
|
// Get databases |
|
35
|
|
|
$dbs = $this->getDriver()->getDbs(); |
|
36
|
|
|
|
|
37
|
|
|
foreach($dbs as &$d) |
|
38
|
|
|
{ |
|
39
|
|
|
// Skip built-in dbs |
|
40
|
|
|
// @codeCoverageIgnoreStart |
|
41
|
|
|
if ($d === 'mysql') |
|
42
|
|
|
{ |
|
43
|
|
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
// @codeCoverageIgnoreEnd |
|
46
|
|
|
|
|
47
|
|
|
// Get the list of tables |
|
48
|
|
|
$tables = $this->getDriver()->driverQuery("SHOW TABLES FROM `{$d}`", TRUE); |
|
49
|
|
|
|
|
50
|
|
|
foreach($tables as $table) |
|
51
|
|
|
{ |
|
52
|
|
|
$array = $this->getDriver()->driverQuery("SHOW CREATE TABLE `{$d}`.`{$table}`", FALSE); |
|
53
|
|
|
$row = current($array); |
|
54
|
|
|
|
|
55
|
|
|
if ( ! isset($row['Create Table'])) |
|
56
|
|
|
{ |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$string[] = $row['Create Table']; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return implode("\n\n", $string); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create an SQL backup file for the current database's data |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $exclude |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function backupData($exclude=[]): string |
|
75
|
|
|
{ |
|
76
|
|
|
$tables = $this->getDriver()->getTables(); |
|
77
|
|
|
|
|
78
|
|
|
// Filter out the tables you don't want |
|
79
|
|
|
if( ! empty($exclude)) |
|
80
|
|
|
{ |
|
81
|
|
|
$tables = array_diff($tables, $exclude); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$outputSql = ''; |
|
85
|
|
|
|
|
86
|
|
|
// Select the rows from each Table |
|
87
|
|
|
foreach($tables as $t) |
|
88
|
|
|
{ |
|
89
|
|
|
$sql = "SELECT * FROM `{$t}`"; |
|
90
|
|
|
$res = $this->getDriver()->query($sql); |
|
91
|
|
|
$rows = $res->fetchAll(PDO::FETCH_ASSOC); |
|
92
|
|
|
|
|
93
|
|
|
// Skip empty tables |
|
94
|
|
|
if (count($rows) < 1) |
|
95
|
|
|
{ |
|
96
|
|
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Nab the column names by getting the keys of the first row |
|
100
|
|
|
$columns = @array_keys($rows[0]); |
|
101
|
|
|
|
|
102
|
|
|
$insertRows = []; |
|
103
|
|
|
|
|
104
|
|
|
// Create the insert statements |
|
105
|
|
|
foreach($rows as $row) |
|
106
|
|
|
{ |
|
107
|
|
|
$row = array_values($row); |
|
108
|
|
|
|
|
109
|
|
|
// Workaround for Quercus |
|
110
|
|
|
foreach($row as &$r) |
|
111
|
|
|
{ |
|
112
|
|
|
$r = $this->getDriver()->quote($r); |
|
113
|
|
|
} |
|
114
|
|
|
$row = array_map('trim', $row); |
|
115
|
|
|
|
|
116
|
|
|
$rowString = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');'; |
|
117
|
|
|
|
|
118
|
|
|
$row = NULL; |
|
119
|
|
|
|
|
120
|
|
|
$insertRows[] = $rowString; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$outputSql .= "\n\n".implode("\n", $insertRows)."\n"; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $outputSql; |
|
127
|
|
|
} |
|
128
|
|
|
} |