xref: /plugin/sqlite/helper.php (revision 78977d741d27df2f16cc6fb2992c34ecca02925b)
1a1e6784eSAndreas Gohr<?php
2a1e6784eSAndreas Gohr/**
3a1e6784eSAndreas Gohr * DokuWiki Plugin sqlite (Helper Component)
4a1e6784eSAndreas Gohr *
5a1e6784eSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6a1e6784eSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
7a1e6784eSAndreas Gohr */
8a1e6784eSAndreas Gohr
9a1e6784eSAndreas Gohr// must be run within Dokuwiki
10a1e6784eSAndreas Gohrif (!defined('DOKU_INC')) die();
11a1e6784eSAndreas Gohr
12a1e6784eSAndreas Gohrif (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13a1e6784eSAndreas Gohrif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14a1e6784eSAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15a1e6784eSAndreas Gohr
1687fa2c18Sstretchyboyif (!defined('DOKU_EXT_SQLITE')) define('DOKU_EXT_SQLITE', 'sqlite');
1787fa2c18Sstretchyboyif (!defined('DOKU_EXT_PDO')) define('DOKU_EXT_PDO', 'pdo');
1887fa2c18Sstretchyboy
1987fa2c18Sstretchyboy
20a1e6784eSAndreas Gohrclass helper_plugin_sqlite extends DokuWiki_Plugin {
21a1e6784eSAndreas Gohr    var $db     = null;
22a1e6784eSAndreas Gohr    var $dbname = '';
237ed6069fSAdrian Lang    var $extension = null;
24a1e6784eSAndreas Gohr    function getInfo() {
25a1e6784eSAndreas Gohr        return confToHash(dirname(__FILE__).'plugin.info.txt');
26a1e6784eSAndreas Gohr    }
27a1e6784eSAndreas Gohr
28a1e6784eSAndreas Gohr    /**
29a1e6784eSAndreas Gohr     * constructor
30a1e6784eSAndreas Gohr     */
31a1e6784eSAndreas Gohr    function helper_plugin_sqlite(){
3287fa2c18Sstretchyboy
3387fa2c18Sstretchyboy      if(!$this->extension)
3487fa2c18Sstretchyboy      {
3587fa2c18Sstretchyboy        if (!extension_loaded('pdo_sqlite')) {
3687fa2c18Sstretchyboy            $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
3787fa2c18Sstretchyboy            if(function_exists('dl')) @dl($prefix . 'pdo_sqlite.' . PHP_SHLIB_SUFFIX);
3887fa2c18Sstretchyboy        }
3987fa2c18Sstretchyboy
4087fa2c18Sstretchyboy        if(class_exists('pdo')){
4187fa2c18Sstretchyboy            $this->extension = DOKU_EXT_PDO;
4287fa2c18Sstretchyboy        }
4387fa2c18Sstretchyboy      }
4487fa2c18Sstretchyboy
4587fa2c18Sstretchyboy      if(!$this->extension)
467ed6069fSAdrian Lang      {
477ed6069fSAdrian Lang        if (!extension_loaded('sqlite')) {
487ed6069fSAdrian Lang            $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
497ed6069fSAdrian Lang            if(function_exists('dl')) @dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
507ed6069fSAdrian Lang        }
517ed6069fSAdrian Lang
527ed6069fSAdrian Lang        if(function_exists('sqlite_open')){
537ed6069fSAdrian Lang           $this->extension = DOKU_EXT_SQLITE;
547ed6069fSAdrian Lang        }
557ed6069fSAdrian Lang      }
567ed6069fSAdrian Lang
577ed6069fSAdrian Lang      if(!$this->extension)
58d8139b19Sstretchyboy
5987fa2c18Sstretchyboy      {
6087fa2c18Sstretchyboy        msg('SQLite & PDO SQLite support missing in this PHP install - plugin will not work',-1);
61a1e6784eSAndreas Gohr      }
62a1e6784eSAndreas Gohr    }
63a1e6784eSAndreas Gohr
64a1e6784eSAndreas Gohr    /**
65a1e6784eSAndreas Gohr     * Initializes and opens the database
66a1e6784eSAndreas Gohr     *
67a1e6784eSAndreas Gohr     * Needs to be called right after loading this helper plugin
68a1e6784eSAndreas Gohr     */
69a1e6784eSAndreas Gohr    function init($dbname,$updatedir){
70a1e6784eSAndreas Gohr        global $conf;
71a1e6784eSAndreas Gohr
72a1e6784eSAndreas Gohr        // check for already open DB
73a1e6784eSAndreas Gohr        if($this->db){
74a1e6784eSAndreas Gohr            if($this->dbname == $dbname){
75a1e6784eSAndreas Gohr                // db already open
76a1e6784eSAndreas Gohr                return true;
77a1e6784eSAndreas Gohr            }
78a1e6784eSAndreas Gohr            // close other db
7987fa2c18Sstretchyboy            if($this->extension == DOKU_EXT_SQLITE)
8087fa2c18Sstretchyboy            {
81a1e6784eSAndreas Gohr              sqlite_close($this->db);
8287fa2c18Sstretchyboy            }
8387fa2c18Sstretchyboy            else
8487fa2c18Sstretchyboy            {
8587fa2c18Sstretchyboy              $this->db->close();
8687fa2c18Sstretchyboy            }
87a1e6784eSAndreas Gohr            $this->db     = null;
88a1e6784eSAndreas Gohr            $this->dbname = '';
89a1e6784eSAndreas Gohr        }
90a1e6784eSAndreas Gohr
91a1e6784eSAndreas Gohr        $this->dbname = $dbname;
9287fa2c18Sstretchyboy
9340698f67SKlap-in        // Separate the database files to prevent not-requested autoupgrades.
9440698f67SKlap-in        if($this->extension == DOKU_EXT_SQLITE){
9587fa2c18Sstretchyboy            $fileextension = '.sqlite';
9640698f67SKlap-in        }else{
9740698f67SKlap-in            $fileextension = '.sqlite3';
9840698f67SKlap-in        }
9987fa2c18Sstretchyboy
100ff97cc8fSstretchyboy        $this->dbfile = $conf['metadir'].'/'.$dbname.$fileextension;
101ff97cc8fSstretchyboy
102ff97cc8fSstretchyboy        $init   = (!@file_exists($this->dbfile) || ((int) @filesize($this->dbfile)) < 3);
103a1e6784eSAndreas Gohr
1040098e1a7SKlap-in        //first line tell the format of db file http://marc.info/?l=sqlite-users&m=109383875408202
10540698f67SKlap-in        $firstline=@file_get_contents($this->dbfile,false,null,0,15);
1060098e1a7SKlap-in
10787fa2c18Sstretchyboy        if($this->extension == DOKU_EXT_SQLITE)
10887fa2c18Sstretchyboy        {
1090098e1a7SKlap-in          if($firstline=='SQLite format 3'){
1100098e1a7SKlap-in              msg("SQLite: failed to open SQLite '".$this->dbname."' database (DB has a sqlite3 format instead of sqlite2 format.)",-1);
1110098e1a7SKlap-in              return false;
1120098e1a7SKlap-in          }
1130098e1a7SKlap-in
114a1e6784eSAndreas Gohr          $error='';
115ff97cc8fSstretchyboy          $this->db = sqlite_open($this->dbfile, 0666, $error);
116a1e6784eSAndreas Gohr          if(!$this->db){
117be16ec27SKlap-in              msg("SQLite: failed to open SQLite '".$this->dbname."' database ($error)",-1);
118a1e6784eSAndreas Gohr              return false;
119a1e6784eSAndreas Gohr          }
120a1e6784eSAndreas Gohr
121b5b947d7SAndreas Gohr          // register our custom aggregate function
122b5b947d7SAndreas Gohr          sqlite_create_aggregate($this->db,'group_concat',
123b5b947d7SAndreas Gohr                                  array($this,'_sqlite_group_concat_step'),
124b5b947d7SAndreas Gohr                                  array($this,'_sqlite_group_concat_finalize'), 2);
12587fa2c18Sstretchyboy        }
12687fa2c18Sstretchyboy        else
12787fa2c18Sstretchyboy        {
12840698f67SKlap-in          if($init){
12940698f67SKlap-in              $oldDbfile = substr($this->dbfile,0,-1);
13040698f67SKlap-in
13140698f67SKlap-in              if(@file_exists($oldDbfile)){
13240698f67SKlap-in                  $notfound_msg="SQLite: '".$this->dbname.$fileextension."' database not found. In the meta directory is '".$this->dbname.substr($fileextension,0,-1)."' available. ";
13340698f67SKlap-in
13440698f67SKlap-in                  $firstline=@file_get_contents($oldDbfile,false,null,0,15);
13540698f67SKlap-in                  if($firstline=='SQLite format 3'){
13640698f67SKlap-in                      msg($notfound_msg."PDO sqlite needs you rename manual the file extension to '.sqlite3' .",-1);
13740698f67SKlap-in                      return false;
13840698f67SKlap-in                  }else{
13940698f67SKlap-in                      msg($notfound_msg."PDO sqlite needs you upgrade manual this sqlite2 db to sqlite3 format.",-1);
14040698f67SKlap-in                      return false;
14140698f67SKlap-in                  }
14240698f67SKlap-in              }
14340698f67SKlap-in          }else{
1440098e1a7SKlap-in              if($firstline!='SQLite format 3'){
1450098e1a7SKlap-in                  msg("SQLite: failed to open SQLite '".$this->dbname."' database (DB has not a sqlite3 format.)",-1);
1460098e1a7SKlap-in                  return false;
1470098e1a7SKlap-in              }
14840698f67SKlap-in          }
1490098e1a7SKlap-in
150ff97cc8fSstretchyboy          $dsn = 'sqlite:'.$this->dbfile;
15187fa2c18Sstretchyboy
15287fa2c18Sstretchyboy          try {
15387fa2c18Sstretchyboy              $this->db = new PDO($dsn);
15487fa2c18Sstretchyboy          } catch (PDOException $e) {
155be16ec27SKlap-in            msg("SQLite: failed to open SQLite '".$this->dbname."' database (".$e->getMessage().")",-1);
15687fa2c18Sstretchyboy              return false;
15787fa2c18Sstretchyboy          }
15887fa2c18Sstretchyboy          $this->db->sqliteCreateAggregate('group_concat',
159ff97cc8fSstretchyboy                                  array($this,'_pdo_group_concat_step'),
160ff97cc8fSstretchyboy                                  array($this,'_pdo_group_concat_finalize'));
16187fa2c18Sstretchyboy        }
162b5b947d7SAndreas Gohr
163a1e6784eSAndreas Gohr        $this->_updatedb($init,$updatedir);
164a1e6784eSAndreas Gohr        return true;
165a1e6784eSAndreas Gohr    }
166a1e6784eSAndreas Gohr
167a1e6784eSAndreas Gohr    /**
168a1e6784eSAndreas Gohr     * Return the current Database Version
169a1e6784eSAndreas Gohr     */
170a1e6784eSAndreas Gohr    function _currentDBversion(){
171a1e6784eSAndreas Gohr        $sql = "SELECT val FROM opts WHERE opt = 'dbversion';";
172a1e6784eSAndreas Gohr        $res = $this->query($sql);
173a1e6784eSAndreas Gohr        if(!$res) return false;
174a1e6784eSAndreas Gohr        $row = $this->res2row($res,0);
175a1e6784eSAndreas Gohr        return (int) $row['val'];
176a1e6784eSAndreas Gohr    }
177a1e6784eSAndreas Gohr    /**
178a1e6784eSAndreas Gohr     * Update the database if needed
179a1e6784eSAndreas Gohr     *
180a1e6784eSAndreas Gohr     * @param bool   $init      - true if this is a new database to initialize
181a1e6784eSAndreas Gohr     * @param string $updatedir - Database update infos
182a1e6784eSAndreas Gohr     */
1834d9093b4Sstretchyboy    function _updatedb($init,$updatedir)
1844d9093b4Sstretchyboy    {
185a1e6784eSAndreas Gohr        if($init){
1864d9093b4Sstretchyboy
187a1e6784eSAndreas Gohr            $current = 0;
188a1e6784eSAndreas Gohr        }else{
189a1e6784eSAndreas Gohr            $current = $this->_currentDBversion();
190a1e6784eSAndreas Gohr            if(!$current){
191be16ec27SKlap-in                msg("SQLite: no DB version found. '".$this->dbname."' DB probably broken.",-1);
192a1e6784eSAndreas Gohr                return false;
193a1e6784eSAndreas Gohr            }
194a1e6784eSAndreas Gohr        }
195a1e6784eSAndreas Gohr
196a1e6784eSAndreas Gohr        // in case of init, add versioning table
197a1e6784eSAndreas Gohr        if($init){
198a1e6784eSAndreas Gohr            if(!$this->_runupdatefile(dirname(__FILE__).'/db.sql',0)){
199be16ec27SKlap-in                  msg("SQLite: '".$this->dbname."' database upgrade failed for version ", -1);
200a1e6784eSAndreas Gohr                return false;
201a1e6784eSAndreas Gohr            }
202a1e6784eSAndreas Gohr        }
203a1e6784eSAndreas Gohr
204a1e6784eSAndreas Gohr        $latest  = (int) trim(io_readFile($updatedir.'/latest.version'));
205a1e6784eSAndreas Gohr
206a1e6784eSAndreas Gohr        // all up to date?
207a1e6784eSAndreas Gohr        if($current >= $latest) return true;
208a1e6784eSAndreas Gohr        for($i=$current+1; $i<=$latest; $i++){
209a1e6784eSAndreas Gohr            $file = sprintf($updatedir.'/update%04d.sql',$i);
210a1e6784eSAndreas Gohr            if(file_exists($file)){
211a1e6784eSAndreas Gohr                if(!$this->_runupdatefile($file,$i)){
212be16ec27SKlap-in                    msg("SQLite: '".$this->dbname."' database upgrade failed for version ".$i, -1);
213a1e6784eSAndreas Gohr                    return false;
214a1e6784eSAndreas Gohr                }
215a1e6784eSAndreas Gohr            }
216a1e6784eSAndreas Gohr        }
217a1e6784eSAndreas Gohr        return true;
218a1e6784eSAndreas Gohr    }
219a1e6784eSAndreas Gohr
220a1e6784eSAndreas Gohr    /**
221a1e6784eSAndreas Gohr     * Updates the database structure using the given file to
222a1e6784eSAndreas Gohr     * the given version.
223a1e6784eSAndreas Gohr     */
224a1e6784eSAndreas Gohr    function _runupdatefile($file,$version){
225a1e6784eSAndreas Gohr        $sql  = io_readFile($file,false);
226a1e6784eSAndreas Gohr
227a1e6784eSAndreas Gohr        $sql = explode(";",$sql);
228a1e6784eSAndreas Gohr        array_unshift($sql,'BEGIN TRANSACTION');
229a1e6784eSAndreas Gohr        array_push($sql,"INSERT OR REPLACE INTO opts (val,opt) VALUES ($version,'dbversion')");
230a1e6784eSAndreas Gohr        array_push($sql,"COMMIT TRANSACTION");
231a1e6784eSAndreas Gohr
232a1e6784eSAndreas Gohr        foreach($sql as $s){
233a1e6784eSAndreas Gohr            $s = preg_replace('!^\s*--.*$!m', '', $s);
234a1e6784eSAndreas Gohr            $s = trim($s);
235a1e6784eSAndreas Gohr            if(!$s) continue;
236fd69a32cSAndreas Gohr
237fd69a32cSAndreas Gohr
238a1e6784eSAndreas Gohr            $res = $this->query("$s;");
239a1e6784eSAndreas Gohr            if ($res === false) {
24087fa2c18Sstretchyboy              if($this->extension == DOKU_EXT_SQLITE)
24187fa2c18Sstretchyboy              {
242a1e6784eSAndreas Gohr                sqlite_query($this->db, 'ROLLBACK TRANSACTION');
243a1e6784eSAndreas Gohr                return false;
244a1e6784eSAndreas Gohr              }
24505f176edSstretchyboy              else
24605f176edSstretchyboy              {
24705f176edSstretchyboy                return false;
24805f176edSstretchyboy              }
249a1e6784eSAndreas Gohr            }
25087fa2c18Sstretchyboy        }
251a1e6784eSAndreas Gohr
252a1e6784eSAndreas Gohr        return ($version == $this->_currentDBversion());
253a1e6784eSAndreas Gohr    }
254a1e6784eSAndreas Gohr
255a1e6784eSAndreas Gohr    /**
256fd69a32cSAndreas Gohr     * Emulate ALTER TABLE
257fd69a32cSAndreas Gohr     *
258fd69a32cSAndreas Gohr     * The ALTER TABLE syntax is parsed and then emulated using a
259fd69a32cSAndreas Gohr     * temporary table
260fd69a32cSAndreas Gohr     *
261fd69a32cSAndreas Gohr     * @author <jon@jenseng.com>
262fd69a32cSAndreas Gohr     * @link   http://code.jenseng.com/db/
263fb394683SAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
264fd69a32cSAndreas Gohr     */
265fd69a32cSAndreas Gohr    function _altertable($table,$alterdefs){
266fb394683SAndreas Gohr
267fb394683SAndreas Gohr        // load original table definition SQL
268fd69a32cSAndreas Gohr        $result = $this->query("SELECT sql,name,type
269fd69a32cSAndreas Gohr                                  FROM sqlite_master
270fd69a32cSAndreas Gohr                                 WHERE tbl_name = '$table'
271fb394683SAndreas Gohr                                   AND type = 'table'");
272fb394683SAndreas Gohr
27387fa2c18Sstretchyboy        if(($result === false) || ($this->extension == DOKU_EXT_SQLITE && sqlite_num_rows($result)<=0)){
274fd69a32cSAndreas Gohr            msg("ALTER TABLE failed, no such table '".hsc($table)."'",-1);
275fd69a32cSAndreas Gohr            return false;
276fd69a32cSAndreas Gohr        }
27787fa2c18Sstretchyboy
27887fa2c18Sstretchyboy        if($this->extension == DOKU_EXT_SQLITE )
27987fa2c18Sstretchyboy        {
280fb394683SAndreas Gohr          $row = sqlite_fetch_array($result);
28187fa2c18Sstretchyboy        }
28287fa2c18Sstretchyboy        else
28387fa2c18Sstretchyboy        {
28487fa2c18Sstretchyboy           $row = $result->fetch(PDO::FETCH_ASSOC);
28587fa2c18Sstretchyboy        }
28687fa2c18Sstretchyboy
28787fa2c18Sstretchyboy        if($row === false){
28887fa2c18Sstretchyboy            msg("ALTER TABLE failed, table '".hsc($table)."' had no master data",-1);
28987fa2c18Sstretchyboy            return false;
29087fa2c18Sstretchyboy        }
29187fa2c18Sstretchyboy
292fd69a32cSAndreas Gohr
293fb394683SAndreas Gohr        // prepare temporary table SQL
294fd69a32cSAndreas Gohr        $tmpname = 't'.time();
295fd69a32cSAndreas Gohr        $origsql = trim(preg_replace("/[\s]+/"," ",
296fd69a32cSAndreas Gohr                        str_replace(",",", ",
297fd69a32cSAndreas Gohr                        preg_replace('/\)$/',' )',
298fd69a32cSAndreas Gohr                        preg_replace("/[\(]/","( ",$row['sql'],1)))));
299fd69a32cSAndreas Gohr        $createtemptableSQL = 'CREATE TEMPORARY '.substr(trim(preg_replace("'".$table."'",$tmpname,$origsql,1)),6);
300fd69a32cSAndreas Gohr        $createindexsql = array();
301fb394683SAndreas Gohr
302fb394683SAndreas Gohr        // load indexes to reapply later
303fb394683SAndreas Gohr        $result = $this->query("SELECT sql,name,type
304fb394683SAndreas Gohr                                  FROM sqlite_master
305fb394683SAndreas Gohr                                 WHERE tbl_name = '$table'
306fb394683SAndreas Gohr                                   AND type = 'index'");
307fb394683SAndreas Gohr        if(!$result){
308fb394683SAndreas Gohr            $indexes = array();
309fb394683SAndreas Gohr        }else{
310fb394683SAndreas Gohr            $indexes = $this->res2arr($result);
311fb394683SAndreas Gohr        }
312fb394683SAndreas Gohr
313fb394683SAndreas Gohr
314fd69a32cSAndreas Gohr        $i = 0;
315fd69a32cSAndreas Gohr        $defs = preg_split("/[,]+/",$alterdefs,-1,PREG_SPLIT_NO_EMPTY);
316fd69a32cSAndreas Gohr        $prevword = $table;
317fd69a32cSAndreas Gohr        $oldcols = preg_split("/[,]+/",substr(trim($createtemptableSQL),strpos(trim($createtemptableSQL),'(')+1),-1,PREG_SPLIT_NO_EMPTY);
318fd69a32cSAndreas Gohr        $newcols = array();
319fd69a32cSAndreas Gohr
320665af209SAdrian Lang        for($i=0;$i<count($oldcols);$i++){
321fd69a32cSAndreas Gohr            $colparts = preg_split("/[\s]+/",$oldcols[$i],-1,PREG_SPLIT_NO_EMPTY);
322fd69a32cSAndreas Gohr            $oldcols[$i] = $colparts[0];
323fd69a32cSAndreas Gohr            $newcols[$colparts[0]] = $colparts[0];
324fd69a32cSAndreas Gohr        }
325fd69a32cSAndreas Gohr        $newcolumns = '';
326fd69a32cSAndreas Gohr        $oldcolumns = '';
327fd69a32cSAndreas Gohr        reset($newcols);
328fd69a32cSAndreas Gohr        while(list($key,$val) = each($newcols)){
329fd69a32cSAndreas Gohr            $newcolumns .= ($newcolumns?', ':'').$val;
330fd69a32cSAndreas Gohr            $oldcolumns .= ($oldcolumns?', ':'').$key;
331fd69a32cSAndreas Gohr        }
332fd69a32cSAndreas Gohr        $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table;
333fd69a32cSAndreas Gohr        $dropoldsql = 'DROP TABLE '.$table;
334fd69a32cSAndreas Gohr        $createtesttableSQL = $createtemptableSQL;
335fd69a32cSAndreas Gohr
336fd69a32cSAndreas Gohr        foreach($defs as $def){
337fd69a32cSAndreas Gohr            $defparts = preg_split("/[\s]+/",$def,-1,PREG_SPLIT_NO_EMPTY);
338fd69a32cSAndreas Gohr            $action = strtolower($defparts[0]);
339fd69a32cSAndreas Gohr            switch($action){
340fd69a32cSAndreas Gohr                case 'add':
341665af209SAdrian Lang                    if(count($defparts) < 2){
342fd69a32cSAndreas Gohr                        msg('ALTER TABLE: not enough arguments for ADD statement',-1);
343fd69a32cSAndreas Gohr                        return false;
344fd69a32cSAndreas Gohr                    }
345fd69a32cSAndreas Gohr                    $createtesttableSQL = substr($createtesttableSQL,0,strlen($createtesttableSQL)-1).',';
346665af209SAdrian Lang                    for($i=1;$i<count($defparts);$i++)
347fd69a32cSAndreas Gohr                        $createtesttableSQL.=' '.$defparts[$i];
348fd69a32cSAndreas Gohr                    $createtesttableSQL.=')';
349fd69a32cSAndreas Gohr                    break;
350fd69a32cSAndreas Gohr
351fd69a32cSAndreas Gohr                case 'change':
352665af209SAdrian Lang                    if(count($defparts) <= 3){
353fd69a32cSAndreas Gohr                        msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').($defparts[2]?' '.$defparts[2]:'').'": syntax error',-1);
354fd69a32cSAndreas Gohr                        return false;
355fd69a32cSAndreas Gohr                    }
356fd69a32cSAndreas Gohr
357fd69a32cSAndreas Gohr                    if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){
358fd69a32cSAndreas Gohr                        if($newcols[$defparts[1]] != $defparts[1]){
359fd69a32cSAndreas Gohr                            msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1);
360fd69a32cSAndreas Gohr                            return false;
361fd69a32cSAndreas Gohr                        }
362fd69a32cSAndreas Gohr                        $newcols[$defparts[1]] = $defparts[2];
363fd69a32cSAndreas Gohr                        $nextcommapos = strpos($createtesttableSQL,',',$severpos);
364fd69a32cSAndreas Gohr                        $insertval = '';
365665af209SAdrian Lang                        for($i=2;$i<count($defparts);$i++)
366fd69a32cSAndreas Gohr                            $insertval.=' '.$defparts[$i];
367fd69a32cSAndreas Gohr                        if($nextcommapos)
368fd69a32cSAndreas Gohr                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos).$insertval.substr($createtesttableSQL,$nextcommapos);
369fd69a32cSAndreas Gohr                        else
370fd69a32cSAndreas Gohr                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1)).$insertval.')';
371fd69a32cSAndreas Gohr                    } else {
372fd69a32cSAndreas Gohr                        msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1);
373fd69a32cSAndreas Gohr                        return false;
374fd69a32cSAndreas Gohr                    }
375fd69a32cSAndreas Gohr                    break;
376fd69a32cSAndreas Gohr                case 'drop':
377665af209SAdrian Lang                    if(count($defparts) < 2){
378fd69a32cSAndreas Gohr                        msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').'": syntax error',-1);
379fd69a32cSAndreas Gohr                        return false;
380fd69a32cSAndreas Gohr                    }
381fd69a32cSAndreas Gohr                    if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){
382fd69a32cSAndreas Gohr                        $nextcommapos = strpos($createtesttableSQL,',',$severpos);
383fd69a32cSAndreas Gohr                        if($nextcommapos)
384fd69a32cSAndreas Gohr                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos).substr($createtesttableSQL,$nextcommapos + 1);
385fd69a32cSAndreas Gohr                        else
386fd69a32cSAndreas Gohr                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1) - 1).')';
387fd69a32cSAndreas Gohr                        unset($newcols[$defparts[1]]);
388fd69a32cSAndreas Gohr                    }else{
389fd69a32cSAndreas Gohr                        msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1);
390fd69a32cSAndreas Gohr                        return false;
391fd69a32cSAndreas Gohr                    }
392fd69a32cSAndreas Gohr                    break;
393fd69a32cSAndreas Gohr                default:
394fd69a32cSAndreas Gohr                    msg('ALTER TABLE: near "'.$prevword.'": syntax error',-1);
395fd69a32cSAndreas Gohr                    return false;
396fd69a32cSAndreas Gohr            }
397665af209SAdrian Lang            $prevword = $defparts[count($defparts)-1];
398fd69a32cSAndreas Gohr        }
399fd69a32cSAndreas Gohr
400fd69a32cSAndreas Gohr        // this block of code generates a test table simply to verify that the
401fd69a32cSAndreas Gohr        // columns specifed are valid in an sql statement
402fd69a32cSAndreas Gohr        // this ensures that no reserved words are used as columns, for example
403fd69a32cSAndreas Gohr        $res = $this->query($createtesttableSQL);
404fd69a32cSAndreas Gohr        if($res === false) return false;
405fd69a32cSAndreas Gohr
406fd69a32cSAndreas Gohr        $droptempsql = 'DROP TABLE '.$tmpname;
407fd69a32cSAndreas Gohr        $res = $this->query($droptempsql);
408fd69a32cSAndreas Gohr        if($res === false) return false;
409fd69a32cSAndreas Gohr
410fd69a32cSAndreas Gohr
411fd69a32cSAndreas Gohr        $createnewtableSQL = 'CREATE '.substr(trim(preg_replace("'".$tmpname."'",$table,$createtesttableSQL,1)),17);
412fd69a32cSAndreas Gohr        $newcolumns = '';
413fd69a32cSAndreas Gohr        $oldcolumns = '';
414fd69a32cSAndreas Gohr        reset($newcols);
415fd69a32cSAndreas Gohr        while(list($key,$val) = each($newcols)){
416fd69a32cSAndreas Gohr            $newcolumns .= ($newcolumns?', ':'').$val;
417fd69a32cSAndreas Gohr            $oldcolumns .= ($oldcolumns?', ':'').$key;
418fd69a32cSAndreas Gohr        }
419fd69a32cSAndreas Gohr
420fd69a32cSAndreas Gohr        $copytonewsql = 'INSERT INTO '.$table.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$tmpname;
421fd69a32cSAndreas Gohr
422fd69a32cSAndreas Gohr        $res = $this->query($createtemptableSQL); //create temp table
423fd69a32cSAndreas Gohr        if($res === false) return false;
424fd69a32cSAndreas Gohr        $res = $this->query($copytotempsql); //copy to table
425fd69a32cSAndreas Gohr        if($res === false) return false;
426fd69a32cSAndreas Gohr        $res = $this->query($dropoldsql); //drop old table
427fd69a32cSAndreas Gohr        if($res === false) return false;
428fd69a32cSAndreas Gohr
429fd69a32cSAndreas Gohr        $res = $this->query($createnewtableSQL); //recreate original table
430fd69a32cSAndreas Gohr        if($res === false) return false;
431fd69a32cSAndreas Gohr        $res = $this->query($copytonewsql); //copy back to original table
432fd69a32cSAndreas Gohr        if($res === false) return false;
433fb394683SAndreas Gohr
434fb394683SAndreas Gohr        foreach($indexes as $index){ // readd indexes
435fb394683SAndreas Gohr            $res = $this->query($index['sql']);
436fb394683SAndreas Gohr            if($res === false) return false;
437fb394683SAndreas Gohr        }
438fb394683SAndreas Gohr
439fd69a32cSAndreas Gohr        $res = $this->query($droptempsql); //drop temp table
440fd69a32cSAndreas Gohr        if($res === false) return false;
441fd69a32cSAndreas Gohr
442fd69a32cSAndreas Gohr        return $res; // return a valid resource
443fd69a32cSAndreas Gohr    }
444fd69a32cSAndreas Gohr
445fd69a32cSAndreas Gohr    /**
4463ae3f79eSKlap-in     * Registers a User Defined Function for use in SQL statements
4473ae3f79eSKlap-in     */
4483ae3f79eSKlap-in    function create_function($function_name,$callback,$num_args){
4493ae3f79eSKlap-in        if($this->extension == DOKU_EXT_SQLITE )
4503ae3f79eSKlap-in        {
4513ae3f79eSKlap-in          sqlite_create_function($this->db,$function_name,$callback,$num_args);
4523ae3f79eSKlap-in        }
4533ae3f79eSKlap-in        else
4543ae3f79eSKlap-in        {
455edc99285SKlap-in          $this->db->sqliteCreateFunction($function_name,$callback,$num_args);
4563ae3f79eSKlap-in        }
4573ae3f79eSKlap-in    }
4583ae3f79eSKlap-in
4593ae3f79eSKlap-in    /**
460a1e6784eSAndreas Gohr     * Execute a query with the given parameters.
461a1e6784eSAndreas Gohr     *
462a1e6784eSAndreas Gohr     * Takes care of escaping
463a1e6784eSAndreas Gohr     *
464a1e6784eSAndreas Gohr     * @param string $sql - the statement
465a1e6784eSAndreas Gohr     * @param arguments...
466a1e6784eSAndreas Gohr     */
467a1e6784eSAndreas Gohr    function query(){
468a1e6784eSAndreas Gohr        if(!$this->db) return false;
469a1e6784eSAndreas Gohr
470a1e6784eSAndreas Gohr        // get function arguments
471a1e6784eSAndreas Gohr        $args = func_get_args();
472a1e6784eSAndreas Gohr        $sql  = trim(array_shift($args));
473fd69a32cSAndreas Gohr        $sql  = rtrim($sql,';');
474a1e6784eSAndreas Gohr
475a1e6784eSAndreas Gohr        if(!$sql){
476a1e6784eSAndreas Gohr            msg('No SQL statement given',-1);
477a1e6784eSAndreas Gohr            return false;
478a1e6784eSAndreas Gohr        }
479a1e6784eSAndreas Gohr
480a1e6784eSAndreas Gohr        $argc = count($args);
48112f8c9c7SAdrian Lang        if($argc > 0 && is_array($args[0])) {
48212f8c9c7SAdrian Lang            $args = $args[0];
48312f8c9c7SAdrian Lang            $argc = count($args);
48412f8c9c7SAdrian Lang        }
485a1e6784eSAndreas Gohr
486a1e6784eSAndreas Gohr        // check number of arguments
487a1e6784eSAndreas Gohr        if($argc < substr_count($sql,'?')){
488a1e6784eSAndreas Gohr            msg('Not enough arguments passed for statement. '.
489a1e6784eSAndreas Gohr                'Expected '.substr_count($sql,'?').' got '.
490a1e6784eSAndreas Gohr                $argc.' - '.hsc($sql),-1);
491a1e6784eSAndreas Gohr            return false;
492a1e6784eSAndreas Gohr        }
493a1e6784eSAndreas Gohr
494a1e6784eSAndreas Gohr        // explode at wildcard, then join again
495a1e6784eSAndreas Gohr        $parts = explode('?',$sql,$argc+1);
496a1e6784eSAndreas Gohr        $args  = array_map(array($this,'quote_string'),$args);
497a1e6784eSAndreas Gohr        $sql   = '';
498a1e6784eSAndreas Gohr
499a1e6784eSAndreas Gohr        while( ($part = array_shift($parts)) !== null ){
500a1e6784eSAndreas Gohr            $sql .= $part;
501a1e6784eSAndreas Gohr            $sql .= array_shift($args);
502a1e6784eSAndreas Gohr        }
503a1e6784eSAndreas Gohr
504fd69a32cSAndreas Gohr        // intercept ALTER TABLE statements
50587fa2c18Sstretchyboy        $match = null;
506fd69a32cSAndreas Gohr        if(preg_match('/^ALTER\s+TABLE\s+([\w\.]+)\s+(.*)/i',$sql,$match)){
507fd69a32cSAndreas Gohr            return $this->_altertable($match[1],$match[2]);
508fd69a32cSAndreas Gohr        }
509fd69a32cSAndreas Gohr
510a1e6784eSAndreas Gohr        // execute query
511a1e6784eSAndreas Gohr        $err = '';
51287fa2c18Sstretchyboy        if($this->extension == DOKU_EXT_SQLITE )
51387fa2c18Sstretchyboy        {
514a1e6784eSAndreas Gohr          $res = @sqlite_query($this->db,$sql,SQLITE_ASSOC,$err);
515a1e6784eSAndreas Gohr          if($err){
516d9cff31cSAndreas Gohr              msg($err.':<br /><pre>'.hsc($sql).'</pre>',-1);
517a1e6784eSAndreas Gohr              return false;
518a1e6784eSAndreas Gohr          }elseif(!$res){
519a1e6784eSAndreas Gohr              msg(sqlite_error_string(sqlite_last_error($this->db)).
520d9cff31cSAndreas Gohr                  ':<br /><pre>'.hsc($sql).'</pre>',-1);
521a1e6784eSAndreas Gohr              return false;
522a1e6784eSAndreas Gohr          }
52387fa2c18Sstretchyboy        }
52487fa2c18Sstretchyboy        else
52587fa2c18Sstretchyboy        {
52687fa2c18Sstretchyboy          $res = $this->db->query($sql);
52787fa2c18Sstretchyboy
52887fa2c18Sstretchyboy          if(!$res){
52987fa2c18Sstretchyboy            $err = $this->db->errorInfo();
53087fa2c18Sstretchyboy              msg($err[2].':<br /><pre>'.hsc($sql).'</pre>',-1);
53187fa2c18Sstretchyboy              return false;
53287fa2c18Sstretchyboy          }
53387fa2c18Sstretchyboy        }
534a1e6784eSAndreas Gohr        return $res;
535a1e6784eSAndreas Gohr    }
536a1e6784eSAndreas Gohr
537a1e6784eSAndreas Gohr    /**
538a1e6784eSAndreas Gohr     * Returns a complete result set as array
539a1e6784eSAndreas Gohr     */
540a1e6784eSAndreas Gohr    function res2arr($res){
541a1e6784eSAndreas Gohr        $data = array();
54287fa2c18Sstretchyboy        if($this->extension == DOKU_EXT_SQLITE )
54387fa2c18Sstretchyboy        {
544a1e6784eSAndreas Gohr          if(!sqlite_num_rows($res)) return $data;
545a1e6784eSAndreas Gohr          sqlite_rewind($res);
546e598d748SAdrian Lang          while(($row = sqlite_fetch_array($res, SQLITE_ASSOC)) !== false){
547a1e6784eSAndreas Gohr              $data[] = $row;
548a1e6784eSAndreas Gohr          }
54987fa2c18Sstretchyboy        }
55087fa2c18Sstretchyboy        else
55187fa2c18Sstretchyboy        {
5522da43e76SKlap-in          if(!$res) return $data;
55387fa2c18Sstretchyboy          $data = $res->fetchAll(PDO::FETCH_ASSOC);
554*78977d74SKlap-in          if(!count($data))
55587fa2c18Sstretchyboy          {
55687fa2c18Sstretchyboy            return false;
55787fa2c18Sstretchyboy          }
55887fa2c18Sstretchyboy        }
559a1e6784eSAndreas Gohr        return $data;
560a1e6784eSAndreas Gohr    }
561a1e6784eSAndreas Gohr
562a1e6784eSAndreas Gohr    /**
563a1e6784eSAndreas Gohr     * Return the wanted row from a given result set as
564a1e6784eSAndreas Gohr     * associative array
565a1e6784eSAndreas Gohr     */
566a1e6784eSAndreas Gohr    function res2row($res,$rownum=0){
56787fa2c18Sstretchyboy        if($this->extension == DOKU_EXT_SQLITE )
56887fa2c18Sstretchyboy        {
569a1e6784eSAndreas Gohr          if(!@sqlite_seek($res,$rownum)){
570a1e6784eSAndreas Gohr              return false;
571a1e6784eSAndreas Gohr          }
572e598d748SAdrian Lang          return sqlite_fetch_array($res, SQLITE_ASSOC);
573a1e6784eSAndreas Gohr        }
57487fa2c18Sstretchyboy        else
57587fa2c18Sstretchyboy        {
576*78977d74SKlap-in          if(!$res) return false;
577*78977d74SKlap-in
578ddcdcb90SKlap-in          return $res->fetch(PDO::FETCH_ASSOC,PDO::FETCH_ORI_ABS,$rownum);
57987fa2c18Sstretchyboy        }
58087fa2c18Sstretchyboy    }
581a1e6784eSAndreas Gohr
582fee3b689Sstretchyboy    /**
583fee3b689Sstretchyboy     * Return the first value from the first row.
584fee3b689Sstretchyboy     */
585fee3b689Sstretchyboy    function res2single($res)
586fee3b689Sstretchyboy    {
58787fa2c18Sstretchyboy      if($this->extension == DOKU_EXT_SQLITE )
58887fa2c18Sstretchyboy      {
589fee3b689Sstretchyboy        return sqlite_fetch_single($res);
590fee3b689Sstretchyboy      }
59187fa2c18Sstretchyboy      else
59287fa2c18Sstretchyboy      {
593*78977d74SKlap-in        if(!$res) return false;
594*78977d74SKlap-in
595ddcdcb90SKlap-in        $data = $res->fetch(PDO::FETCH_NUM,PDO::FETCH_ORI_ABS,0);
596*78977d74SKlap-in        if(!count($data))
59787fa2c18Sstretchyboy        {
59887fa2c18Sstretchyboy          return false;
59987fa2c18Sstretchyboy        }
600ddcdcb90SKlap-in        return $data[0];
60187fa2c18Sstretchyboy      }
60287fa2c18Sstretchyboy    }
603a1e6784eSAndreas Gohr
604a1e6784eSAndreas Gohr    /**
605a1e6784eSAndreas Gohr     * Join the given values and quote them for SQL insertion
606a1e6784eSAndreas Gohr     */
607a1e6784eSAndreas Gohr    function quote_and_join($vals,$sep=',') {
608a1e6784eSAndreas Gohr        $vals = array_map(array('helper_plugin_sqlite','quote_string'),$vals);
609a1e6784eSAndreas Gohr        return join($sep,$vals);
610a1e6784eSAndreas Gohr    }
611a1e6784eSAndreas Gohr
612a1e6784eSAndreas Gohr    /**
613a1e6784eSAndreas Gohr     * Run sqlite_escape_string() on the given string and surround it
614a1e6784eSAndreas Gohr     * with quotes
615a1e6784eSAndreas Gohr     */
616a1e6784eSAndreas Gohr    function quote_string($string){
61787fa2c18Sstretchyboy      if($this->extension == DOKU_EXT_SQLITE )
61887fa2c18Sstretchyboy      {
619a1e6784eSAndreas Gohr        return "'".sqlite_escape_string($string)."'";
620a1e6784eSAndreas Gohr      }
62187fa2c18Sstretchyboy      else
62287fa2c18Sstretchyboy      {
62387fa2c18Sstretchyboy        return $this->db->quote($string);
62487fa2c18Sstretchyboy      }
62587fa2c18Sstretchyboy    }
626a1e6784eSAndreas Gohr
627a1e6784eSAndreas Gohr
628b5b947d7SAndreas Gohr    /**
629fee3b689Sstretchyboy     * Escape string for sql
630fee3b689Sstretchyboy     */
631fee3b689Sstretchyboy    function escape_string($str)
632fee3b689Sstretchyboy    {
63387fa2c18Sstretchyboy      if($this->extension == DOKU_EXT_SQLITE )
63487fa2c18Sstretchyboy      {
635fee3b689Sstretchyboy        return sqlite_escape_string($str);
636fee3b689Sstretchyboy      }
63787fa2c18Sstretchyboy      else
63887fa2c18Sstretchyboy      {
63987fa2c18Sstretchyboy        return trim($this->db->quote($str), "'");
64087fa2c18Sstretchyboy      }
64187fa2c18Sstretchyboy    }
642fee3b689Sstretchyboy
643fee3b689Sstretchyboy
644ff97cc8fSstretchyboy
645fee3b689Sstretchyboy    /**
646b5b947d7SAndreas Gohr    * Aggregation function for SQLite
647b5b947d7SAndreas Gohr    *
648b5b947d7SAndreas Gohr    * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
649b5b947d7SAndreas Gohr    */
650ff97cc8fSstretchyboy    function _sqlite_group_concat_step(&$context, $string, $separator = ',') {
651ff97cc8fSstretchyboy         $context['sep'] = $separator;
652ff97cc8fSstretchyboy         $context['data'][] = $string;
653ff97cc8fSstretchyboy    }
654ff97cc8fSstretchyboy
655ff97cc8fSstretchyboy    /**
656ff97cc8fSstretchyboy    * Aggregation function for SQLite
657ff97cc8fSstretchyboy    *
658ff97cc8fSstretchyboy    * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
659ff97cc8fSstretchyboy    */
660ff97cc8fSstretchyboy    function _sqlite_group_concat_finalize(&$context) {
661ff97cc8fSstretchyboy         $context['data'] = array_unique($context['data']);
662ff97cc8fSstretchyboy         return join($context['sep'],$context['data']);
663ff97cc8fSstretchyboy    }
664ff97cc8fSstretchyboy
665ff97cc8fSstretchyboy
666ff97cc8fSstretchyboy    /**
667ff97cc8fSstretchyboy     * Aggregation function for SQLite via PDO
668ff97cc8fSstretchyboy     *
669ff97cc8fSstretchyboy     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
670ff97cc8fSstretchyboy     */
671ff97cc8fSstretchyboy    function _pdo_group_concat_step(&$context, $rownumber, $string, $separator = ',') {
67287fa2c18Sstretchyboy         if(is_null($context))
67387fa2c18Sstretchyboy         {
67487fa2c18Sstretchyboy           $context = array(
67587fa2c18Sstretchyboy             'sep'  => $separator,
67687fa2c18Sstretchyboy             'data' => array()
67787fa2c18Sstretchyboy             );
67887fa2c18Sstretchyboy         }
67987fa2c18Sstretchyboy
680b5b947d7SAndreas Gohr         $context['data'][] = $string;
68187fa2c18Sstretchyboy         return $context;
682b5b947d7SAndreas Gohr    }
683b5b947d7SAndreas Gohr
684b5b947d7SAndreas Gohr     /**
685ff97cc8fSstretchyboy     * Aggregation function for SQLite via PDO
686b5b947d7SAndreas Gohr     *
687b5b947d7SAndreas Gohr     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
688b5b947d7SAndreas Gohr     */
689ff97cc8fSstretchyboy    function _pdo_group_concat_finalize(&$context, $rownumber)
69087fa2c18Sstretchyboy    {
69187fa2c18Sstretchyboy        if(!is_array($context))
69287fa2c18Sstretchyboy        {
69387fa2c18Sstretchyboy          return null;
69487fa2c18Sstretchyboy        }
695b5b947d7SAndreas Gohr        $context['data'] = array_unique($context['data']);
696b5b947d7SAndreas Gohr        return join($context['sep'],$context['data']);
697b5b947d7SAndreas Gohr    }
698b5b947d7SAndreas Gohr
699e7112ccbSAdrian Lang    /**
700e7112ccbSAdrian Lang     * Keep separate instances for every call to keep database connections
701e7112ccbSAdrian Lang     */
702e7112ccbSAdrian Lang    function isSingleton() {
703e7112ccbSAdrian Lang         return false;
704e7112ccbSAdrian Lang    }
705fee3b689Sstretchyboy
706fee3b689Sstretchyboy     /**
707fee3b689Sstretchyboy     * fetch the next row as zero indexed array
708fee3b689Sstretchyboy     */
709fee3b689Sstretchyboy    function res_fetch_array($res)
710fee3b689Sstretchyboy    {
71187fa2c18Sstretchyboy      if($this->extension == DOKU_EXT_SQLITE )
71287fa2c18Sstretchyboy      {
713fee3b689Sstretchyboy        return sqlite_fetch_array($res, SQLITE_NUM);
714fee3b689Sstretchyboy      }
71587fa2c18Sstretchyboy      else
71687fa2c18Sstretchyboy      {
717*78977d74SKlap-in        if(!$res) return false;
718*78977d74SKlap-in
71987fa2c18Sstretchyboy        return $res->fetch(PDO::FETCH_NUM);
72087fa2c18Sstretchyboy      }
72187fa2c18Sstretchyboy    }
722fee3b689Sstretchyboy
723fee3b689Sstretchyboy
724fee3b689Sstretchyboy    /**
725fee3b689Sstretchyboy     * fetch the next row as assocative array
726fee3b689Sstretchyboy     */
727fee3b689Sstretchyboy    function res_fetch_assoc($res)
728fee3b689Sstretchyboy    {
72987fa2c18Sstretchyboy      if($this->extension == DOKU_EXT_SQLITE )
73087fa2c18Sstretchyboy      {
731fee3b689Sstretchyboy        return sqlite_fetch_array($res, SQLITE_ASSOC);
732fee3b689Sstretchyboy      }
73387fa2c18Sstretchyboy      else
73487fa2c18Sstretchyboy      {
735*78977d74SKlap-in        if(!$res) return false;
736*78977d74SKlap-in
73787fa2c18Sstretchyboy        return $res->fetch(PDO::FETCH_ASSOC);
73887fa2c18Sstretchyboy      }
73987fa2c18Sstretchyboy    }
740fee3b689Sstretchyboy
741fee3b689Sstretchyboy
742fee3b689Sstretchyboy    /**
743*78977d74SKlap-in    * Count the number of records in result
744fee3b689Sstretchyboy    */
745fee3b689Sstretchyboy    function res2count($res) {
74687fa2c18Sstretchyboy      if($this->extension == DOKU_EXT_SQLITE )
74787fa2c18Sstretchyboy      {
748fee3b689Sstretchyboy        return sqlite_num_rows($res);
749fee3b689Sstretchyboy      }
75087fa2c18Sstretchyboy      else
75187fa2c18Sstretchyboy      {
752*78977d74SKlap-in        if(!$res) return false;
753*78977d74SKlap-in
75487fa2c18Sstretchyboy        $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
75587fa2c18Sstretchyboy        if (preg_match($regex, $res->queryString, $output) > 0) {
75687fa2c18Sstretchyboy            $stmt = $this->db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM);
75787fa2c18Sstretchyboy
75887fa2c18Sstretchyboy            return $stmt->fetchColumn();
75987fa2c18Sstretchyboy        }
76087fa2c18Sstretchyboy
76187fa2c18Sstretchyboy        return false;
76287fa2c18Sstretchyboy      }
76387fa2c18Sstretchyboy    }
76424a03f6cSstretchyboy
76524a03f6cSstretchyboy
76624a03f6cSstretchyboy    /**
76724a03f6cSstretchyboy    * Count the number of records changed last time
76824a03f6cSstretchyboy    */
76924a03f6cSstretchyboy    function countChanges($db, $res)
77024a03f6cSstretchyboy    {
77187fa2c18Sstretchyboy      if($this->extension == DOKU_EXT_SQLITE )
77287fa2c18Sstretchyboy      {
77324a03f6cSstretchyboy        return sqlite_changes($db);
77424a03f6cSstretchyboy      }
77587fa2c18Sstretchyboy      else
77687fa2c18Sstretchyboy      {
777*78977d74SKlap-in        if(!$res) return false;
778*78977d74SKlap-in
77987fa2c18Sstretchyboy        return $res->rowCount();
78087fa2c18Sstretchyboy      }
78187fa2c18Sstretchyboy    }
782a1e6784eSAndreas Gohr}
783a1e6784eSAndreas Gohr
784a1e6784eSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8:
785