1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Query |
4
|
|
|
* |
5
|
|
|
* SQL Query Builder / Database Abstraction Layer |
6
|
|
|
* |
7
|
|
|
* PHP version 7 |
8
|
|
|
* |
9
|
|
|
* @package Query |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2012 - 2016 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
|
|
|
* @package Query |
24
|
|
|
* @subpackage Drivers |
25
|
|
|
* @method mixed query(string $sql) |
26
|
|
|
* @method string quote(string $str) |
27
|
|
|
*/ |
28
|
|
|
class Util extends AbstractUtil { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create an SQL backup file for the current database's data |
32
|
|
|
* |
33
|
|
|
* @param array $excluded |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function backupData($excluded=[]) |
37
|
|
|
{ |
38
|
|
|
// Get a list of all the objects |
39
|
|
|
$sql = 'SELECT DISTINCT "name" |
40
|
|
|
FROM "sqlite_master" |
41
|
|
|
WHERE "type"=\'table\''; |
42
|
|
|
|
43
|
|
|
if( ! empty($excluded)) |
44
|
|
|
{ |
45
|
|
|
$sql .= " AND \"name\" NOT IN('".implode("','", $excluded)."')"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$res = $this->getDriver()->query($sql); |
|
|
|
|
49
|
|
|
$result = $res->fetchAll(PDO::FETCH_ASSOC); |
50
|
|
|
|
51
|
|
|
unset($res); |
52
|
|
|
|
53
|
|
|
$outputSql = ''; |
54
|
|
|
|
55
|
|
|
// Get the data for each object |
56
|
|
|
foreach($result as $r) |
57
|
|
|
{ |
58
|
|
|
$sql = 'SELECT * FROM "'.$r['name'].'"'; |
59
|
|
|
$res = $this->getDriver()->query($sql); |
|
|
|
|
60
|
|
|
$objRes = $res->fetchAll(PDO::FETCH_ASSOC); |
61
|
|
|
|
62
|
|
|
unset($res); |
63
|
|
|
|
64
|
|
|
// If the row is empty, continue; |
65
|
|
|
if (empty($objRes)) |
66
|
|
|
{ |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Nab the column names by getting the keys of the first row |
71
|
|
|
$columns = array_keys(current($objRes)); |
72
|
|
|
|
73
|
|
|
$insertRows = []; |
74
|
|
|
|
75
|
|
|
// Create the insert statements |
76
|
|
|
foreach($objRes as $row) |
77
|
|
|
{ |
78
|
|
|
$row = array_values($row); |
79
|
|
|
|
80
|
|
|
// Quote values as needed by type |
81
|
|
|
for($i=0, $icount=count($row); $i<$icount; $i++) |
82
|
|
|
{ |
83
|
|
|
$row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->getDriver()->quote($row[$i]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$rowString = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; |
87
|
|
|
|
88
|
|
|
unset($row); |
89
|
|
|
|
90
|
|
|
$insertRows[] = $rowString; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
unset($objRes); |
94
|
|
|
|
95
|
|
|
$outputSql .= "\n\n".implode("\n", $insertRows); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $outputSql; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create an SQL backup file for the current database's structure |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function backupStructure() |
107
|
|
|
{ |
108
|
|
|
// Fairly easy for SQLite...just query the master table |
109
|
|
|
$sql = 'SELECT "sql" FROM "sqlite_master"'; |
110
|
|
|
$res = $this->getDriver()->query($sql); |
|
|
|
|
111
|
|
|
$result = $res->fetchAll(PDO::FETCH_ASSOC); |
112
|
|
|
|
113
|
|
|
$sqlArray = []; |
114
|
|
|
|
115
|
|
|
foreach($result as $r) |
116
|
|
|
{ |
117
|
|
|
$sqlArray[] = $r['sql']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return implode(";\n", $sqlArray) . ";"; |
121
|
|
|
} |
122
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.