xref: /plugin/sqlite/SQLiteDB.php (revision a7a40fb292e55f99ed1ae34981feed1314e2696b)
1<?php
2
3/**
4 * @noinspection SqlNoDataSourceInspection
5 * @noinspection SqlDialectInspection
6 * @noinspection PhpComposerExtensionStubsInspection
7 */
8
9namespace dokuwiki\plugin\sqlite;
10
11use dokuwiki\Extension\Event;
12use dokuwiki\Logger;
13
14/**
15 * Helpers to access a SQLite Database with automatic schema migration
16 */
17class SQLiteDB
18{
19    public const FILE_EXTENSION = '.sqlite3';
20
21    /** @var \PDO */
22    protected $pdo;
23
24    /** @var string */
25    protected $schemadir;
26
27    /** @var string */
28    protected $dbname;
29
30    /** @var \helper_plugin_sqlite */
31    protected $helper;
32
33
34    /**
35     * Constructor
36     *
37     * @param string $dbname Database name
38     * @param string $schemadir directory with schema migration files
39     * @param \helper_plugin_sqlite $sqlitehelper for backwards compatibility
40     * @throws \Exception
41     */
42    public function __construct($dbname, $schemadir, $sqlitehelper = null)
43    {
44        if (!class_exists('pdo') || !in_array('sqlite', \PDO::getAvailableDrivers())) {
45            throw new \Exception('SQLite PDO driver not available');
46        }
47
48        // backwards compatibility, circular dependency
49        $this->helper = $sqlitehelper;
50        if (!$this->helper) {
51            $this->helper = new \helper_plugin_sqlite();
52        }
53        $this->helper->setAdapter($this);
54
55        $this->schemadir = $schemadir;
56        $this->dbname = $dbname;
57        $file = $this->getDbFile();
58
59        $this->pdo = new \PDO(
60            'sqlite:' . $file,
61            null,
62            null,
63            [
64                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
65                \PDO::ATTR_TIMEOUT => 10, // wait for locks up to 10 seconds
66            ]
67        );
68
69        try {
70            // See https://www.sqlite.org/wal.html
71            $this->exec('PRAGMA journal_mode=WAL');
72        } catch (\Exception $e) {
73            // this is not critical, but we log it as error. FIXME might be degraded to debug later
74            Logger::error('SQLite: Could not set WAL mode.', $e, $e->getFile(), $e->getLine());
75        }
76
77        if ($schemadir !== '') {
78            // schema dir is empty, when accessing the DB from Admin interface instead of plugin context
79            $this->applyMigrations();
80        }
81        Functions::register($this->pdo);
82    }
83
84    /**
85     * Do not serialize the DB connection
86     *
87     * @return array
88     */
89    public function __sleep()
90    {
91        $this->pdo = null;
92        return array_keys(get_object_vars($this));
93    }
94
95    /**
96     * On deserialization, reinit database connection
97     */
98    public function __wakeup()
99    {
100        $this->__construct($this->dbname, $this->schemadir, $this->helper);
101    }
102
103    // region public API
104
105    /**
106     * Direct access to the PDO object
107     * @return \PDO
108     */
109    public function getPdo()
110    {
111        return $this->pdo;
112    }
113
114    /**
115     * Execute a statement and return it
116     *
117     * @param string $sql
118     * @param ...mixed|array $parameters
119     * @return \PDOStatement Be sure to close the cursor yourself
120     * @throws \PDOException
121     */
122    public function query($sql, ...$parameters)
123    {
124        $start = microtime(true);
125
126        if ($parameters && is_array($parameters[0])) $parameters = $parameters[0];
127
128        // Statement preparation sometime throws ValueErrors instead of PDOExceptions, we streamline here
129        try {
130            $stmt = $this->pdo->prepare($sql);
131        } catch (\Throwable $e) {
132            throw new \PDOException($e->getMessage(), (int)$e->getCode(), $e);
133        }
134        $eventData = [
135            'sqlitedb' => $this,
136            'sql' => &$sql,
137            'parameters' => &$parameters,
138            'stmt' => $stmt
139        ];
140        $event = new Event('PLUGIN_SQLITE_QUERY_EXECUTE', $eventData);
141        if ($event->advise_before()) {
142            $stmt->execute($parameters);
143        }
144        $event->advise_after();
145
146        $time = microtime(true) - $start;
147        if ($time > 0.2) {
148            Logger::debug('[sqlite] slow query:  (' . $time . 's)', [
149                'sql' => $sql,
150                'parameters' => $parameters,
151                'backtrace' => explode("\n", dbg_backtrace())
152            ]);
153        }
154
155        return $stmt;
156    }
157
158    /**
159     * Execute a statement and return metadata
160     *
161     * Returns the last insert ID on INSERTs or the number of affected rows
162     *
163     * @param string $sql
164     * @param ...mixed|array $parameters
165     * @return int
166     * @throws \PDOException
167     */
168    public function exec($sql, ...$parameters)
169    {
170        $stmt = $this->query($sql, ...$parameters);
171
172        $count = $stmt->rowCount();
173        $stmt->closeCursor();
174        if ($count && preg_match('/^INSERT /i', $sql)) {
175            return $this->queryValue('SELECT last_insert_rowid()');
176        }
177
178        return $count;
179    }
180
181    /**
182     * Simple query abstraction
183     *
184     * Returns all data
185     *
186     * @param string $sql
187     * @param ...mixed|array $params
188     * @return array
189     * @throws \PDOException
190     */
191    public function queryAll($sql, ...$params)
192    {
193        $stmt = $this->query($sql, ...$params);
194        $data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
195        $stmt->closeCursor();
196        return $data;
197    }
198
199    /**
200     * Query one single row
201     *
202     * @param string $sql
203     * @param ...mixed|array $params
204     * @return array|null
205     * @throws \PDOException
206     */
207    public function queryRecord($sql, ...$params)
208    {
209        $stmt = $this->query($sql, ...$params);
210        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
211        $stmt->closeCursor();
212        if (is_array($row) && count($row)) {
213            return $row;
214        }
215        return null;
216    }
217
218    /**
219     * Insert or replace the given data into the table
220     *
221     * @param string $table
222     * @param array $data
223     * @param bool $replace Conflict resolution, replace or ignore
224     * @return array|null Either the inserted row or null if nothing was inserted
225     * @throws \PDOException
226     */
227    public function saveRecord($table, $data, $replace = true)
228    {
229        $columns = array_map(function ($column) {
230            return '"' . $column . '"';
231        }, array_keys($data));
232        $values = array_values($data);
233        $placeholders = array_pad([], count($columns), '?');
234
235        if ($replace) {
236            $command = 'REPLACE';
237        } else {
238            $command = 'INSERT OR IGNORE';
239        }
240
241        /** @noinspection SqlResolve */
242        $sql = $command . ' INTO "' . $table . '" (' . implode(',', $columns) . ') VALUES (' . implode(
243            ',',
244            $placeholders
245        ) . ')';
246        $stm = $this->query($sql, $values);
247        $success = $stm->rowCount();
248        $stm->closeCursor();
249
250        if ($success) {
251            $sql = 'SELECT * FROM "' . $table . '" WHERE rowid = last_insert_rowid()';
252            return $this->queryRecord($sql);
253        }
254        return null;
255    }
256
257    /**
258     * Execute a query that returns a single value
259     *
260     * @param string $sql
261     * @param ...mixed|array $params
262     * @return mixed|null
263     * @throws \PDOException
264     */
265    public function queryValue($sql, ...$params)
266    {
267        $result = $this->queryAll($sql, ...$params);
268        if (is_array($result) && count($result)) {
269            return array_values($result[0])[0];
270        }
271        return null;
272    }
273
274    /**
275     * Execute a query that returns a list of key-value pairs
276     *
277     * The first column is used as key, the second as value. Any additional colums are ignored.
278     *
279     * @param string $sql
280     * @param ...mixed|array $params
281     * @return array
282     */
283    public function queryKeyValueList($sql, ...$params)
284    {
285        $result = $this->queryAll($sql, ...$params);
286        if (!$result) return [];
287        if (count(array_keys($result[0])) != 2) {
288            throw new \RuntimeException('queryKeyValueList expects a query that returns exactly two columns');
289        }
290        [$key, $val] = array_keys($result[0]);
291
292        return array_combine(
293            array_column($result, $key),
294            array_column($result, $val)
295        );
296    }
297
298    // endregion
299
300    // region meta handling
301
302    /**
303     * Get a config value from the opt table
304     *
305     * @param string $opt Config name
306     * @param mixed $default What to return if the value isn't set
307     * @return mixed
308     * @throws \PDOException
309     */
310    public function getOpt($opt, $default = null)
311    {
312        $value = $this->queryValue("SELECT val FROM opts WHERE opt = ?", [$opt]);
313        if ($value === null) {
314            return $default;
315        }
316        return $value;
317    }
318
319    /**
320     * Set a config value in the opt table
321     *
322     * @param $opt
323     * @param $value
324     * @throws \PDOException
325     */
326    public function setOpt($opt, $value)
327    {
328        $this->exec('REPLACE INTO opts (opt,val) VALUES (?,?)', [$opt, $value]);
329    }
330
331    /**
332     * @return string
333     */
334    public function getDbName()
335    {
336        return $this->dbname;
337    }
338
339    /**
340     * @return string
341     */
342    public function getDbFile()
343    {
344        global $conf;
345        return $conf['metadir'] . '/' . $this->dbname . self::FILE_EXTENSION;
346    }
347
348    /**
349     * Create a dump of the database and its contents
350     *
351     * @return string
352     * @throws \Exception
353     */
354    public function dumpToFile($filename)
355    {
356        $fp = fopen($filename, 'w');
357        if (!$fp) {
358            throw new \Exception('Could not open file ' . $filename . ' for writing');
359        }
360
361        $tables = $this->queryAll("SELECT name,sql FROM sqlite_master WHERE type='table'");
362        $indexes = $this->queryAll("SELECT name,sql FROM sqlite_master WHERE type='index'");
363
364        foreach ($tables as $table) {
365            fwrite($fp, "DROP TABLE IF EXISTS '{$table['name']}';\n");
366        }
367
368        foreach ($tables as $table) {
369            fwrite($fp, $table['sql'] . ";\n");
370        }
371
372        foreach ($tables as $table) {
373            $sql = "SELECT * FROM " . $table['name'];
374            $res = $this->query($sql);
375            while ($row = $res->fetch(\PDO::FETCH_ASSOC)) {
376                $values = implode(',', array_map(function ($value) {
377                    if ($value === null) return 'NULL';
378                    return $this->pdo->quote($value);
379                }, $row));
380                fwrite($fp, "INSERT INTO '{$table['name']}' VALUES ({$values});\n");
381            }
382            $res->closeCursor();
383        }
384
385        foreach ($indexes as $index) {
386            fwrite($fp, $index['sql'] . ";\n");
387        }
388        fclose($fp);
389        return $filename;
390    }
391
392    // endregion
393
394    // region migration handling
395
396    /**
397     * Apply all pending migrations
398     *
399     * Each migration is executed in a transaction which is rolled back on failure
400     * Migrations can be files in the schema directory or event handlers
401     *
402     * @throws \Exception
403     */
404    protected function applyMigrations()
405    {
406        $currentVersion = $this->currentDbVersion();
407        $latestVersion = $this->latestDbVersion();
408
409        if ($currentVersion === $latestVersion) return;
410
411        for ($newVersion = $currentVersion + 1; $newVersion <= $latestVersion; $newVersion++) {
412            $data = [
413                'dbname' => $this->dbname,
414                'from' => $currentVersion,
415                'to' => $newVersion,
416                'file' => $this->getMigrationFile($newVersion),
417                'sqlite' => $this->helper,
418                'adapter' => $this,
419            ];
420            $event = new Event('PLUGIN_SQLITE_DATABASE_UPGRADE', $data);
421
422            $this->pdo->beginTransaction();
423            try {
424                if ($event->advise_before()) {
425                    // standard migration file
426                    $sql = Tools::SQLstring2array(file_get_contents($data['file']));
427                    foreach ($sql as $query) {
428                        $this->pdo->exec($query);
429                    }
430                } elseif (!$event->result) {
431                    // advise before returned false, but the result was false
432                    throw new \PDOException('Plugin event did not signal success');
433                }
434                $this->setOpt('dbversion', $newVersion);
435                $this->pdo->commit();
436                $event->advise_after();
437            } catch (\Exception $e) {
438                // something went wrong, rollback
439                $this->pdo->rollBack();
440                throw $e;
441            }
442        }
443
444        // vacuum the database to free up unused space
445        $this->pdo->exec('VACUUM');
446    }
447
448    /**
449     * Read the current version from the opt table
450     *
451     * The opt table is created here if not found
452     *
453     * @return int
454     * @throws \PDOException
455     */
456    protected function currentDbVersion()
457    {
458        try {
459            $version = $this->getOpt('dbversion', 0);
460            return (int)$version;
461        } catch (\PDOException $e) {
462            if (!preg_match('/no such table/', $e->getMessage())) {
463                // if this is not a "no such table" error, there is something wrong see #80
464                Logger::error(
465                    'SQLite: Could not read dbversion from opt table due to unexpected error',
466                    [
467                        'dbname' => $this->dbname,
468                        'exception' => get_class($e),
469                        'message' => $e->getMessage(),
470                        'code' => $e->getCode(),
471                    ],
472                    __FILE__,
473                    __LINE__
474                );
475            }
476
477            // add the opt table - if this fails too, let the exception bubble up
478            $sql = "CREATE TABLE IF NOT EXISTS opts (opt TEXT NOT NULL PRIMARY KEY, val NOT NULL DEFAULT '')";
479            $this->exec($sql);
480            return 0;
481        }
482    }
483
484    /**
485     * Get the version this db should have
486     *
487     * @return int
488     * @throws \PDOException
489     */
490    protected function latestDbVersion()
491    {
492        if (!file_exists($this->schemadir . '/latest.version')) {
493            throw new \PDOException('No latest.version in schema dir');
494        }
495        return (int)trim(file_get_contents($this->schemadir . '/latest.version'));
496    }
497
498    /**
499     * Get the migrartion file for the given version
500     *
501     * @param int $version
502     * @return string
503     */
504    protected function getMigrationFile($version)
505    {
506        return sprintf($this->schemadir . '/update%04d.sql', $version);
507    }
508    // endregion
509}
510