1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BootPress\SQLite; |
4
|
|
|
|
5
|
|
|
use BootPress\Database\Component as Database; |
6
|
|
|
|
7
|
|
|
class Component extends Database |
8
|
|
|
{ |
9
|
|
|
public $fts; |
10
|
|
|
public $created = false; // whether or not this is a new database |
11
|
|
|
private $info = array(); |
12
|
|
|
|
13
|
49 |
|
public function __construct($file = null) |
14
|
|
|
{ |
15
|
49 |
|
if (is_null($file)) { |
16
|
2 |
|
$file = ':memory:'; |
17
|
2 |
|
$this->created = true; |
18
|
2 |
|
} else { |
19
|
48 |
|
if (!is_file($file)) { |
20
|
8 |
|
if (!is_dir(dirname($file))) { |
21
|
2 |
|
mkdir(dirname($file), 0755, true); |
22
|
2 |
|
} |
23
|
8 |
|
$this->created = true; |
24
|
8 |
|
} |
25
|
48 |
|
if (class_exists('BootPress\Page\Component') && class_exists('Symfony\Component\Yaml\Yaml')) { |
26
|
48 |
|
$page = \BootPress\Page\Component::html(); |
27
|
48 |
|
$databases = $page->file('databases.yml'); |
28
|
48 |
|
$yaml = (is_file($databases)) ? \Symfony\Component\Yaml\Yaml::parse(file_get_contents($databases)) : array(); |
29
|
48 |
|
if (!isset($yaml['sqlite'])) { |
30
|
1 |
|
$yaml['sqlite'] = array(); |
31
|
1 |
|
} |
32
|
48 |
|
if (!in_array($file, $yaml['sqlite'])) { |
33
|
7 |
|
$yaml['sqlite'][] = $file; |
34
|
7 |
|
ksort($yaml); |
35
|
7 |
|
sort($yaml['sqlite']); |
36
|
7 |
|
$yaml = \Symfony\Component\Yaml\Yaml::dump($yaml, 3); |
37
|
7 |
|
file_put_contents($databases, $yaml); |
38
|
7 |
|
} |
39
|
48 |
|
} |
40
|
|
|
} |
41
|
49 |
|
$this->driver($file); |
42
|
49 |
|
$this->connection = new \SQLite3($file); |
43
|
49 |
|
$this->connection->exec('PRAGMA foreign_keys = ON'); |
44
|
49 |
|
$this->fts = new Fts($this); |
45
|
49 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function __get($name) |
48
|
|
|
{ |
49
|
1 |
|
if ($name == 'connection') { |
50
|
1 |
|
return $this->connection; // not normally needed, but this allows you to close the database |
51
|
|
|
} |
52
|
1 |
|
} |
53
|
|
|
|
54
|
8 |
|
public function create($table, array $fields, $index = array(), array $changes = array()) |
55
|
|
|
{ |
56
|
8 |
|
$columns = array(); |
57
|
8 |
|
foreach ($fields as $name => $type) { |
58
|
8 |
|
$columns[] = (is_int($name)) ? $type : $name.' '.$type; |
59
|
8 |
|
} |
60
|
8 |
|
$columns = implode(", \n\t", $columns); |
61
|
8 |
|
$query = 'CREATE TABLE '.$table.' ('.$columns.')'; |
62
|
8 |
|
$executed = $this->info('tables', $table); |
63
|
|
|
// See http://www.sqlite.org/fileformat2.html - 2.5 Storage Of The SQL Database Schema |
64
|
8 |
|
if (preg_replace('/(\'|")/', '', $query) == preg_replace('/(\'|")/', '', $executed)) { |
65
|
1 |
|
$this->index($table, $index); // make sure they are all correct also |
66
|
1 |
|
return false; // the table has already been created in it's requested state |
67
|
|
|
} |
68
|
8 |
|
$this->info('tables', $table, $query); // to either add or update |
69
|
8 |
|
if ($executed) { // then this table is being altered in some way |
70
|
1 |
|
$this->index($table, ''); |
71
|
1 |
|
$this->alter($table, $fields, $changes, $columns); |
72
|
1 |
|
} else { |
73
|
8 |
|
$this->exec($query); // We should only get here once |
74
|
|
|
} |
75
|
8 |
|
$this->index($table, $index); |
76
|
|
|
|
77
|
8 |
|
return true; // the table has been created (or altered) |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function settings() |
81
|
|
|
{ |
82
|
1 |
|
switch (func_num_args()) { |
83
|
1 |
|
case 0: // they want it all |
84
|
1 |
|
return $this->info('settings'); |
85
|
|
|
break; |
|
|
|
|
86
|
1 |
|
case 1: // they want to retrieve a specific setting |
87
|
1 |
|
return $this->info('settings', func_get_arg(0)); |
88
|
|
|
break; |
|
|
|
|
89
|
1 |
|
case 2: // they want to establish a setting |
90
|
1 |
|
$update = false; |
91
|
1 |
|
list($setting, $value) = func_get_args(); |
92
|
1 |
|
$current = $this->info('settings', $setting); |
93
|
1 |
|
if ($value === false) { // then we don't want this in the database as "false" is the default value |
94
|
1 |
|
if ($current !== false) { |
95
|
1 |
|
unset($this->info['settings'][$setting]); |
96
|
1 |
|
$update = true; |
97
|
1 |
|
} |
98
|
1 |
|
} elseif ($current !== $value) { |
99
|
1 |
|
$this->info['settings'][$setting] = $value; |
100
|
1 |
|
$update = true; |
101
|
1 |
|
} |
102
|
1 |
|
if ($update) { |
103
|
1 |
|
$this->exec('UPDATE config SET settings = ?', serialize($this->info['settings'])); |
|
|
|
|
104
|
1 |
|
} |
105
|
1 |
|
break; |
106
|
1 |
|
} |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
// http://stackoverflow.com/questions/396748/ordering-by-the-order-of-values-in-a-sql-in-clause |
110
|
6 |
|
public function orderIn($field, array $ids) |
111
|
|
|
{ |
112
|
6 |
|
if (empty($ids)) { |
113
|
1 |
|
return ''; |
114
|
|
|
} |
115
|
6 |
|
$count = 1; |
116
|
6 |
|
$order = 'ORDER BY CASE '.$field; |
117
|
6 |
|
foreach ($ids as $id) { |
118
|
6 |
|
$order .= ' WHEN '.$id.' THEN '.$count++; |
119
|
6 |
|
} |
120
|
6 |
|
$order .= ' ELSE NULL END ASC'; |
121
|
|
|
|
122
|
6 |
|
return $order; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
public function recreate($file) |
126
|
|
|
{ |
127
|
1 |
|
if (is_file($file)) { |
128
|
1 |
|
return; |
129
|
|
|
} |
130
|
1 |
|
$virtual = $tables = $indexes = array(); |
131
|
1 |
|
if ($result = $this->query('SELECT type, name, sql FROM sqlite_master')) { |
132
|
1 |
|
while (list($type, $name, $sql) = $this->fetch($result)) { |
133
|
1 |
|
if (!empty($sql)) { |
134
|
|
|
switch ($type) { |
135
|
1 |
|
case 'table': |
136
|
1 |
|
$tables[$name] = $sql; |
137
|
1 |
|
break; |
138
|
1 |
|
case 'index': |
139
|
1 |
|
$indexes[] = $sql; |
140
|
1 |
|
break; |
141
|
|
|
} |
142
|
1 |
|
} |
143
|
1 |
|
} |
144
|
1 |
|
$this->close($result); |
145
|
1 |
|
} |
146
|
1 |
|
foreach ($tables as $name => $sql) { |
147
|
1 |
|
if (strpos($sql, 'VIRTUAL TABLE')) { |
148
|
1 |
|
$virtual[] = $name; |
149
|
1 |
|
} |
150
|
1 |
|
} |
151
|
1 |
|
foreach ($virtual as $table) { |
152
|
1 |
|
foreach ($tables as $name => $sql) { |
153
|
1 |
|
if (strpos($name, "{$table}_") === 0) { |
154
|
1 |
|
unset($tables[$name]); |
155
|
1 |
|
} |
156
|
1 |
|
} |
157
|
1 |
|
} |
158
|
1 |
|
$db = new self($file); |
159
|
1 |
|
$this->exec('ATTACH DATABASE '.$this->dbEscape($file).' AS recreate'); |
160
|
1 |
|
foreach ($tables as $table => $sql) { |
161
|
1 |
|
$db->connection()->exec($sql); |
162
|
1 |
|
if ($fields = $this->row('SELECT * FROM '.$table.' LIMIT 1', '', 'assoc')) { |
|
|
|
|
163
|
1 |
|
$fields = implode(', ', array_keys($fields)); |
164
|
1 |
|
$this->exec("INSERT INTO recreate.{$table} ({$fields}) SELECT * FROM {$table}"); |
165
|
1 |
|
} |
166
|
1 |
|
} |
167
|
1 |
|
foreach ($indexes as $sql) { |
168
|
1 |
|
$db->connection()->exec($sql); |
169
|
1 |
|
} |
170
|
1 |
|
$db->connection()->close(); |
171
|
1 |
|
} |
172
|
|
|
|
173
|
1 |
|
private function alter($table, array $fields, array $changes, $columns) |
174
|
|
|
{ |
175
|
1 |
|
$map = array(); |
176
|
1 |
|
if ($compare = $this->row('SELECT * FROM '.$table.' LIMIT 1', array(), 'assoc')) { |
177
|
1 |
|
foreach ($changes as $old => $new) { |
178
|
1 |
|
if (isset($fields[$new]) && isset($compare[$old])) { |
179
|
1 |
|
$map[$old] = $new; // legitimate changes |
180
|
1 |
|
} |
181
|
1 |
|
} |
182
|
1 |
|
foreach (array_keys($compare) as $field) { |
183
|
1 |
|
if (isset($fields[$field]) && !isset($map[$field])) { |
184
|
1 |
|
$map[$field] = $field; // old fields that match the new |
185
|
1 |
|
} |
186
|
1 |
|
} |
187
|
1 |
|
} |
188
|
1 |
|
$this->connection->exec('PRAGMA foreign_keys = OFF'); |
189
|
1 |
|
$this->connection->exec('BEGIN IMMEDIATE'); |
190
|
1 |
|
$result = true; |
191
|
1 |
|
if ($result !== false) { |
192
|
1 |
|
$result = $this->exec("CREATE TABLE {$table}_copy ({$columns})"); |
193
|
1 |
|
} |
194
|
1 |
|
if (!empty($map)) { |
195
|
1 |
|
$new = implode(', ', array_values($map)); |
196
|
1 |
|
$old = implode(', ', array_keys($map)); |
197
|
1 |
|
if ($result !== false) { |
198
|
1 |
|
$result = $this->exec("INSERT INTO {$table}_copy ({$new}) SELECT {$old} FROM {$table}"); |
199
|
1 |
|
} |
200
|
1 |
|
} |
201
|
1 |
|
if ($result !== false) { |
202
|
1 |
|
$result = $this->exec("DROP TABLE {$table}"); |
203
|
1 |
|
} |
204
|
1 |
|
if ($result !== false) { |
205
|
1 |
|
$result = $this->exec("ALTER TABLE {$table}_copy RENAME TO {$table}"); |
206
|
1 |
|
} |
207
|
1 |
|
$this->connection->exec($result !== false ? 'COMMIT' : 'ROLLBACK'); |
208
|
1 |
|
$this->connection->exec('PRAGMA foreign_keys = ON'); |
209
|
1 |
|
} |
210
|
|
|
|
211
|
8 |
|
private function index($table, $columns) |
212
|
|
|
{ |
213
|
8 |
|
$queries = array(); |
214
|
8 |
|
$outdated = $this->info('indexes', $table); |
215
|
8 |
|
if (empty($outdated)) { |
216
|
8 |
|
$outdated = array(); |
217
|
8 |
|
} |
218
|
8 |
|
if (!empty($columns)) { |
219
|
7 |
|
foreach ((array) $columns as $key => $indexes) { |
220
|
7 |
|
$unique = (!is_int($key) && strtolower($key) == 'unique') ? ' UNIQUE ' : ' '; |
221
|
7 |
|
$indexes = array_map('trim', explode(',', $indexes)); |
222
|
7 |
|
$name = $table.'_'.implode('_', $indexes); |
223
|
7 |
|
$sql = "CREATE{$unique}INDEX {$name} ON {$table} (".implode(', ', $indexes).')'; |
224
|
7 |
|
$queries[$name] = $sql; |
225
|
7 |
|
if (!isset($outdated[$name]) || $outdated[$name] != $sql) { |
226
|
7 |
|
if (isset($outdated[$name])) { |
227
|
1 |
|
$this->exec('DROP INDEX '.$name); |
228
|
1 |
|
} |
229
|
7 |
|
$this->exec($sql); |
230
|
7 |
|
} |
231
|
7 |
|
} |
232
|
7 |
|
foreach ($outdated as $name => $sql) { |
233
|
1 |
|
if (!isset($queries[$name])) { |
234
|
1 |
|
$this->exec('DROP INDEX '.$name); |
235
|
1 |
|
} |
236
|
7 |
|
} |
237
|
7 |
|
$this->info('indexes', $table, $queries); |
238
|
7 |
|
} |
239
|
8 |
|
} |
240
|
|
|
|
241
|
9 |
|
public function info($master) // only make public so that $this->fts can call it |
242
|
|
|
{ |
243
|
9 |
|
if ($master == 'settings') { |
244
|
1 |
|
if (!isset($this->info['settings'])) { |
245
|
1 |
|
if ($this->create('config', array('settings' => 'TEXT NOT NULL DEFAULT ""'))) { |
246
|
1 |
|
$this->exec('INSERT INTO config (settings) VALUES (?)', serialize(array())); |
|
|
|
|
247
|
1 |
|
} |
248
|
1 |
|
$this->info['settings'] = array(); |
249
|
1 |
|
if ($settings = $this->value('SELECT settings FROM config')) { |
250
|
1 |
|
$this->info['settings'] = unserialize($settings); |
251
|
1 |
|
} |
252
|
1 |
|
} |
253
|
9 |
|
} elseif (!isset($this->info[$master])) { // 'tables' or 'indexes' |
254
|
7 |
|
if ($result = $this->query('SELECT type, name, tbl_name, sql FROM sqlite_master')) { |
255
|
7 |
|
while (list($type, $name, $table, $sql) = $this->fetch($result)) { |
256
|
|
|
switch ($type) { |
257
|
7 |
|
case 'table': |
258
|
7 |
|
$this->info['tables'][$table] = $sql; |
259
|
7 |
|
break; |
260
|
7 |
|
case 'index': |
261
|
7 |
|
if (!empty($sql)) { |
262
|
7 |
|
$this->info['indexes'][$table][$name] = $sql; |
263
|
7 |
|
} |
264
|
7 |
|
break; |
265
|
|
|
} |
266
|
7 |
|
} |
267
|
7 |
|
$this->close($result); |
268
|
7 |
|
} |
269
|
7 |
|
} |
270
|
9 |
|
switch (func_num_args()) { |
271
|
9 |
|
case 3: |
272
|
9 |
|
list($master, $name, $add) = func_get_args(); |
273
|
9 |
|
$this->info[$master][$name] = $add; |
274
|
9 |
|
break; |
275
|
9 |
|
case 2: |
276
|
9 |
|
list($master, $name) = func_get_args(); |
277
|
|
|
|
278
|
9 |
|
return (isset($this->info[$master][$name])) ? $this->info[$master][$name] : false; |
279
|
|
|
break; |
|
|
|
|
280
|
1 |
|
case 1: |
281
|
1 |
|
list($master) = func_get_args(); |
282
|
|
|
|
283
|
1 |
|
return (isset($this->info[$master])) ? $this->info[$master] : false; |
284
|
|
|
break; |
|
|
|
|
285
|
9 |
|
} |
286
|
9 |
|
} |
287
|
|
|
|
288
|
56 |
|
protected function dbPrepare($query) |
289
|
|
|
{ |
290
|
|
|
try { |
291
|
56 |
|
return $this->connection->prepare($query); // returns (mixed) $stmt object or false |
292
|
2 |
|
} catch (\Exception $e) { |
293
|
2 |
|
return false; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
4 |
|
protected function dbPrepareError() |
298
|
|
|
{ |
299
|
4 |
|
return ($msg = $this->connection->lastErrorMsg()) ? 'Code: '.$this->connection->lastErrorCode()." Error: {$msg}" : false; // returns (string) error or false |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
55 |
|
protected function dbExecute($stmt, array $values, $reference) |
303
|
|
|
{ |
304
|
55 |
|
if (isset($this->prepared[$reference]['result'])) { |
305
|
8 |
|
$this->prepared[$reference]['result']->finalize(); |
306
|
8 |
|
unset($this->prepared[$reference]['result']); |
307
|
8 |
|
$stmt->reset(); |
308
|
8 |
|
} |
309
|
55 |
|
foreach (array_values($values) as $key => $value) { |
310
|
48 |
|
switch (gettype($value)) { |
311
|
48 |
|
case 'boolean': |
312
|
48 |
|
case 'integer': |
313
|
29 |
|
$type = SQLITE3_INTEGER; |
314
|
29 |
|
break; |
315
|
48 |
|
case 'double': |
316
|
1 |
|
$type = SQLITE3_FLOAT; |
317
|
1 |
|
break; |
318
|
48 |
|
case 'NULL': |
319
|
1 |
|
$type = SQLITE3_NULL; |
320
|
1 |
|
break; |
321
|
48 |
|
default: |
322
|
48 |
|
$type = SQLITE3_TEXT; |
323
|
48 |
|
break; |
324
|
48 |
|
} |
325
|
48 |
|
$stmt->bindValue($key + 1, $value, $type); |
326
|
55 |
|
} |
327
|
|
|
// Throws an ErrorException when a constraint fails eg. a (datatype mismatch)[https://www.sqlite.org/rescode.html#mismatch] |
328
|
|
|
try { |
329
|
55 |
|
if ($object = $stmt->execute()) { |
330
|
55 |
|
$this->prepared[$reference]['result'] = $object; |
331
|
55 |
|
} |
332
|
55 |
|
} catch (\Exception $e) { |
333
|
1 |
|
return false; |
334
|
|
|
} |
335
|
|
|
|
336
|
55 |
|
return ($object) ? true : false; // returns (bool) true or false |
337
|
|
|
} |
338
|
|
|
|
339
|
2 |
|
protected function dbExecuteError($stmt) |
340
|
|
|
{ |
341
|
2 |
|
return $this->dbPrepareError(); // returns (string) error or false |
342
|
|
|
} |
343
|
|
|
|
344
|
56 |
|
protected function dbStyle($fetch) |
345
|
|
|
{ |
346
|
|
|
switch ($fetch) { |
347
|
56 |
|
case 'assoc': |
348
|
50 |
|
return \SQLITE3_ASSOC; |
349
|
|
|
break; |
|
|
|
|
350
|
39 |
|
case 'both': |
351
|
1 |
|
return \SQLITE3_BOTH; |
352
|
|
|
break; |
|
|
|
|
353
|
39 |
|
default: |
354
|
39 |
|
return \SQLITE3_NUM; |
355
|
|
|
break; |
|
|
|
|
356
|
39 |
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
55 |
|
protected function dbFetch($stmt, $style, $reference) |
360
|
|
|
{ |
361
|
55 |
|
return (isset($this->prepared[$reference]['result'])) ? $this->prepared[$reference]['result']->fetchArray($style) : false; // returns (mixed) $style or false |
362
|
|
|
} |
363
|
|
|
|
364
|
16 |
|
protected function dbInserted() |
365
|
|
|
{ |
366
|
16 |
|
return $this->connection->lastInsertRowID(); // returns (int) last inserted row id or sequence value |
|
|
|
|
367
|
|
|
} |
368
|
|
|
|
369
|
23 |
|
protected function dbAffected($stmt) |
370
|
|
|
{ |
371
|
23 |
|
return $this->connection->changes(); // returns (int) number of rows affected by last $stmt |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
55 |
|
protected function dbClose($stmt, $reference) |
375
|
|
|
{ |
376
|
55 |
|
if (isset($this->prepared[$reference]['result'])) { |
377
|
55 |
|
$this->prepared[$reference]['result']->finalize(); |
378
|
55 |
|
$this->prepared[$reference]['result'] = null; |
379
|
55 |
|
} |
380
|
|
|
|
381
|
55 |
|
return $stmt->close(); // returns (bool) true or false |
382
|
|
|
} |
383
|
|
|
|
384
|
2 |
|
protected function dbEscape($string) |
385
|
|
|
{ |
386
|
2 |
|
return (is_numeric($string)) ? $string : "'".$this->connection->escapeString($string)."'"; |
|
|
|
|
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.