Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Util | |
100.00% |
24 / 24 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
backupStructure | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
backupData | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
6 |
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\Pgsql; |
18 | |
19 | use PDO; |
20 | use Query\Drivers\AbstractUtil; |
21 | |
22 | /** |
23 | * Postgres-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 | // @TODO Implement Backup function |
33 | return ''; |
34 | } |
35 | |
36 | /** |
37 | * Create an SQL backup file for the current database's data |
38 | */ |
39 | public function backupData(array $exclude=[]): string |
40 | { |
41 | $tables = $this->getDriver()->getTables(); |
42 | |
43 | // Filter out the tables you don't want |
44 | if ( ! empty($exclude)) |
45 | { |
46 | $tables = array_diff($tables, $exclude); |
47 | } |
48 | |
49 | $outputSql = ''; |
50 | |
51 | // Get the data for each object |
52 | foreach ($tables as $t) |
53 | { |
54 | $sql = 'SELECT * FROM "' . trim($t) . '"'; |
55 | $res = $this->getDriver()->query($sql); |
56 | $objRes = $res->fetchAll(PDO::FETCH_ASSOC); |
57 | |
58 | // Don't add to the file if the table is empty |
59 | if ((is_countable($objRes) ? count($objRes) : 0) < 1) |
60 | { |
61 | continue; |
62 | } |
63 | |
64 | $res = NULL; |
65 | |
66 | // Nab the column names by getting the keys of the first row |
67 | $columns = @array_keys($objRes[0]); |
68 | |
69 | $insertRows = []; |
70 | |
71 | // Create the insert statements |
72 | foreach ($objRes as $row) |
73 | { |
74 | $row = array_values($row); |
75 | |
76 | // Quote values as needed by type |
77 | $row = array_map([$this->getDriver(), 'quote'], $row); |
78 | $row = array_map('trim', $row); |
79 | |
80 | $rowString = 'INSERT INTO "' . trim($t) . '" ("' . implode('","', $columns) . '") VALUES (' . implode(',', $row) . ');'; |
81 | |
82 | $row = NULL; |
83 | |
84 | $insertRows[] = $rowString; |
85 | } |
86 | |
87 | $objRes = NULL; |
88 | |
89 | $outputSql .= "\n\n" . implode("\n", $insertRows) . "\n"; |
90 | } |
91 | |
92 | return $outputSql; |
93 | } |
94 | } |