Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Util | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
backupData | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
7 | |||
backupStructure | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 |
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\Sqlite; |
18 | |
19 | use PDO; |
20 | use Query\Drivers\AbstractUtil; |
21 | |
22 | /** |
23 | * SQLite-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 data |
29 | */ |
30 | public function backupData(array $excluded=[]): string |
31 | { |
32 | // Get a list of all the objects |
33 | $sql = 'SELECT DISTINCT "name" |
34 | FROM "sqlite_master" |
35 | WHERE "type"=\'table\''; |
36 | |
37 | if ( ! empty($excluded)) |
38 | { |
39 | $sql .= " AND \"name\" NOT IN('" . implode("','", $excluded) . "')"; |
40 | } |
41 | |
42 | $res = $this->getDriver()->query($sql); |
43 | $result = $res->fetchAll(PDO::FETCH_ASSOC); |
44 | |
45 | unset($res); |
46 | |
47 | $outputSql = ''; |
48 | |
49 | // Get the data for each object |
50 | foreach ($result as $r) |
51 | { |
52 | $sql = 'SELECT * FROM "' . $r['name'] . '"'; |
53 | $res = $this->getDriver()->query($sql); |
54 | $objRes = $res->fetchAll(PDO::FETCH_ASSOC); |
55 | |
56 | unset($res); |
57 | |
58 | // If the row is empty, continue |
59 | if (empty($objRes)) |
60 | { |
61 | continue; |
62 | } |
63 | |
64 | // Nab the column names by getting the keys of the first row |
65 | $columns = array_keys(current($objRes)); |
66 | |
67 | $insertRows = []; |
68 | |
69 | // Create the insert statements |
70 | foreach ($objRes as $row) |
71 | { |
72 | $row = array_values($row); |
73 | |
74 | // Quote values as needed by type |
75 | foreach ($row as $i => $_) |
76 | { |
77 | $row[$i] = (is_numeric($row[$i])) |
78 | ? $row[$i] |
79 | : $this->getDriver()->quote($row[$i]); |
80 | } |
81 | |
82 | $rowString = 'INSERT INTO "' . $r['name'] . '" ("' . implode('","', $columns) . '") VALUES (' . implode(',', $row) . ');'; |
83 | |
84 | unset($row); |
85 | |
86 | $insertRows[] = $rowString; |
87 | } |
88 | |
89 | unset($objRes); |
90 | |
91 | $outputSql .= "\n\n" . implode("\n", $insertRows); |
92 | } |
93 | |
94 | return $outputSql; |
95 | } |
96 | |
97 | /** |
98 | * Create an SQL backup file for the current database's structure |
99 | */ |
100 | public function backupStructure(): string |
101 | { |
102 | // Fairly easy for SQLite...just query the master table |
103 | $sql = 'SELECT "sql" FROM "sqlite_master"'; |
104 | $res = $this->getDriver()->query($sql); |
105 | $result = $res->fetchAll(PDO::FETCH_ASSOC); |
106 | |
107 | $sqlArray = []; |
108 | |
109 | foreach ($result as $r) |
110 | { |
111 | $sqlArray[] = $r['sql']; |
112 | } |
113 | |
114 | return implode(";\n", $sqlArray) . ';'; |
115 | } |
116 | } |