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