|
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\Pgsql; |
|
16
|
|
|
|
|
17
|
|
|
use Query\Drivers\AbstractUtil; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Postgres-specific backup, import and creation methods |
|
21
|
|
|
*/ |
|
22
|
|
|
class Util extends AbstractUtil { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create an SQL backup file for the current database's structure |
|
26
|
|
|
* |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
public function backupStructure(): string |
|
30
|
|
|
{ |
|
31
|
|
|
// @TODO Implement Backup function |
|
32
|
|
|
return ''; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create an SQL backup file for the current database's data |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $exclude |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function backupData(array $exclude=[]): string |
|
42
|
|
|
{ |
|
43
|
|
|
$tables = $this->getDriver()->getTables(); |
|
44
|
|
|
|
|
45
|
|
|
// Filter out the tables you don't want |
|
46
|
|
|
if( ! empty($exclude)) |
|
47
|
|
|
{ |
|
48
|
|
|
$tables = array_diff($tables, $exclude); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$outputSql = ''; |
|
52
|
|
|
|
|
53
|
|
|
// Get the data for each object |
|
54
|
|
|
foreach($tables as $t) |
|
55
|
|
|
{ |
|
56
|
|
|
$sql = 'SELECT * FROM "'.trim($t).'"'; |
|
57
|
|
|
$res = $this->getDriver()->query($sql); |
|
58
|
|
|
$objRes = $res->fetchAll(\PDO::FETCH_ASSOC); |
|
59
|
|
|
|
|
60
|
|
|
// Don't add to the file if the table is empty |
|
61
|
|
|
if (count($objRes) < 1) |
|
62
|
|
|
{ |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$res = NULL; |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
// Nab the column names by getting the keys of the first row |
|
69
|
|
|
$columns = @array_keys($objRes[0]); |
|
70
|
|
|
|
|
71
|
|
|
$insertRows = []; |
|
72
|
|
|
|
|
73
|
|
|
// Create the insert statements |
|
74
|
|
|
foreach($objRes as $row) |
|
75
|
|
|
{ |
|
76
|
|
|
$row = array_values($row); |
|
77
|
|
|
|
|
78
|
|
|
// Quote values as needed by type |
|
79
|
|
|
$row = array_map([$this->getDriver(), 'quote'], $row); |
|
80
|
|
|
$row = array_map('trim', $row); |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$rowString = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; |
|
84
|
|
|
|
|
85
|
|
|
$row = NULL; |
|
86
|
|
|
|
|
87
|
|
|
$insertRows[] = $rowString; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$objRes = NULL; |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$outputSql .= "\n\n".implode("\n", $insertRows)."\n"; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $outputSql; |
|
96
|
|
|
} |
|
97
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.