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