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 = ''; 2387fa2c18Sstretchyboy var $extension = false; 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 { 35a1e6784eSAndreas Gohr if (!extension_loaded('sqlite')) { 36a1e6784eSAndreas Gohr $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : ''; 37a1e6784eSAndreas Gohr if(function_exists('dl')) @dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX); 38a1e6784eSAndreas Gohr } 39a1e6784eSAndreas Gohr 4087fa2c18Sstretchyboy if(function_exists('sqlite_open')){ 4187fa2c18Sstretchyboy $this->extension = DOKU_EXT_SQLITE; 4287fa2c18Sstretchyboy } 4387fa2c18Sstretchyboy } 4487fa2c18Sstretchyboy 4587fa2c18Sstretchyboy if(!$this->extension) 4687fa2c18Sstretchyboy { 4787fa2c18Sstretchyboy if (!extension_loaded('pdo_sqlite')) { 4887fa2c18Sstretchyboy $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : ''; 4987fa2c18Sstretchyboy if(function_exists('dl')) @dl($prefix . 'pdo_sqlite.' . PHP_SHLIB_SUFFIX); 5087fa2c18Sstretchyboy } 5187fa2c18Sstretchyboy 5287fa2c18Sstretchyboy if(class_exists('pdo')){ 5387fa2c18Sstretchyboy $this->extension = DOKU_EXT_PDO; 5487fa2c18Sstretchyboy } 5587fa2c18Sstretchyboy } 5687fa2c18Sstretchyboy 5787fa2c18Sstretchyboy if(!$this->extension) 5887fa2c18Sstretchyboy { 5987fa2c18Sstretchyboy msg('SQLite & PDO SQLite support missing in this PHP install - plugin will not work',-1); 60a1e6784eSAndreas Gohr } 61a1e6784eSAndreas Gohr } 62a1e6784eSAndreas Gohr 63a1e6784eSAndreas Gohr /** 64a1e6784eSAndreas Gohr * Initializes and opens the database 65a1e6784eSAndreas Gohr * 66a1e6784eSAndreas Gohr * Needs to be called right after loading this helper plugin 67a1e6784eSAndreas Gohr */ 68a1e6784eSAndreas Gohr function init($dbname,$updatedir){ 69a1e6784eSAndreas Gohr global $conf; 70a1e6784eSAndreas Gohr 71a1e6784eSAndreas Gohr // check for already open DB 72a1e6784eSAndreas Gohr if($this->db){ 73a1e6784eSAndreas Gohr if($this->dbname == $dbname){ 74a1e6784eSAndreas Gohr // db already open 75a1e6784eSAndreas Gohr return true; 76a1e6784eSAndreas Gohr } 77a1e6784eSAndreas Gohr // close other db 7887fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 7987fa2c18Sstretchyboy { 80a1e6784eSAndreas Gohr sqlite_close($this->db); 8187fa2c18Sstretchyboy } 8287fa2c18Sstretchyboy else 8387fa2c18Sstretchyboy { 8487fa2c18Sstretchyboy $this->db->close(); 8587fa2c18Sstretchyboy } 86a1e6784eSAndreas Gohr $this->db = null; 87a1e6784eSAndreas Gohr $this->dbname = ''; 88a1e6784eSAndreas Gohr } 89a1e6784eSAndreas Gohr 90a1e6784eSAndreas Gohr $this->dbname = $dbname; 9187fa2c18Sstretchyboy 9287fa2c18Sstretchyboy $fileextension = '.sqlite'; 9387fa2c18Sstretchyboy if($this->extension == DOKU_EXT_PDO) 9487fa2c18Sstretchyboy { 9587fa2c18Sstretchyboy $fileextension = '.sqlite3'; 9687fa2c18Sstretchyboy } 9787fa2c18Sstretchyboy 98*ff97cc8fSstretchyboy $this->dbfile = $conf['metadir'].'/'.$dbname.$fileextension; 99*ff97cc8fSstretchyboy 100*ff97cc8fSstretchyboy $init = (!@file_exists($this->dbfile) || ((int) @filesize($this->dbfile)) < 3); 101a1e6784eSAndreas Gohr 10287fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 10387fa2c18Sstretchyboy { 104a1e6784eSAndreas Gohr $error=''; 105*ff97cc8fSstretchyboy $this->db = sqlite_open($this->dbfile, 0666, $error); 106a1e6784eSAndreas Gohr if(!$this->db){ 107a1e6784eSAndreas Gohr msg("SQLite: failed to open SQLite ".$this->dbname." database ($error)",-1); 108a1e6784eSAndreas Gohr return false; 109a1e6784eSAndreas Gohr } 110a1e6784eSAndreas Gohr 111b5b947d7SAndreas Gohr // register our custom aggregate function 112b5b947d7SAndreas Gohr sqlite_create_aggregate($this->db,'group_concat', 113b5b947d7SAndreas Gohr array($this,'_sqlite_group_concat_step'), 114b5b947d7SAndreas Gohr array($this,'_sqlite_group_concat_finalize'), 2); 11587fa2c18Sstretchyboy } 11687fa2c18Sstretchyboy else 11787fa2c18Sstretchyboy { 118*ff97cc8fSstretchyboy $dsn = 'sqlite:'.$this->dbfile; 11987fa2c18Sstretchyboy 12087fa2c18Sstretchyboy try { 12187fa2c18Sstretchyboy $this->db = new PDO($dsn); 12287fa2c18Sstretchyboy } catch (PDOException $e) { 12387fa2c18Sstretchyboy msg("SQLite: failed to open SQLite ".$this->dbname." database (".$e->getMessage().")",-1); 12487fa2c18Sstretchyboy return false; 12587fa2c18Sstretchyboy } 12687fa2c18Sstretchyboy $this->db->sqliteCreateAggregate('group_concat', 127*ff97cc8fSstretchyboy array($this,'_pdo_group_concat_step'), 128*ff97cc8fSstretchyboy array($this,'_pdo_group_concat_finalize')); 12987fa2c18Sstretchyboy } 130b5b947d7SAndreas Gohr 131a1e6784eSAndreas Gohr $this->_updatedb($init,$updatedir); 132a1e6784eSAndreas Gohr return true; 133a1e6784eSAndreas Gohr } 134a1e6784eSAndreas Gohr 135a1e6784eSAndreas Gohr /** 136a1e6784eSAndreas Gohr * Return the current Database Version 137a1e6784eSAndreas Gohr */ 138a1e6784eSAndreas Gohr function _currentDBversion(){ 139a1e6784eSAndreas Gohr $sql = "SELECT val FROM opts WHERE opt = 'dbversion';"; 140a1e6784eSAndreas Gohr $res = $this->query($sql); 141a1e6784eSAndreas Gohr if(!$res) return false; 142a1e6784eSAndreas Gohr $row = $this->res2row($res,0); 143a1e6784eSAndreas Gohr return (int) $row['val']; 144a1e6784eSAndreas Gohr } 145a1e6784eSAndreas Gohr /** 146a1e6784eSAndreas Gohr * Update the database if needed 147a1e6784eSAndreas Gohr * 148a1e6784eSAndreas Gohr * @param bool $init - true if this is a new database to initialize 149a1e6784eSAndreas Gohr * @param string $updatedir - Database update infos 150a1e6784eSAndreas Gohr */ 151a1e6784eSAndreas Gohr function _updatedb($init,$updatedir){ 152a1e6784eSAndreas Gohr if($init){ 153a1e6784eSAndreas Gohr $current = 0; 154a1e6784eSAndreas Gohr }else{ 155a1e6784eSAndreas Gohr $current = $this->_currentDBversion(); 156a1e6784eSAndreas Gohr if(!$current){ 157a1e6784eSAndreas Gohr msg('SQLite: no DB version found. '.$this->dbname.' DB probably broken.',-1); 158a1e6784eSAndreas Gohr return false; 159a1e6784eSAndreas Gohr } 160a1e6784eSAndreas Gohr } 161a1e6784eSAndreas Gohr 162a1e6784eSAndreas Gohr // in case of init, add versioning table 163a1e6784eSAndreas Gohr if($init){ 164a1e6784eSAndreas Gohr if(!$this->_runupdatefile(dirname(__FILE__).'/db.sql',0)){ 16587fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 16687fa2c18Sstretchyboy { 16787fa2c18Sstretchyboy msg('SQLite: '.$this->dbname.' database upgrade failed for version ', -1); 16887fa2c18Sstretchyboy } 169a1e6784eSAndreas Gohr return false; 170a1e6784eSAndreas Gohr } 171a1e6784eSAndreas Gohr } 172a1e6784eSAndreas Gohr 173a1e6784eSAndreas Gohr $latest = (int) trim(io_readFile($updatedir.'/latest.version')); 174a1e6784eSAndreas Gohr 175a1e6784eSAndreas Gohr // all up to date? 176a1e6784eSAndreas Gohr if($current >= $latest) return true; 177a1e6784eSAndreas Gohr for($i=$current+1; $i<=$latest; $i++){ 178a1e6784eSAndreas Gohr $file = sprintf($updatedir.'/update%04d.sql',$i); 179a1e6784eSAndreas Gohr if(file_exists($file)){ 180a1e6784eSAndreas Gohr if(!$this->_runupdatefile($file,$i)){ 181a1e6784eSAndreas Gohr msg('SQLite: '.$this->dbname.' database upgrade failed for version '.$i, -1); 182a1e6784eSAndreas Gohr 183a1e6784eSAndreas Gohr 184a1e6784eSAndreas Gohr return false; 185a1e6784eSAndreas Gohr } 186a1e6784eSAndreas Gohr } 187a1e6784eSAndreas Gohr } 188a1e6784eSAndreas Gohr return true; 189a1e6784eSAndreas Gohr } 190a1e6784eSAndreas Gohr 191a1e6784eSAndreas Gohr /** 192a1e6784eSAndreas Gohr * Updates the database structure using the given file to 193a1e6784eSAndreas Gohr * the given version. 194a1e6784eSAndreas Gohr */ 195a1e6784eSAndreas Gohr function _runupdatefile($file,$version){ 196a1e6784eSAndreas Gohr $sql = io_readFile($file,false); 197a1e6784eSAndreas Gohr 198a1e6784eSAndreas Gohr $sql = explode(";",$sql); 199a1e6784eSAndreas Gohr array_unshift($sql,'BEGIN TRANSACTION'); 200a1e6784eSAndreas Gohr array_push($sql,"INSERT OR REPLACE INTO opts (val,opt) VALUES ($version,'dbversion')"); 201a1e6784eSAndreas Gohr array_push($sql,"COMMIT TRANSACTION"); 202a1e6784eSAndreas Gohr 203a1e6784eSAndreas Gohr foreach($sql as $s){ 204a1e6784eSAndreas Gohr $s = preg_replace('!^\s*--.*$!m', '', $s); 205a1e6784eSAndreas Gohr $s = trim($s); 206a1e6784eSAndreas Gohr if(!$s) continue; 207fd69a32cSAndreas Gohr 208fd69a32cSAndreas Gohr 209a1e6784eSAndreas Gohr $res = $this->query("$s;"); 210a1e6784eSAndreas Gohr if ($res === false) { 21187fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 21287fa2c18Sstretchyboy { 213a1e6784eSAndreas Gohr sqlite_query($this->db, 'ROLLBACK TRANSACTION'); 214a1e6784eSAndreas Gohr return false; 215a1e6784eSAndreas Gohr } 216a1e6784eSAndreas Gohr } 21787fa2c18Sstretchyboy } 218a1e6784eSAndreas Gohr 219a1e6784eSAndreas Gohr return ($version == $this->_currentDBversion()); 220a1e6784eSAndreas Gohr } 221a1e6784eSAndreas Gohr 222a1e6784eSAndreas Gohr /** 223fd69a32cSAndreas Gohr * Emulate ALTER TABLE 224fd69a32cSAndreas Gohr * 225fd69a32cSAndreas Gohr * The ALTER TABLE syntax is parsed and then emulated using a 226fd69a32cSAndreas Gohr * temporary table 227fd69a32cSAndreas Gohr * 228fd69a32cSAndreas Gohr * @author <jon@jenseng.com> 229fd69a32cSAndreas Gohr * @link http://code.jenseng.com/db/ 230fb394683SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 231fd69a32cSAndreas Gohr */ 232fd69a32cSAndreas Gohr function _altertable($table,$alterdefs){ 233fb394683SAndreas Gohr 234fb394683SAndreas Gohr // load original table definition SQL 235fd69a32cSAndreas Gohr $result = $this->query("SELECT sql,name,type 236fd69a32cSAndreas Gohr FROM sqlite_master 237fd69a32cSAndreas Gohr WHERE tbl_name = '$table' 238fb394683SAndreas Gohr AND type = 'table'"); 239fb394683SAndreas Gohr 24087fa2c18Sstretchyboy if(($result === false) || ($this->extension == DOKU_EXT_SQLITE && sqlite_num_rows($result)<=0)){ 241fd69a32cSAndreas Gohr msg("ALTER TABLE failed, no such table '".hsc($table)."'",-1); 242fd69a32cSAndreas Gohr return false; 243fd69a32cSAndreas Gohr } 24487fa2c18Sstretchyboy 24587fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 24687fa2c18Sstretchyboy { 247fb394683SAndreas Gohr $row = sqlite_fetch_array($result); 24887fa2c18Sstretchyboy } 24987fa2c18Sstretchyboy else 25087fa2c18Sstretchyboy { 25187fa2c18Sstretchyboy $row = $result->fetch(PDO::FETCH_ASSOC); 25287fa2c18Sstretchyboy } 25387fa2c18Sstretchyboy 25487fa2c18Sstretchyboy if($row === false){ 25587fa2c18Sstretchyboy msg("ALTER TABLE failed, table '".hsc($table)."' had no master data",-1); 25687fa2c18Sstretchyboy return false; 25787fa2c18Sstretchyboy } 25887fa2c18Sstretchyboy 259fd69a32cSAndreas Gohr 260fb394683SAndreas Gohr // prepare temporary table SQL 261fd69a32cSAndreas Gohr $tmpname = 't'.time(); 262fd69a32cSAndreas Gohr $origsql = trim(preg_replace("/[\s]+/"," ", 263fd69a32cSAndreas Gohr str_replace(",",", ", 264fd69a32cSAndreas Gohr preg_replace('/\)$/',' )', 265fd69a32cSAndreas Gohr preg_replace("/[\(]/","( ",$row['sql'],1))))); 266fd69a32cSAndreas Gohr $createtemptableSQL = 'CREATE TEMPORARY '.substr(trim(preg_replace("'".$table."'",$tmpname,$origsql,1)),6); 267fd69a32cSAndreas Gohr $createindexsql = array(); 268fb394683SAndreas Gohr 269fb394683SAndreas Gohr // load indexes to reapply later 270fb394683SAndreas Gohr $result = $this->query("SELECT sql,name,type 271fb394683SAndreas Gohr FROM sqlite_master 272fb394683SAndreas Gohr WHERE tbl_name = '$table' 273fb394683SAndreas Gohr AND type = 'index'"); 274fb394683SAndreas Gohr if(!$result){ 275fb394683SAndreas Gohr $indexes = array(); 276fb394683SAndreas Gohr }else{ 277fb394683SAndreas Gohr $indexes = $this->res2arr($result); 278fb394683SAndreas Gohr } 279fb394683SAndreas Gohr 280fb394683SAndreas Gohr 281fd69a32cSAndreas Gohr $i = 0; 282fd69a32cSAndreas Gohr $defs = preg_split("/[,]+/",$alterdefs,-1,PREG_SPLIT_NO_EMPTY); 283fd69a32cSAndreas Gohr $prevword = $table; 284fd69a32cSAndreas Gohr $oldcols = preg_split("/[,]+/",substr(trim($createtemptableSQL),strpos(trim($createtemptableSQL),'(')+1),-1,PREG_SPLIT_NO_EMPTY); 285fd69a32cSAndreas Gohr $newcols = array(); 286fd69a32cSAndreas Gohr 287665af209SAdrian Lang for($i=0;$i<count($oldcols);$i++){ 288fd69a32cSAndreas Gohr $colparts = preg_split("/[\s]+/",$oldcols[$i],-1,PREG_SPLIT_NO_EMPTY); 289fd69a32cSAndreas Gohr $oldcols[$i] = $colparts[0]; 290fd69a32cSAndreas Gohr $newcols[$colparts[0]] = $colparts[0]; 291fd69a32cSAndreas Gohr } 292fd69a32cSAndreas Gohr $newcolumns = ''; 293fd69a32cSAndreas Gohr $oldcolumns = ''; 294fd69a32cSAndreas Gohr reset($newcols); 295fd69a32cSAndreas Gohr while(list($key,$val) = each($newcols)){ 296fd69a32cSAndreas Gohr $newcolumns .= ($newcolumns?', ':'').$val; 297fd69a32cSAndreas Gohr $oldcolumns .= ($oldcolumns?', ':'').$key; 298fd69a32cSAndreas Gohr } 299fd69a32cSAndreas Gohr $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table; 300fd69a32cSAndreas Gohr $dropoldsql = 'DROP TABLE '.$table; 301fd69a32cSAndreas Gohr $createtesttableSQL = $createtemptableSQL; 302fd69a32cSAndreas Gohr 303fd69a32cSAndreas Gohr foreach($defs as $def){ 304fd69a32cSAndreas Gohr $defparts = preg_split("/[\s]+/",$def,-1,PREG_SPLIT_NO_EMPTY); 305fd69a32cSAndreas Gohr $action = strtolower($defparts[0]); 306fd69a32cSAndreas Gohr switch($action){ 307fd69a32cSAndreas Gohr case 'add': 308665af209SAdrian Lang if(count($defparts) < 2){ 309fd69a32cSAndreas Gohr msg('ALTER TABLE: not enough arguments for ADD statement',-1); 310fd69a32cSAndreas Gohr return false; 311fd69a32cSAndreas Gohr } 312fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,strlen($createtesttableSQL)-1).','; 313665af209SAdrian Lang for($i=1;$i<count($defparts);$i++) 314fd69a32cSAndreas Gohr $createtesttableSQL.=' '.$defparts[$i]; 315fd69a32cSAndreas Gohr $createtesttableSQL.=')'; 316fd69a32cSAndreas Gohr break; 317fd69a32cSAndreas Gohr 318fd69a32cSAndreas Gohr case 'change': 319665af209SAdrian Lang if(count($defparts) <= 3){ 320fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').($defparts[2]?' '.$defparts[2]:'').'": syntax error',-1); 321fd69a32cSAndreas Gohr return false; 322fd69a32cSAndreas Gohr } 323fd69a32cSAndreas Gohr 324fd69a32cSAndreas Gohr if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){ 325fd69a32cSAndreas Gohr if($newcols[$defparts[1]] != $defparts[1]){ 326fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 327fd69a32cSAndreas Gohr return false; 328fd69a32cSAndreas Gohr } 329fd69a32cSAndreas Gohr $newcols[$defparts[1]] = $defparts[2]; 330fd69a32cSAndreas Gohr $nextcommapos = strpos($createtesttableSQL,',',$severpos); 331fd69a32cSAndreas Gohr $insertval = ''; 332665af209SAdrian Lang for($i=2;$i<count($defparts);$i++) 333fd69a32cSAndreas Gohr $insertval.=' '.$defparts[$i]; 334fd69a32cSAndreas Gohr if($nextcommapos) 335fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos).$insertval.substr($createtesttableSQL,$nextcommapos); 336fd69a32cSAndreas Gohr else 337fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1)).$insertval.')'; 338fd69a32cSAndreas Gohr } else { 339fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 340fd69a32cSAndreas Gohr return false; 341fd69a32cSAndreas Gohr } 342fd69a32cSAndreas Gohr break; 343fd69a32cSAndreas Gohr case 'drop': 344665af209SAdrian Lang if(count($defparts) < 2){ 345fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').'": syntax error',-1); 346fd69a32cSAndreas Gohr return false; 347fd69a32cSAndreas Gohr } 348fd69a32cSAndreas Gohr if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){ 349fd69a32cSAndreas Gohr $nextcommapos = strpos($createtesttableSQL,',',$severpos); 350fd69a32cSAndreas Gohr if($nextcommapos) 351fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos).substr($createtesttableSQL,$nextcommapos + 1); 352fd69a32cSAndreas Gohr else 353fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1) - 1).')'; 354fd69a32cSAndreas Gohr unset($newcols[$defparts[1]]); 355fd69a32cSAndreas Gohr }else{ 356fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 357fd69a32cSAndreas Gohr return false; 358fd69a32cSAndreas Gohr } 359fd69a32cSAndreas Gohr break; 360fd69a32cSAndreas Gohr default: 361fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$prevword.'": syntax error',-1); 362fd69a32cSAndreas Gohr return false; 363fd69a32cSAndreas Gohr } 364665af209SAdrian Lang $prevword = $defparts[count($defparts)-1]; 365fd69a32cSAndreas Gohr } 366fd69a32cSAndreas Gohr 367fd69a32cSAndreas Gohr // this block of code generates a test table simply to verify that the 368fd69a32cSAndreas Gohr // columns specifed are valid in an sql statement 369fd69a32cSAndreas Gohr // this ensures that no reserved words are used as columns, for example 370fd69a32cSAndreas Gohr $res = $this->query($createtesttableSQL); 371fd69a32cSAndreas Gohr if($res === false) return false; 372fd69a32cSAndreas Gohr 373fd69a32cSAndreas Gohr $droptempsql = 'DROP TABLE '.$tmpname; 374fd69a32cSAndreas Gohr $res = $this->query($droptempsql); 375fd69a32cSAndreas Gohr if($res === false) return false; 376fd69a32cSAndreas Gohr 377fd69a32cSAndreas Gohr 378fd69a32cSAndreas Gohr $createnewtableSQL = 'CREATE '.substr(trim(preg_replace("'".$tmpname."'",$table,$createtesttableSQL,1)),17); 379fd69a32cSAndreas Gohr $newcolumns = ''; 380fd69a32cSAndreas Gohr $oldcolumns = ''; 381fd69a32cSAndreas Gohr reset($newcols); 382fd69a32cSAndreas Gohr while(list($key,$val) = each($newcols)){ 383fd69a32cSAndreas Gohr $newcolumns .= ($newcolumns?', ':'').$val; 384fd69a32cSAndreas Gohr $oldcolumns .= ($oldcolumns?', ':'').$key; 385fd69a32cSAndreas Gohr } 386fd69a32cSAndreas Gohr 387fd69a32cSAndreas Gohr $copytonewsql = 'INSERT INTO '.$table.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$tmpname; 388fd69a32cSAndreas Gohr 389fd69a32cSAndreas Gohr $res = $this->query($createtemptableSQL); //create temp table 390fd69a32cSAndreas Gohr if($res === false) return false; 391fd69a32cSAndreas Gohr $res = $this->query($copytotempsql); //copy to table 392fd69a32cSAndreas Gohr if($res === false) return false; 393fd69a32cSAndreas Gohr $res = $this->query($dropoldsql); //drop old table 394fd69a32cSAndreas Gohr if($res === false) return false; 395fd69a32cSAndreas Gohr 396fd69a32cSAndreas Gohr $res = $this->query($createnewtableSQL); //recreate original table 397fd69a32cSAndreas Gohr if($res === false) return false; 398fd69a32cSAndreas Gohr $res = $this->query($copytonewsql); //copy back to original table 399fd69a32cSAndreas Gohr if($res === false) return false; 400fb394683SAndreas Gohr 401fb394683SAndreas Gohr foreach($indexes as $index){ // readd indexes 402fb394683SAndreas Gohr $res = $this->query($index['sql']); 403fb394683SAndreas Gohr if($res === false) return false; 404fb394683SAndreas Gohr } 405fb394683SAndreas Gohr 406fd69a32cSAndreas Gohr $res = $this->query($droptempsql); //drop temp table 407fd69a32cSAndreas Gohr if($res === false) return false; 408fd69a32cSAndreas Gohr 409fd69a32cSAndreas Gohr return $res; // return a valid resource 410fd69a32cSAndreas Gohr } 411fd69a32cSAndreas Gohr 412fd69a32cSAndreas Gohr /** 413a1e6784eSAndreas Gohr * Execute a query with the given parameters. 414a1e6784eSAndreas Gohr * 415a1e6784eSAndreas Gohr * Takes care of escaping 416a1e6784eSAndreas Gohr * 417a1e6784eSAndreas Gohr * @param string $sql - the statement 418a1e6784eSAndreas Gohr * @param arguments... 419a1e6784eSAndreas Gohr */ 420a1e6784eSAndreas Gohr function query(){ 421a1e6784eSAndreas Gohr if(!$this->db) return false; 422a1e6784eSAndreas Gohr 423a1e6784eSAndreas Gohr // get function arguments 424a1e6784eSAndreas Gohr $args = func_get_args(); 425a1e6784eSAndreas Gohr $sql = trim(array_shift($args)); 426fd69a32cSAndreas Gohr $sql = rtrim($sql,';'); 427a1e6784eSAndreas Gohr 428a1e6784eSAndreas Gohr if(!$sql){ 429a1e6784eSAndreas Gohr msg('No SQL statement given',-1); 430a1e6784eSAndreas Gohr return false; 431a1e6784eSAndreas Gohr } 432a1e6784eSAndreas Gohr 433a1e6784eSAndreas Gohr $argc = count($args); 43412f8c9c7SAdrian Lang if($argc > 0 && is_array($args[0])) { 43512f8c9c7SAdrian Lang $args = $args[0]; 43612f8c9c7SAdrian Lang $argc = count($args); 43712f8c9c7SAdrian Lang } 438a1e6784eSAndreas Gohr 439a1e6784eSAndreas Gohr // check number of arguments 440a1e6784eSAndreas Gohr if($argc < substr_count($sql,'?')){ 441a1e6784eSAndreas Gohr msg('Not enough arguments passed for statement. '. 442a1e6784eSAndreas Gohr 'Expected '.substr_count($sql,'?').' got '. 443a1e6784eSAndreas Gohr $argc.' - '.hsc($sql),-1); 444a1e6784eSAndreas Gohr return false; 445a1e6784eSAndreas Gohr } 446a1e6784eSAndreas Gohr 447a1e6784eSAndreas Gohr // explode at wildcard, then join again 448a1e6784eSAndreas Gohr $parts = explode('?',$sql,$argc+1); 449a1e6784eSAndreas Gohr $args = array_map(array($this,'quote_string'),$args); 450a1e6784eSAndreas Gohr $sql = ''; 451a1e6784eSAndreas Gohr 452a1e6784eSAndreas Gohr while( ($part = array_shift($parts)) !== null ){ 453a1e6784eSAndreas Gohr $sql .= $part; 454a1e6784eSAndreas Gohr $sql .= array_shift($args); 455a1e6784eSAndreas Gohr } 456a1e6784eSAndreas Gohr 457fd69a32cSAndreas Gohr // intercept ALTER TABLE statements 45887fa2c18Sstretchyboy $match = null; 459fd69a32cSAndreas Gohr if(preg_match('/^ALTER\s+TABLE\s+([\w\.]+)\s+(.*)/i',$sql,$match)){ 460fd69a32cSAndreas Gohr return $this->_altertable($match[1],$match[2]); 461fd69a32cSAndreas Gohr } 462fd69a32cSAndreas Gohr 463a1e6784eSAndreas Gohr // execute query 464a1e6784eSAndreas Gohr $err = ''; 46587fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 46687fa2c18Sstretchyboy { 467a1e6784eSAndreas Gohr $res = @sqlite_query($this->db,$sql,SQLITE_ASSOC,$err); 468a1e6784eSAndreas Gohr if($err){ 469d9cff31cSAndreas Gohr msg($err.':<br /><pre>'.hsc($sql).'</pre>',-1); 470a1e6784eSAndreas Gohr return false; 471a1e6784eSAndreas Gohr }elseif(!$res){ 472a1e6784eSAndreas Gohr msg(sqlite_error_string(sqlite_last_error($this->db)). 473d9cff31cSAndreas Gohr ':<br /><pre>'.hsc($sql).'</pre>',-1); 474a1e6784eSAndreas Gohr return false; 475a1e6784eSAndreas Gohr } 47687fa2c18Sstretchyboy } 47787fa2c18Sstretchyboy else 47887fa2c18Sstretchyboy { 47987fa2c18Sstretchyboy $result = false; 480a1e6784eSAndreas Gohr 48187fa2c18Sstretchyboy $res = $this->db->query($sql); 48287fa2c18Sstretchyboy 48387fa2c18Sstretchyboy if(!$res){ 48487fa2c18Sstretchyboy $err = $this->db->errorInfo(); 48587fa2c18Sstretchyboy msg($err[2].':<br /><pre>'.hsc($sql).'</pre>',-1); 48687fa2c18Sstretchyboy return false; 48787fa2c18Sstretchyboy } 48887fa2c18Sstretchyboy } 489a1e6784eSAndreas Gohr return $res; 490a1e6784eSAndreas Gohr } 491a1e6784eSAndreas Gohr 492a1e6784eSAndreas Gohr /** 493a1e6784eSAndreas Gohr * Returns a complete result set as array 494a1e6784eSAndreas Gohr */ 495a1e6784eSAndreas Gohr function res2arr($res){ 496a1e6784eSAndreas Gohr $data = array(); 49787fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 49887fa2c18Sstretchyboy { 499a1e6784eSAndreas Gohr if(!sqlite_num_rows($res)) return $data; 500a1e6784eSAndreas Gohr sqlite_rewind($res); 501a1e6784eSAndreas Gohr while(($row = sqlite_fetch_array($res)) !== false){ 502a1e6784eSAndreas Gohr $data[] = $row; 503a1e6784eSAndreas Gohr } 50487fa2c18Sstretchyboy } 50587fa2c18Sstretchyboy else 50687fa2c18Sstretchyboy { 50787fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_ASSOC); 50887fa2c18Sstretchyboy if(!count(data)) 50987fa2c18Sstretchyboy { 51087fa2c18Sstretchyboy return false; 51187fa2c18Sstretchyboy } 51287fa2c18Sstretchyboy } 513a1e6784eSAndreas Gohr return $data; 514a1e6784eSAndreas Gohr } 515a1e6784eSAndreas Gohr 516a1e6784eSAndreas Gohr /** 517a1e6784eSAndreas Gohr * Return the wanted row from a given result set as 518a1e6784eSAndreas Gohr * associative array 519a1e6784eSAndreas Gohr */ 520a1e6784eSAndreas Gohr function res2row($res,$rownum=0){ 52187fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 52287fa2c18Sstretchyboy { 523a1e6784eSAndreas Gohr if(!@sqlite_seek($res,$rownum)){ 524a1e6784eSAndreas Gohr return false; 525a1e6784eSAndreas Gohr } 526a1e6784eSAndreas Gohr return sqlite_fetch_array($res); 527a1e6784eSAndreas Gohr } 52887fa2c18Sstretchyboy else 52987fa2c18Sstretchyboy { 53087fa2c18Sstretchyboy //very dirty replication of the same functionality (really must look at cursors) 53187fa2c18Sstretchyboy $data = array(); 53287fa2c18Sstretchyboy //do we need to rewind? 53387fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_ASSOC); 53487fa2c18Sstretchyboy if(!count(data)) 53587fa2c18Sstretchyboy { 53687fa2c18Sstretchyboy return false; 53787fa2c18Sstretchyboy } 53887fa2c18Sstretchyboy 53987fa2c18Sstretchyboy if(!isset($data[$rownum])) 54087fa2c18Sstretchyboy { 54187fa2c18Sstretchyboy return false; 54287fa2c18Sstretchyboy } 54387fa2c18Sstretchyboy else 54487fa2c18Sstretchyboy { 54587fa2c18Sstretchyboy return $data[$rownum]; 54687fa2c18Sstretchyboy } 54787fa2c18Sstretchyboy } 54887fa2c18Sstretchyboy } 549a1e6784eSAndreas Gohr 550fee3b689Sstretchyboy /** 551fee3b689Sstretchyboy * Return the first value from the first row. 552fee3b689Sstretchyboy */ 553fee3b689Sstretchyboy function res2single($res) 554fee3b689Sstretchyboy { 55587fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 55687fa2c18Sstretchyboy { 557fee3b689Sstretchyboy return sqlite_fetch_single($res); 558fee3b689Sstretchyboy } 55987fa2c18Sstretchyboy else 56087fa2c18Sstretchyboy { 56187fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_NUM); 56287fa2c18Sstretchyboy if(!count(data)) 56387fa2c18Sstretchyboy { 56487fa2c18Sstretchyboy return false; 56587fa2c18Sstretchyboy } 56687fa2c18Sstretchyboy return $data[0][0]; 56787fa2c18Sstretchyboy } 56887fa2c18Sstretchyboy } 569a1e6784eSAndreas Gohr 570a1e6784eSAndreas Gohr /** 571a1e6784eSAndreas Gohr * Join the given values and quote them for SQL insertion 572a1e6784eSAndreas Gohr */ 573a1e6784eSAndreas Gohr function quote_and_join($vals,$sep=',') { 574a1e6784eSAndreas Gohr $vals = array_map(array('helper_plugin_sqlite','quote_string'),$vals); 575a1e6784eSAndreas Gohr return join($sep,$vals); 576a1e6784eSAndreas Gohr } 577a1e6784eSAndreas Gohr 578a1e6784eSAndreas Gohr /** 579a1e6784eSAndreas Gohr * Run sqlite_escape_string() on the given string and surround it 580a1e6784eSAndreas Gohr * with quotes 581a1e6784eSAndreas Gohr */ 582a1e6784eSAndreas Gohr function quote_string($string){ 58387fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 58487fa2c18Sstretchyboy { 585a1e6784eSAndreas Gohr return "'".sqlite_escape_string($string)."'"; 586a1e6784eSAndreas Gohr } 58787fa2c18Sstretchyboy else 58887fa2c18Sstretchyboy { 58987fa2c18Sstretchyboy return $this->db->quote($string); 59087fa2c18Sstretchyboy } 59187fa2c18Sstretchyboy } 592a1e6784eSAndreas Gohr 593a1e6784eSAndreas Gohr 594b5b947d7SAndreas Gohr /** 595fee3b689Sstretchyboy * Escape string for sql 596fee3b689Sstretchyboy */ 597fee3b689Sstretchyboy function escape_string($str) 598fee3b689Sstretchyboy { 59987fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 60087fa2c18Sstretchyboy { 601fee3b689Sstretchyboy return sqlite_escape_string($str); 602fee3b689Sstretchyboy } 60387fa2c18Sstretchyboy else 60487fa2c18Sstretchyboy { 60587fa2c18Sstretchyboy return trim($this->db->quote($str), "'"); 60687fa2c18Sstretchyboy } 60787fa2c18Sstretchyboy } 608fee3b689Sstretchyboy 609fee3b689Sstretchyboy 610*ff97cc8fSstretchyboy 611fee3b689Sstretchyboy /** 612b5b947d7SAndreas Gohr * Aggregation function for SQLite 613b5b947d7SAndreas Gohr * 614b5b947d7SAndreas Gohr * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 615b5b947d7SAndreas Gohr */ 616*ff97cc8fSstretchyboy function _sqlite_group_concat_step(&$context, $string, $separator = ',') { 617*ff97cc8fSstretchyboy $context['sep'] = $separator; 618*ff97cc8fSstretchyboy $context['data'][] = $string; 619*ff97cc8fSstretchyboy } 620*ff97cc8fSstretchyboy 621*ff97cc8fSstretchyboy /** 622*ff97cc8fSstretchyboy * Aggregation function for SQLite 623*ff97cc8fSstretchyboy * 624*ff97cc8fSstretchyboy * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 625*ff97cc8fSstretchyboy */ 626*ff97cc8fSstretchyboy function _sqlite_group_concat_finalize(&$context) { 627*ff97cc8fSstretchyboy $context['data'] = array_unique($context['data']); 628*ff97cc8fSstretchyboy return join($context['sep'],$context['data']); 629*ff97cc8fSstretchyboy } 630*ff97cc8fSstretchyboy 631*ff97cc8fSstretchyboy 632*ff97cc8fSstretchyboy /** 633*ff97cc8fSstretchyboy * Aggregation function for SQLite via PDO 634*ff97cc8fSstretchyboy * 635*ff97cc8fSstretchyboy * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 636*ff97cc8fSstretchyboy */ 637*ff97cc8fSstretchyboy function _pdo_group_concat_step(&$context, $rownumber, $string, $separator = ',') { 63887fa2c18Sstretchyboy if(is_null($context)) 63987fa2c18Sstretchyboy { 64087fa2c18Sstretchyboy $context = array( 64187fa2c18Sstretchyboy 'sep' => $separator, 64287fa2c18Sstretchyboy 'data' => array() 64387fa2c18Sstretchyboy ); 64487fa2c18Sstretchyboy } 64587fa2c18Sstretchyboy 646b5b947d7SAndreas Gohr $context['data'][] = $string; 64787fa2c18Sstretchyboy return $context; 648b5b947d7SAndreas Gohr } 649b5b947d7SAndreas Gohr 650b5b947d7SAndreas Gohr /** 651*ff97cc8fSstretchyboy * Aggregation function for SQLite via PDO 652b5b947d7SAndreas Gohr * 653b5b947d7SAndreas Gohr * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 654b5b947d7SAndreas Gohr */ 655*ff97cc8fSstretchyboy function _pdo_group_concat_finalize(&$context, $rownumber) 65687fa2c18Sstretchyboy { 65787fa2c18Sstretchyboy if(!is_array($context)) 65887fa2c18Sstretchyboy { 65987fa2c18Sstretchyboy return null; 66087fa2c18Sstretchyboy } 661b5b947d7SAndreas Gohr $context['data'] = array_unique($context['data']); 662b5b947d7SAndreas Gohr return join($context['sep'],$context['data']); 663b5b947d7SAndreas Gohr } 664b5b947d7SAndreas Gohr 665e7112ccbSAdrian Lang /** 666e7112ccbSAdrian Lang * Keep separate instances for every call to keep database connections 667e7112ccbSAdrian Lang */ 668e7112ccbSAdrian Lang function isSingleton() { 669e7112ccbSAdrian Lang return false; 670e7112ccbSAdrian Lang } 671fee3b689Sstretchyboy 672fee3b689Sstretchyboy /** 673fee3b689Sstretchyboy * fetch the next row as zero indexed array 674fee3b689Sstretchyboy */ 675fee3b689Sstretchyboy function res_fetch_array($res) 676fee3b689Sstretchyboy { 67787fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 67887fa2c18Sstretchyboy { 679fee3b689Sstretchyboy return sqlite_fetch_array($res, SQLITE_NUM); 680fee3b689Sstretchyboy } 68187fa2c18Sstretchyboy else 68287fa2c18Sstretchyboy { 68387fa2c18Sstretchyboy return $res->fetch(PDO::FETCH_NUM); 68487fa2c18Sstretchyboy } 68587fa2c18Sstretchyboy } 686fee3b689Sstretchyboy 687fee3b689Sstretchyboy 688fee3b689Sstretchyboy /** 689fee3b689Sstretchyboy * fetch the next row as assocative array 690fee3b689Sstretchyboy */ 691fee3b689Sstretchyboy function res_fetch_assoc($res) 692fee3b689Sstretchyboy { 69387fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 69487fa2c18Sstretchyboy { 695fee3b689Sstretchyboy return sqlite_fetch_array($res, SQLITE_ASSOC); 696fee3b689Sstretchyboy } 69787fa2c18Sstretchyboy else 69887fa2c18Sstretchyboy { 69987fa2c18Sstretchyboy return $res->fetch(PDO::FETCH_ASSOC); 70087fa2c18Sstretchyboy } 70187fa2c18Sstretchyboy } 702fee3b689Sstretchyboy 703fee3b689Sstretchyboy 704fee3b689Sstretchyboy /** 705fee3b689Sstretchyboy * Count the number of records in rsult 706fee3b689Sstretchyboy */ 707fee3b689Sstretchyboy function res2count($res) { 70887fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 70987fa2c18Sstretchyboy { 710fee3b689Sstretchyboy return sqlite_num_rows($res); 711fee3b689Sstretchyboy } 71287fa2c18Sstretchyboy else 71387fa2c18Sstretchyboy { 71487fa2c18Sstretchyboy $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i'; 71587fa2c18Sstretchyboy if (preg_match($regex, $res->queryString, $output) > 0) { 71687fa2c18Sstretchyboy $stmt = $this->db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM); 71787fa2c18Sstretchyboy 71887fa2c18Sstretchyboy return $stmt->fetchColumn(); 71987fa2c18Sstretchyboy } 72087fa2c18Sstretchyboy 72187fa2c18Sstretchyboy return false; 72287fa2c18Sstretchyboy } 72387fa2c18Sstretchyboy } 72424a03f6cSstretchyboy 72524a03f6cSstretchyboy 72624a03f6cSstretchyboy /** 72724a03f6cSstretchyboy * Count the number of records changed last time 72824a03f6cSstretchyboy */ 72924a03f6cSstretchyboy function countChanges($db, $res) 73024a03f6cSstretchyboy { 73187fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 73287fa2c18Sstretchyboy { 73324a03f6cSstretchyboy return sqlite_changes($db); 73424a03f6cSstretchyboy } 73587fa2c18Sstretchyboy else 73687fa2c18Sstretchyboy { 73787fa2c18Sstretchyboy return $res->rowCount(); 73887fa2c18Sstretchyboy } 73987fa2c18Sstretchyboy } 740a1e6784eSAndreas Gohr} 741a1e6784eSAndreas Gohr 742a1e6784eSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8: 743