|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Core\DatabaseManager; |
|
4
|
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\DatabaseManager; |
|
6
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Executor; |
|
7
|
|
|
use Db3v4l\Core\SqlAction\Command; |
|
8
|
|
|
|
|
9
|
|
|
class SQLite extends BaseManager implements DatabaseManager |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Returns the sql 'action' used to list all available databases |
|
13
|
|
|
* @return Command |
|
|
|
|
|
|
14
|
|
|
* @todo for each database, retrieve the charset/collation |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
public function getListDatabasesSqlAction() |
|
17
|
|
|
{ |
|
18
|
|
|
$fileGlob = dirname($this->databaseConfiguration['path']) . '/*.sqlite'; |
|
19
|
|
|
return new Command( |
|
20
|
|
|
null, |
|
21
|
|
|
function() use ($fileGlob) { |
|
|
|
|
|
|
22
|
|
|
$out = []; |
|
23
|
|
|
foreach (glob($fileGlob) as $filename) { |
|
24
|
|
|
$out[] = basename($filename); |
|
25
|
|
|
} |
|
26
|
|
|
return $out; |
|
27
|
|
|
} |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns the sql 'action' used to create a new db and accompanying user |
|
33
|
|
|
* @param string $dbName |
|
|
|
|
|
|
34
|
|
|
* @param string $userName |
|
|
|
|
|
|
35
|
|
|
* @param string $password |
|
|
|
|
|
|
36
|
|
|
* @param string $charset charset/collation name |
|
|
|
|
|
|
37
|
|
|
* @return Command |
|
|
|
|
|
|
38
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null) |
|
41
|
|
|
{ |
|
42
|
|
|
//$collation = $this->getCollationName($charset); |
|
43
|
|
|
|
|
44
|
|
|
/// @todo throw if $charset is not supported |
|
45
|
|
|
|
|
46
|
|
|
/// @todo this does not support creation of the new db with a different character encoding... |
|
47
|
|
|
/// see https://stackoverflow.com/questions/21348459/set-pragma-encoding-utf-16-for-main-database-in-sqlite |
|
48
|
|
|
$filename = dirname($this->databaseConfiguration['path']) . '/' . $dbName . '.sqlite'; |
|
49
|
|
|
return new Command( |
|
50
|
|
|
"ATTACH '$filename' AS \"$dbName\";" |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the sql 'action' used to drop a db and associated user account |
|
56
|
|
|
* @param string $dbName |
|
|
|
|
|
|
57
|
|
|
* @param string $userName |
|
|
|
|
|
|
58
|
|
|
* @return Command |
|
|
|
|
|
|
59
|
|
|
* @param bool $ifExists |
|
|
|
|
|
|
60
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false) |
|
63
|
|
|
{ |
|
64
|
|
|
$filename = dirname($this->databaseConfiguration['path']) . '/' . $dbName . '.sqlite'; |
|
65
|
|
|
return new Command( |
|
66
|
|
|
null, |
|
67
|
|
|
function() use($filename, $dbName, $ifExists) { |
|
|
|
|
|
|
68
|
|
|
if (is_file($filename)) { |
|
69
|
|
|
unlink($filename); |
|
70
|
|
|
} else { |
|
71
|
|
|
if (!$ifExists) { |
|
72
|
|
|
throw new \Exception("Can not drop database '$dbName': file not found"); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the sql 'action' used to list all existing db users |
|
81
|
|
|
* @return Command |
|
|
|
|
|
|
82
|
|
|
*/ |
|
83
|
|
|
public function getListUsersSqlAction() |
|
84
|
|
|
{ |
|
85
|
|
|
return new Command( |
|
86
|
|
|
null, |
|
87
|
|
|
function () { |
|
88
|
|
|
// since sqlite does not support users, null seems more appropriate than an empty array... |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
|
|
|
|
|
95
|
|
|
* @param string $userName |
|
|
|
|
|
|
96
|
|
|
* @param bool $ifExists |
|
|
|
|
|
|
97
|
|
|
* @return Command |
|
|
|
|
|
|
98
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
99
|
|
|
*/ |
|
100
|
|
|
public function getDropUserSqlAction($userName, $ifExists = false) |
|
101
|
|
|
{ |
|
102
|
|
|
return new Command( |
|
103
|
|
|
null, |
|
104
|
|
|
function() { |
|
|
|
|
|
|
105
|
|
|
/// @todo should we return something ? |
|
106
|
|
|
} |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the sql 'action' used to list all available collations |
|
112
|
|
|
* @return Command |
|
|
|
|
|
|
113
|
|
|
*/ |
|
114
|
|
|
public function getListCollationsSqlAction() |
|
115
|
|
|
{ |
|
116
|
|
|
return new Command( |
|
117
|
|
|
null, |
|
118
|
|
|
/// @todo list the supported utf16 variants as soon as allow using them |
|
119
|
|
|
function () { |
|
120
|
|
|
return []; |
|
121
|
|
|
} |
|
122
|
|
|
); |
|
123
|
|
|
/*return [ |
|
124
|
|
|
// q: are these comparable to other databases ? we probably should instead list the values for https://www.sqlite.org/pragma.html#pragma_encoding |
|
125
|
|
|
'PRAGMA collation_list;', |
|
126
|
|
|
function ($output, $executor) { |
|
127
|
|
|
$out = []; |
|
128
|
|
|
foreach(explode("\n", $output) as $line) { |
|
129
|
|
|
$out[] = explode("|", $line, 2)[1]; |
|
130
|
|
|
} |
|
131
|
|
|
sort($out); |
|
132
|
|
|
return implode("\n", $out); |
|
133
|
|
|
} |
|
134
|
|
|
];*/ |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the sql 'action' used to retrieve the db instance version info |
|
139
|
|
|
* @return Command |
|
|
|
|
|
|
140
|
|
|
*/ |
|
141
|
|
|
public function getRetrieveVersionInfoSqlAction() |
|
142
|
|
|
{ |
|
143
|
|
|
return new Command( |
|
144
|
|
|
"select sqlite_version();", |
|
145
|
|
|
function ($output, $executor) { |
|
146
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
147
|
|
|
return $executor->resultSetToArray($output)[0]; |
|
148
|
|
|
} |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Transform collation name into a supported one |
|
154
|
|
|
* @param null|string $charset so far only 'utf8' is supported... |
|
|
|
|
|
|
155
|
|
|
* @return null|string |
|
|
|
|
|
|
156
|
|
|
* @todo what shall we accept as valid input, ie. 'generic' charset names ? maybe do 2 passes: known-db-charset => generic => specific for each db ? |
|
|
|
|
|
|
157
|
|
|
* see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names |
|
158
|
|
|
*/ |
|
159
|
|
|
/*protected function getCollationName($charset) |
|
160
|
|
|
{ |
|
161
|
|
|
if ($charset == null) { |
|
162
|
|
|
return null; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$charset = trim(strtolower($charset)); |
|
166
|
|
|
|
|
167
|
|
|
// accept official iana charset name, but most dbs prefer 'utf8'... |
|
168
|
|
|
if ($charset == 'utf-8') { |
|
169
|
|
|
$charset = 'utf8'; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $charset; |
|
173
|
|
|
}*/ |
|
174
|
|
|
} |
|
175
|
|
|
|