xref: /plugin/sqlite/helper.php (revision 6917135c2bad18478af79a48489f4dd3c0f37a76)
1a1e6784eSAndreas Gohr<?php
28da7d805SAndreas Gohr
38da7d805SAndreas Gohr/**
48da7d805SAndreas Gohr * @noinspection SqlNoDataSourceInspection
58da7d805SAndreas Gohr * @noinspection SqlDialectInspection
68da7d805SAndreas Gohr * @noinspection PhpComposerExtensionStubsInspection
78da7d805SAndreas Gohr */
88da7d805SAndreas Gohr
98da7d805SAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB;
108da7d805SAndreas Gohruse dokuwiki\plugin\sqlite\Tools;
118da7d805SAndreas Gohr
128da7d805SAndreas Gohr
13801b921eSSzymon Olewniczak
14801b921eSSzymon Olewniczak/**
15801b921eSSzymon Olewniczak * For compatibility with previous adapter implementation.
16801b921eSSzymon Olewniczak */
17801b921eSSzymon Olewniczakif(!defined('DOKU_EXT_PDO')) define('DOKU_EXT_PDO', 'pdo');
18801b921eSSzymon Olewniczakclass helper_plugin_sqlite_adapter_dummy
19801b921eSSzymon Olewniczak{
20801b921eSSzymon Olewniczak    public function getName() {
21801b921eSSzymon Olewniczak        return DOKU_EXT_PDO;
22801b921eSSzymon Olewniczak    }
23801b921eSSzymon Olewniczak
24801b921eSSzymon Olewniczak    public function setUseNativeAlter($set) {}
25801b921eSSzymon Olewniczak}
26801b921eSSzymon Olewniczak
27a1e6784eSAndreas Gohr/**
28a1e6784eSAndreas Gohr * DokuWiki Plugin sqlite (Helper Component)
29a1e6784eSAndreas Gohr *
30a1e6784eSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
31a1e6784eSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
328da7d805SAndreas Gohr * @deprecated 2023-03-15
33a1e6784eSAndreas Gohr */
348da7d805SAndreas Gohrclass helper_plugin_sqlite extends DokuWiki_Plugin
358da7d805SAndreas Gohr{
368da7d805SAndreas Gohr    /** @var SQLiteDB|null */
376c7ee3f2SAndreas Gohr    protected $adapter = null;
38aa81d781SKlap-in
398da7d805SAndreas Gohr    /** @var array result cache */
408da7d805SAndreas Gohr    protected $data;
418da7d805SAndreas Gohr
423e9ac593SGerrit Uitslag    /**
438da7d805SAndreas Gohr     * constructor
443e9ac593SGerrit Uitslag     */
458da7d805SAndreas Gohr    public function __construct()
468da7d805SAndreas Gohr    {
478da7d805SAndreas Gohr        if (!$this->existsPDOSqlite()) {
488da7d805SAndreas Gohr            msg('PDO SQLite support missing in this PHP install - The sqlite plugin will not work', -1);
498da7d805SAndreas Gohr        }
50801b921eSSzymon Olewniczak        $this->adapter = new helper_plugin_sqlite_adapter_dummy();
518da7d805SAndreas Gohr    }
528da7d805SAndreas Gohr
538da7d805SAndreas Gohr    /**
548da7d805SAndreas Gohr     * Get the current Adapter
558da7d805SAndreas Gohr     * @return SQLiteDB|null
568da7d805SAndreas Gohr     */
578da7d805SAndreas Gohr    public function getAdapter()
588da7d805SAndreas Gohr    {
59ca2f2adaSKlap-in        return $this->adapter;
60ca2f2adaSKlap-in    }
61ca2f2adaSKlap-in
62a1e6784eSAndreas Gohr    /**
63aa81d781SKlap-in     * Keep separate instances for every call to keep database connections
64aa81d781SKlap-in     */
658da7d805SAndreas Gohr    public function isSingleton()
668da7d805SAndreas Gohr    {
67aa81d781SKlap-in        return false;
68aa81d781SKlap-in    }
69aa81d781SKlap-in
70aa81d781SKlap-in    /**
71aa81d781SKlap-in     * check availabilty of PHP PDO sqlite3
72aa81d781SKlap-in     */
738da7d805SAndreas Gohr    public function existsPDOSqlite()
748da7d805SAndreas Gohr    {
7587fa2c18Sstretchyboy        if (class_exists('pdo')) {
768da7d805SAndreas Gohr            return in_array('sqlite', \PDO::getAvailableDrivers());
777ed6069fSAdrian Lang        }
78aa81d781SKlap-in        return false;
79a1e6784eSAndreas Gohr    }
80a1e6784eSAndreas Gohr
81a1e6784eSAndreas Gohr    /**
82a1e6784eSAndreas Gohr     * Initializes and opens the database
83a1e6784eSAndreas Gohr     *
84a1e6784eSAndreas Gohr     * Needs to be called right after loading this helper plugin
85aa81d781SKlap-in     *
86aa81d781SKlap-in     * @param string $dbname
87aa81d781SKlap-in     * @param string $updatedir - Database update infos
88aa81d781SKlap-in     * @return bool
89a1e6784eSAndreas Gohr     */
908da7d805SAndreas Gohr    public function init($dbname, $updatedir)
918da7d805SAndreas Gohr    {
92801b921eSSzymon Olewniczak        if(!defined('DOKU_UNITTEST')) { // for now we don't want to trigger the deprecation warning in the tests
933a56750bSAndreas Gohr            dbg_deprecated(SQLiteDB::class);
94d0a5ba7aSAndreas Gohr        }
953a56750bSAndreas Gohr
968da7d805SAndreas Gohr        try {
978da7d805SAndreas Gohr            $this->adapter = new SQLiteDB($dbname, $updatedir, $this);
988da7d805SAndreas Gohr        } catch (Exception $e) {
998da7d805SAndreas Gohr            msg('SQLite: ' . $e->getMessage(), -1);
100c137e95fSAndreas Gohr            return false;
101c137e95fSAndreas Gohr        }
102a1e6784eSAndreas Gohr        return true;
103a1e6784eSAndreas Gohr    }
104a1e6784eSAndreas Gohr
105a1e6784eSAndreas Gohr    /**
1068da7d805SAndreas Gohr     * This is called from the adapter itself for backwards compatibility
1078da7d805SAndreas Gohr     *
1088da7d805SAndreas Gohr     * @param SQLiteDB $adapter
1098da7d805SAndreas Gohr     * @return void
110a1e6784eSAndreas Gohr     */
1118da7d805SAndreas Gohr    function setAdapter($adapter)
1128da7d805SAndreas Gohr    {
1138da7d805SAndreas Gohr        $this->adapter = $adapter;
114a34ef333SKlap-in    }
115f10ea6c1SKlap-in
116a1e6784eSAndreas Gohr    /**
1173ae3f79eSKlap-in     * Registers a User Defined Function for use in SQL statements
1183ae3f79eSKlap-in     */
1198da7d805SAndreas Gohr    public function create_function($function_name, $callback, $num_args)
1208da7d805SAndreas Gohr    {
1218da7d805SAndreas Gohr        $this->adapter->pdo()->sqliteCreateFunction($function_name, $callback, $num_args);
1223ae3f79eSKlap-in    }
1233ae3f79eSKlap-in
1248da7d805SAndreas Gohr    // region query and result handling functions
1258da7d805SAndreas Gohr
1263ae3f79eSKlap-in    /**
127e7b0736cSAndreas Gohr     * Convenience function to run an INSERT OR REPLACE operation
128e7b0736cSAndreas Gohr     *
129e7b0736cSAndreas Gohr     * The function takes a key-value array with the column names in the key and the actual value in the value,
130e7b0736cSAndreas Gohr     * build the appropriate query and executes it.
131e7b0736cSAndreas Gohr     *
132e7b0736cSAndreas Gohr     * @param string $table the table the entry should be saved to (will not be escaped)
133e7b0736cSAndreas Gohr     * @param array $entry A simple key-value pair array (only values will be escaped)
1348da7d805SAndreas Gohr     * @return bool
135e7b0736cSAndreas Gohr     */
1368da7d805SAndreas Gohr    public function storeEntry($table, $entry)
1378da7d805SAndreas Gohr    {
1388da7d805SAndreas Gohr        try {
1398da7d805SAndreas Gohr            $this->adapter->saveRecord($table, $entry);
1408da7d805SAndreas Gohr        } catch (\Exception $e) {
1418da7d805SAndreas Gohr            msg('SQLite: ' . $e->getMessage(), -1);
1428da7d805SAndreas Gohr            return false;
143e7b0736cSAndreas Gohr        }
144e7b0736cSAndreas Gohr
1458da7d805SAndreas Gohr        return true;
1468da7d805SAndreas Gohr    }
147e7b0736cSAndreas Gohr
148e7b0736cSAndreas Gohr    /**
149a1e6784eSAndreas Gohr     * Execute a query with the given parameters.
150a1e6784eSAndreas Gohr     *
151a1e6784eSAndreas Gohr     * Takes care of escaping
152a1e6784eSAndreas Gohr     *
153a2a82480SAndreas Gohr     *
154a2a82480SAndreas Gohr     * @param string ...$args - the arguments of query(), the first is the sql and others are values
155a1e6784eSAndreas Gohr     */
1568da7d805SAndreas Gohr    public function query()
1578da7d805SAndreas Gohr    {
158a1e6784eSAndreas Gohr        // get function arguments
159a1e6784eSAndreas Gohr        $args = func_get_args();
1608da7d805SAndreas Gohr        $sql = array_shift($args);
161a1e6784eSAndreas Gohr
162*6917135cSSzymon Olewniczak        // check if args contains single array
163*6917135cSSzymon Olewniczak        if (isset($args[0]) && is_array($args[0])) {
164*6917135cSSzymon Olewniczak            $args = $args[0];
165*6917135cSSzymon Olewniczak        }
166*6917135cSSzymon Olewniczak
167*6917135cSSzymon Olewniczak        // clear the cache
168*6917135cSSzymon Olewniczak        $this->data = null;
169*6917135cSSzymon Olewniczak
1708da7d805SAndreas Gohr        try {
1718da7d805SAndreas Gohr            return $this->adapter->query($sql, $args);
1728da7d805SAndreas Gohr        } catch (\Exception $e) {
1738da7d805SAndreas Gohr            msg('SQLite: ' . $e->getMessage(), -1);
1748da7d805SAndreas Gohr            return false;
1758da7d805SAndreas Gohr        }
17687fa2c18Sstretchyboy    }
177a1e6784eSAndreas Gohr
178ff97cc8fSstretchyboy
179ff97cc8fSstretchyboy    /**
180b122d121SAndreas Gohr     * Closes the result set (and it's cursors)
181b122d121SAndreas Gohr     *
182b122d121SAndreas Gohr     * If you're doing SELECT queries inside a TRANSACTION, be sure to call this
183b122d121SAndreas Gohr     * function on all your results sets, before COMMITing the transaction.
184b122d121SAndreas Gohr     *
1854b4b2db0SGerrit Uitslag     * Also required when not all rows of a result are fetched
1864b4b2db0SGerrit Uitslag     *
1878da7d805SAndreas Gohr     * @param \PDOStatement $res
188b122d121SAndreas Gohr     * @return bool
189b122d121SAndreas Gohr     */
1908da7d805SAndreas Gohr    public function res_close($res)
1918da7d805SAndreas Gohr    {
1928da7d805SAndreas Gohr        if (!$res) return false;
1938da7d805SAndreas Gohr
1948da7d805SAndreas Gohr        return $res->closeCursor();
195b122d121SAndreas Gohr    }
196b122d121SAndreas Gohr
197b122d121SAndreas Gohr    /**
198aa81d781SKlap-in     * Returns a complete result set as array
1998da7d805SAndreas Gohr     *
2008da7d805SAndreas Gohr     * @param \PDOStatement $res
2018da7d805SAndreas Gohr     * @return array
202ff97cc8fSstretchyboy     */
2038da7d805SAndreas Gohr    public function res2arr($res, $assoc = true)
2048da7d805SAndreas Gohr    {
2058da7d805SAndreas Gohr        if (!$res) return [];
2068da7d805SAndreas Gohr
2078da7d805SAndreas Gohr        // this is a bullshit workaround for having res2arr and res2count work on one result
2088da7d805SAndreas Gohr        if (!$this->data) {
2098da7d805SAndreas Gohr            $mode = $assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
2108da7d805SAndreas Gohr            $this->data = $res->fetchAll($mode);
2118da7d805SAndreas Gohr        }
2128da7d805SAndreas Gohr        return $this->data;
213b5b947d7SAndreas Gohr    }
214b5b947d7SAndreas Gohr
215b5b947d7SAndreas Gohr    /**
2168da7d805SAndreas Gohr     * Return the next row from the result set as associative array
2178da7d805SAndreas Gohr     *
2188da7d805SAndreas Gohr     * @param \PDOStatement $res
2198da7d805SAndreas Gohr     * @param int $rownum will be ignored
220b5b947d7SAndreas Gohr     */
2218da7d805SAndreas Gohr    public function res2row($res, $rownum = 0)
2228da7d805SAndreas Gohr    {
2238da7d805SAndreas Gohr        if (!$res) return false;
2248da7d805SAndreas Gohr
2258da7d805SAndreas Gohr        return $res->fetch(\PDO::FETCH_ASSOC);
226b5b947d7SAndreas Gohr    }
227b5b947d7SAndreas Gohr
228e7112ccbSAdrian Lang    /**
22944685fc6SKlap-in     * Return the first value from the next row.
2308da7d805SAndreas Gohr     *
2318da7d805SAndreas Gohr     * @param \PDOStatement $res
2328da7d805SAndreas Gohr     * @return mixed
233e7112ccbSAdrian Lang     */
2348da7d805SAndreas Gohr    public function res2single($res)
2358da7d805SAndreas Gohr    {
2368da7d805SAndreas Gohr        if (!$res) return false;
2378da7d805SAndreas Gohr
2388da7d805SAndreas Gohr        $data = $res->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, 0);
2398da7d805SAndreas Gohr        if (empty($data)) {
2408da7d805SAndreas Gohr            return false;
2418da7d805SAndreas Gohr        }
2428da7d805SAndreas Gohr        return $data[0];
243e7112ccbSAdrian Lang    }
244fee3b689Sstretchyboy
245fee3b689Sstretchyboy    /**
246fee3b689Sstretchyboy     * fetch the next row as zero indexed array
2478da7d805SAndreas Gohr     *
2488da7d805SAndreas Gohr     * @param \PDOStatement $res
2498da7d805SAndreas Gohr     * @return array|bool
250fee3b689Sstretchyboy     */
2518da7d805SAndreas Gohr    public function res_fetch_array($res)
2528da7d805SAndreas Gohr    {
2538da7d805SAndreas Gohr        if (!$res) return false;
2548da7d805SAndreas Gohr
2558da7d805SAndreas Gohr        return $res->fetch(PDO::FETCH_NUM);
25687fa2c18Sstretchyboy    }
257fee3b689Sstretchyboy
258fee3b689Sstretchyboy    /**
259fee3b689Sstretchyboy     * fetch the next row as assocative array
2608da7d805SAndreas Gohr     *
2618da7d805SAndreas Gohr     * @param \PDOStatement $res
2628da7d805SAndreas Gohr     * @return array|bool
263fee3b689Sstretchyboy     */
2648da7d805SAndreas Gohr    public function res_fetch_assoc($res)
2658da7d805SAndreas Gohr    {
2668da7d805SAndreas Gohr        if (!$res) return false;
2678da7d805SAndreas Gohr
2688da7d805SAndreas Gohr        return $res->fetch(PDO::FETCH_ASSOC);
269fee3b689Sstretchyboy    }
270fee3b689Sstretchyboy
271fee3b689Sstretchyboy    /**
27278977d74SKlap-in     * Count the number of records in result
2733157674bSAndreas Gohr     *
274db58e525SKlap-in     * This function is really inperformant in PDO and should be avoided!
2758da7d805SAndreas Gohr     *
2768da7d805SAndreas Gohr     * @param \PDOStatement $res
2778da7d805SAndreas Gohr     * @return int
278fee3b689Sstretchyboy     */
2798da7d805SAndreas Gohr    public function res2count($res)
2808da7d805SAndreas Gohr    {
2818da7d805SAndreas Gohr        if (!$res) return 0;
2828da7d805SAndreas Gohr
2838da7d805SAndreas Gohr        // this is a bullshit workaround for having res2arr and res2count work on one result
2848da7d805SAndreas Gohr        if (!$this->data) {
2858da7d805SAndreas Gohr            $this->data = $this->res2arr($res);
2868da7d805SAndreas Gohr        }
2878da7d805SAndreas Gohr
2888da7d805SAndreas Gohr        return count($this->data);
289fee3b689Sstretchyboy    }
29024a03f6cSstretchyboy
29124a03f6cSstretchyboy    /**
29224a03f6cSstretchyboy     * Count the number of records changed last time
2938da7d805SAndreas Gohr     *
2948da7d805SAndreas Gohr     * @param \PDOStatement $res
2958da7d805SAndreas Gohr     * @return int
29624a03f6cSstretchyboy     */
2978da7d805SAndreas Gohr    public function countChanges($res)
2988da7d805SAndreas Gohr    {
2998da7d805SAndreas Gohr        if (!$res) return 0;
3008da7d805SAndreas Gohr
3018da7d805SAndreas Gohr        return $res->rowCount();
302a1e6784eSAndreas Gohr    }
303a1e6784eSAndreas Gohr
3048da7d805SAndreas Gohr    // endregion
3058da7d805SAndreas Gohr
3068da7d805SAndreas Gohr    // region quoting/escaping functions
3078da7d805SAndreas Gohr
3088da7d805SAndreas Gohr    /**
3098da7d805SAndreas Gohr     * Join the given values and quote them for SQL insertion
3108da7d805SAndreas Gohr     */
3118da7d805SAndreas Gohr    public function quote_and_join($vals, $sep = ',')
3128da7d805SAndreas Gohr    {
3138da7d805SAndreas Gohr        $vals = array_map([$this->adapter->pdo(), 'quote'], $vals);
3148da7d805SAndreas Gohr        return join($sep, $vals);
3158da7d805SAndreas Gohr    }
3168da7d805SAndreas Gohr
3178da7d805SAndreas Gohr    /**
3188da7d805SAndreas Gohr     * Quotes a string, by escaping it and adding quotes
3198da7d805SAndreas Gohr     */
3208da7d805SAndreas Gohr    public function quote_string($string)
3218da7d805SAndreas Gohr    {
3228da7d805SAndreas Gohr        return $this->adapter->pdo()->quote($string);
3238da7d805SAndreas Gohr    }
3248da7d805SAndreas Gohr
3258da7d805SAndreas Gohr    /**
3268da7d805SAndreas Gohr     * Similar to quote_string, but without the quotes, useful to construct LIKE patterns
3278da7d805SAndreas Gohr     */
3288da7d805SAndreas Gohr    public function escape_string($str)
3298da7d805SAndreas Gohr    {
3308da7d805SAndreas Gohr        return trim($this->adapter->pdo()->quote($str), "'");
3318da7d805SAndreas Gohr    }
3328da7d805SAndreas Gohr
3338da7d805SAndreas Gohr    // endregion
3348da7d805SAndreas Gohr
3358da7d805SAndreas Gohr    // region speciality functions
3368da7d805SAndreas Gohr
3378da7d805SAndreas Gohr    /**
3388da7d805SAndreas Gohr     * Split sql queries on semicolons, unless when semicolons are quoted
3398da7d805SAndreas Gohr     *
3408da7d805SAndreas Gohr     * Usually you don't need this. It's only really needed if you need individual results for
3418da7d805SAndreas Gohr     * multiple queries. For example in the admin interface.
3428da7d805SAndreas Gohr     *
3438da7d805SAndreas Gohr     * @param string $sql
3448da7d805SAndreas Gohr     * @return array sql queries
3458da7d805SAndreas Gohr     * @deprecated
3468da7d805SAndreas Gohr     */
3478da7d805SAndreas Gohr    public function SQLstring2array($sql)
3488da7d805SAndreas Gohr    {
349d0a5ba7aSAndreas Gohr        if(!DOKU_UNITTEST) { // for now we don't want to trigger the deprecation warning in the tests
3508da7d805SAndreas Gohr            dbg_deprecated(Tools::class . '::SQLstring2array');
351d0a5ba7aSAndreas Gohr        }
3528da7d805SAndreas Gohr        return Tools::SQLstring2array($sql);
3538da7d805SAndreas Gohr    }
3548da7d805SAndreas Gohr
3558da7d805SAndreas Gohr    /**
3568da7d805SAndreas Gohr     * @deprecated needs to be fixed in stuct and structpublish
3578da7d805SAndreas Gohr     */
3588da7d805SAndreas Gohr    public function doTransaction($sql, $sqlpreparing = true) {
3598da7d805SAndreas Gohr        throw new \Exception(
3608da7d805SAndreas Gohr            'This method seems to never have done what it suggests. Please use the query() function instead.'
3618da7d805SAndreas Gohr        );
3628da7d805SAndreas Gohr    }
3638da7d805SAndreas Gohr
3648da7d805SAndreas Gohr    // endregion
365aa81d781SKlap-in}
366