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