1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Query |
4
|
|
|
* |
5
|
|
|
* Free Query Builder / Database Abstraction Layer |
6
|
|
|
* |
7
|
|
|
* @package Query |
8
|
|
|
* @author Timothy J. Warren |
9
|
|
|
* @copyright Copyright (c) 2012 - 2015 |
10
|
|
|
* @link https://github.com/aviat4ion/Query |
11
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
// -------------------------------------------------------------------------- |
15
|
|
|
|
16
|
|
|
namespace Query\Drivers\Sqlite; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SQLite-specific backup, import and creation methods |
20
|
|
|
* |
21
|
|
|
* @package Query |
22
|
|
|
* @subpackage Drivers |
23
|
|
|
* @method mixed query(string $sql) |
24
|
|
|
* @method string quote(string $str) |
25
|
|
|
*/ |
26
|
|
|
class Util extends \Query\AbstractUtil { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create an SQL backup file for the current database's data |
30
|
|
|
* |
31
|
|
|
* @param array $excluded |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function backup_data($excluded=array()) |
35
|
|
|
{ |
36
|
|
|
// Get a list of all the objects |
37
|
|
|
$sql = 'SELECT DISTINCT "name" |
38
|
|
|
FROM "sqlite_master" |
39
|
|
|
WHERE "type"=\'table\''; |
40
|
|
|
|
41
|
|
|
if( ! empty($excluded)) |
42
|
|
|
{ |
43
|
|
|
$sql .= " AND \"name\" NOT IN('".implode("','", $excluded)."')"; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$res = $this->get_driver()->query($sql); |
47
|
|
|
$result = $res->fetchAll(\PDO::FETCH_ASSOC); |
48
|
|
|
|
49
|
|
|
unset($res); |
50
|
|
|
|
51
|
|
|
$output_sql = ''; |
52
|
|
|
|
53
|
|
|
// Get the data for each object |
54
|
|
|
foreach($result as $r) |
55
|
|
|
{ |
56
|
|
|
$sql = 'SELECT * FROM "'.$r['name'].'"'; |
57
|
|
|
$res = $this->get_driver()->query($sql); |
58
|
|
|
$obj_res = $res->fetchAll(\PDO::FETCH_ASSOC); |
59
|
|
|
|
60
|
|
|
unset($res); |
61
|
|
|
|
62
|
|
|
// If the row is empty, continue; |
63
|
|
|
if (empty($obj_res)) |
64
|
|
|
{ |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Nab the column names by getting the keys of the first row |
69
|
|
|
$columns = array_keys(current($obj_res)); |
70
|
|
|
|
71
|
|
|
$insert_rows = array(); |
72
|
|
|
|
73
|
|
|
// Create the insert statements |
74
|
|
|
foreach($obj_res as $row) |
75
|
|
|
{ |
76
|
|
|
$row = array_values($row); |
77
|
|
|
|
78
|
|
|
// Quote values as needed by type |
79
|
|
|
for($i=0, $icount=count($row); $i<$icount; $i++) |
80
|
|
|
{ |
81
|
|
|
$row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->get_driver()->quote($row[$i]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$row_string = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; |
85
|
|
|
|
86
|
|
|
unset($row); |
87
|
|
|
|
88
|
|
|
$insert_rows[] = $row_string; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
unset($obj_res); |
92
|
|
|
|
93
|
|
|
$output_sql .= "\n\n".implode("\n", $insert_rows); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $output_sql; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// -------------------------------------------------------------------------- |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create an SQL backup file for the current database's structure |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function backup_structure() |
107
|
|
|
{ |
108
|
|
|
// Fairly easy for SQLite...just query the master table |
109
|
|
|
$sql = 'SELECT "sql" FROM "sqlite_master"'; |
110
|
|
|
$res = $this->get_driver()->query($sql); |
111
|
|
|
$result = $res->fetchAll(\PDO::FETCH_ASSOC); |
112
|
|
|
|
113
|
|
|
$sql_array = array(); |
114
|
|
|
|
115
|
|
|
foreach($result as $r) |
116
|
|
|
{ |
117
|
|
|
$sql_array[] = $r['sql']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return implode(";\n", $sql_array) . ";"; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
// End of sqlite_util.php |