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'); 18*4d9093b4Sstretchyboyif (!defined('DOKU_DEFAULT_EXT')) define('DOKU_DEFAULT_EXT', DOKU_EXT_SQLITE); 1987fa2c18Sstretchyboy 2087fa2c18Sstretchyboy 21a1e6784eSAndreas Gohrclass helper_plugin_sqlite extends DokuWiki_Plugin { 22a1e6784eSAndreas Gohr var $db = null; 23a1e6784eSAndreas Gohr var $dbname = ''; 24*4d9093b4Sstretchyboy var $extension = DOKU_DEFAULT_EXT; 25a1e6784eSAndreas Gohr function getInfo() { 26a1e6784eSAndreas Gohr return confToHash(dirname(__FILE__).'plugin.info.txt'); 27a1e6784eSAndreas Gohr } 28a1e6784eSAndreas Gohr 29a1e6784eSAndreas Gohr /** 30a1e6784eSAndreas Gohr * constructor 31a1e6784eSAndreas Gohr */ 32a1e6784eSAndreas Gohr function helper_plugin_sqlite(){ 3387fa2c18Sstretchyboy 3487fa2c18Sstretchyboy if(!$this->extension) 3587fa2c18Sstretchyboy { 36a1e6784eSAndreas Gohr if (!extension_loaded('sqlite')) { 37a1e6784eSAndreas Gohr $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : ''; 38a1e6784eSAndreas Gohr if(function_exists('dl')) @dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX); 39a1e6784eSAndreas Gohr } 40a1e6784eSAndreas Gohr 4187fa2c18Sstretchyboy if(function_exists('sqlite_open')){ 4287fa2c18Sstretchyboy $this->extension = DOKU_EXT_SQLITE; 4387fa2c18Sstretchyboy } 4487fa2c18Sstretchyboy } 4587fa2c18Sstretchyboy 4687fa2c18Sstretchyboy if(!$this->extension) 4787fa2c18Sstretchyboy { 4887fa2c18Sstretchyboy if (!extension_loaded('pdo_sqlite')) { 4987fa2c18Sstretchyboy $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : ''; 5087fa2c18Sstretchyboy if(function_exists('dl')) @dl($prefix . 'pdo_sqlite.' . PHP_SHLIB_SUFFIX); 5187fa2c18Sstretchyboy } 5287fa2c18Sstretchyboy 5387fa2c18Sstretchyboy if(class_exists('pdo')){ 5487fa2c18Sstretchyboy $this->extension = DOKU_EXT_PDO; 5587fa2c18Sstretchyboy } 5687fa2c18Sstretchyboy } 5787fa2c18Sstretchyboy 5887fa2c18Sstretchyboy if(!$this->extension) 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 9387fa2c18Sstretchyboy $fileextension = '.sqlite'; 9487fa2c18Sstretchyboy if($this->extension == DOKU_EXT_PDO) 9587fa2c18Sstretchyboy { 9687fa2c18Sstretchyboy $fileextension = '.sqlite3'; 9787fa2c18Sstretchyboy } 9887fa2c18Sstretchyboy 99ff97cc8fSstretchyboy $this->dbfile = $conf['metadir'].'/'.$dbname.$fileextension; 100ff97cc8fSstretchyboy 101ff97cc8fSstretchyboy $init = (!@file_exists($this->dbfile) || ((int) @filesize($this->dbfile)) < 3); 102a1e6784eSAndreas Gohr 10387fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 10487fa2c18Sstretchyboy { 105a1e6784eSAndreas Gohr $error=''; 106ff97cc8fSstretchyboy $this->db = sqlite_open($this->dbfile, 0666, $error); 107a1e6784eSAndreas Gohr if(!$this->db){ 108a1e6784eSAndreas Gohr msg("SQLite: failed to open SQLite ".$this->dbname." database ($error)",-1); 109a1e6784eSAndreas Gohr return false; 110a1e6784eSAndreas Gohr } 111a1e6784eSAndreas Gohr 112b5b947d7SAndreas Gohr // register our custom aggregate function 113b5b947d7SAndreas Gohr sqlite_create_aggregate($this->db,'group_concat', 114b5b947d7SAndreas Gohr array($this,'_sqlite_group_concat_step'), 115b5b947d7SAndreas Gohr array($this,'_sqlite_group_concat_finalize'), 2); 11687fa2c18Sstretchyboy } 11787fa2c18Sstretchyboy else 11887fa2c18Sstretchyboy { 119ff97cc8fSstretchyboy $dsn = 'sqlite:'.$this->dbfile; 12087fa2c18Sstretchyboy 12187fa2c18Sstretchyboy try { 12287fa2c18Sstretchyboy $this->db = new PDO($dsn); 12387fa2c18Sstretchyboy } catch (PDOException $e) { 12487fa2c18Sstretchyboy msg("SQLite: failed to open SQLite ".$this->dbname." database (".$e->getMessage().")",-1); 12587fa2c18Sstretchyboy return false; 12687fa2c18Sstretchyboy } 12787fa2c18Sstretchyboy $this->db->sqliteCreateAggregate('group_concat', 128ff97cc8fSstretchyboy array($this,'_pdo_group_concat_step'), 129ff97cc8fSstretchyboy array($this,'_pdo_group_concat_finalize')); 13087fa2c18Sstretchyboy } 131b5b947d7SAndreas Gohr 132a1e6784eSAndreas Gohr $this->_updatedb($init,$updatedir); 133a1e6784eSAndreas Gohr return true; 134a1e6784eSAndreas Gohr } 135a1e6784eSAndreas Gohr 136a1e6784eSAndreas Gohr /** 137a1e6784eSAndreas Gohr * Return the current Database Version 138a1e6784eSAndreas Gohr */ 139a1e6784eSAndreas Gohr function _currentDBversion(){ 140a1e6784eSAndreas Gohr $sql = "SELECT val FROM opts WHERE opt = 'dbversion';"; 141a1e6784eSAndreas Gohr $res = $this->query($sql); 142a1e6784eSAndreas Gohr if(!$res) return false; 143a1e6784eSAndreas Gohr $row = $this->res2row($res,0); 144a1e6784eSAndreas Gohr return (int) $row['val']; 145a1e6784eSAndreas Gohr } 146a1e6784eSAndreas Gohr /** 147a1e6784eSAndreas Gohr * Update the database if needed 148a1e6784eSAndreas Gohr * 149a1e6784eSAndreas Gohr * @param bool $init - true if this is a new database to initialize 150a1e6784eSAndreas Gohr * @param string $updatedir - Database update infos 151a1e6784eSAndreas Gohr */ 152*4d9093b4Sstretchyboy function _updatedb($init,$updatedir) 153*4d9093b4Sstretchyboy { 154a1e6784eSAndreas Gohr if($init){ 155*4d9093b4Sstretchyboy 156a1e6784eSAndreas Gohr $current = 0; 157a1e6784eSAndreas Gohr }else{ 158a1e6784eSAndreas Gohr $current = $this->_currentDBversion(); 159a1e6784eSAndreas Gohr if(!$current){ 160a1e6784eSAndreas Gohr msg('SQLite: no DB version found. '.$this->dbname.' DB probably broken.',-1); 161a1e6784eSAndreas Gohr return false; 162a1e6784eSAndreas Gohr } 163a1e6784eSAndreas Gohr } 164a1e6784eSAndreas Gohr 165a1e6784eSAndreas Gohr // in case of init, add versioning table 166a1e6784eSAndreas Gohr if($init){ 167a1e6784eSAndreas Gohr if(!$this->_runupdatefile(dirname(__FILE__).'/db.sql',0)){ 16887fa2c18Sstretchyboy msg('SQLite: '.$this->dbname.' database upgrade failed for version ', -1); 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 return false; 183a1e6784eSAndreas Gohr } 184a1e6784eSAndreas Gohr } 185a1e6784eSAndreas Gohr } 186a1e6784eSAndreas Gohr return true; 187a1e6784eSAndreas Gohr } 188a1e6784eSAndreas Gohr 189a1e6784eSAndreas Gohr /** 190a1e6784eSAndreas Gohr * Updates the database structure using the given file to 191a1e6784eSAndreas Gohr * the given version. 192a1e6784eSAndreas Gohr */ 193a1e6784eSAndreas Gohr function _runupdatefile($file,$version){ 194a1e6784eSAndreas Gohr $sql = io_readFile($file,false); 195a1e6784eSAndreas Gohr 196a1e6784eSAndreas Gohr $sql = explode(";",$sql); 197a1e6784eSAndreas Gohr array_unshift($sql,'BEGIN TRANSACTION'); 198a1e6784eSAndreas Gohr array_push($sql,"INSERT OR REPLACE INTO opts (val,opt) VALUES ($version,'dbversion')"); 199a1e6784eSAndreas Gohr array_push($sql,"COMMIT TRANSACTION"); 200a1e6784eSAndreas Gohr 201a1e6784eSAndreas Gohr foreach($sql as $s){ 202a1e6784eSAndreas Gohr $s = preg_replace('!^\s*--.*$!m', '', $s); 203a1e6784eSAndreas Gohr $s = trim($s); 204a1e6784eSAndreas Gohr if(!$s) continue; 205fd69a32cSAndreas Gohr 206fd69a32cSAndreas Gohr 207a1e6784eSAndreas Gohr $res = $this->query("$s;"); 208a1e6784eSAndreas Gohr if ($res === false) { 20987fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 21087fa2c18Sstretchyboy { 211a1e6784eSAndreas Gohr sqlite_query($this->db, 'ROLLBACK TRANSACTION'); 212a1e6784eSAndreas Gohr return false; 213a1e6784eSAndreas Gohr } 21405f176edSstretchyboy else 21505f176edSstretchyboy { 21605f176edSstretchyboy return false; 21705f176edSstretchyboy } 218a1e6784eSAndreas Gohr } 21987fa2c18Sstretchyboy } 220a1e6784eSAndreas Gohr 221a1e6784eSAndreas Gohr return ($version == $this->_currentDBversion()); 222a1e6784eSAndreas Gohr } 223a1e6784eSAndreas Gohr 224a1e6784eSAndreas Gohr /** 225fd69a32cSAndreas Gohr * Emulate ALTER TABLE 226fd69a32cSAndreas Gohr * 227fd69a32cSAndreas Gohr * The ALTER TABLE syntax is parsed and then emulated using a 228fd69a32cSAndreas Gohr * temporary table 229fd69a32cSAndreas Gohr * 230fd69a32cSAndreas Gohr * @author <jon@jenseng.com> 231fd69a32cSAndreas Gohr * @link http://code.jenseng.com/db/ 232fb394683SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 233fd69a32cSAndreas Gohr */ 234fd69a32cSAndreas Gohr function _altertable($table,$alterdefs){ 235fb394683SAndreas Gohr 236fb394683SAndreas Gohr // load original table definition SQL 237fd69a32cSAndreas Gohr $result = $this->query("SELECT sql,name,type 238fd69a32cSAndreas Gohr FROM sqlite_master 239fd69a32cSAndreas Gohr WHERE tbl_name = '$table' 240fb394683SAndreas Gohr AND type = 'table'"); 241fb394683SAndreas Gohr 24287fa2c18Sstretchyboy if(($result === false) || ($this->extension == DOKU_EXT_SQLITE && sqlite_num_rows($result)<=0)){ 243fd69a32cSAndreas Gohr msg("ALTER TABLE failed, no such table '".hsc($table)."'",-1); 244fd69a32cSAndreas Gohr return false; 245fd69a32cSAndreas Gohr } 24687fa2c18Sstretchyboy 24787fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 24887fa2c18Sstretchyboy { 249fb394683SAndreas Gohr $row = sqlite_fetch_array($result); 25087fa2c18Sstretchyboy } 25187fa2c18Sstretchyboy else 25287fa2c18Sstretchyboy { 25387fa2c18Sstretchyboy $row = $result->fetch(PDO::FETCH_ASSOC); 25487fa2c18Sstretchyboy } 25587fa2c18Sstretchyboy 25687fa2c18Sstretchyboy if($row === false){ 25787fa2c18Sstretchyboy msg("ALTER TABLE failed, table '".hsc($table)."' had no master data",-1); 25887fa2c18Sstretchyboy return false; 25987fa2c18Sstretchyboy } 26087fa2c18Sstretchyboy 261fd69a32cSAndreas Gohr 262fb394683SAndreas Gohr // prepare temporary table SQL 263fd69a32cSAndreas Gohr $tmpname = 't'.time(); 264fd69a32cSAndreas Gohr $origsql = trim(preg_replace("/[\s]+/"," ", 265fd69a32cSAndreas Gohr str_replace(",",", ", 266fd69a32cSAndreas Gohr preg_replace('/\)$/',' )', 267fd69a32cSAndreas Gohr preg_replace("/[\(]/","( ",$row['sql'],1))))); 268fd69a32cSAndreas Gohr $createtemptableSQL = 'CREATE TEMPORARY '.substr(trim(preg_replace("'".$table."'",$tmpname,$origsql,1)),6); 269fd69a32cSAndreas Gohr $createindexsql = array(); 270fb394683SAndreas Gohr 271fb394683SAndreas Gohr // load indexes to reapply later 272fb394683SAndreas Gohr $result = $this->query("SELECT sql,name,type 273fb394683SAndreas Gohr FROM sqlite_master 274fb394683SAndreas Gohr WHERE tbl_name = '$table' 275fb394683SAndreas Gohr AND type = 'index'"); 276fb394683SAndreas Gohr if(!$result){ 277fb394683SAndreas Gohr $indexes = array(); 278fb394683SAndreas Gohr }else{ 279fb394683SAndreas Gohr $indexes = $this->res2arr($result); 280fb394683SAndreas Gohr } 281fb394683SAndreas Gohr 282fb394683SAndreas Gohr 283fd69a32cSAndreas Gohr $i = 0; 284fd69a32cSAndreas Gohr $defs = preg_split("/[,]+/",$alterdefs,-1,PREG_SPLIT_NO_EMPTY); 285fd69a32cSAndreas Gohr $prevword = $table; 286fd69a32cSAndreas Gohr $oldcols = preg_split("/[,]+/",substr(trim($createtemptableSQL),strpos(trim($createtemptableSQL),'(')+1),-1,PREG_SPLIT_NO_EMPTY); 287fd69a32cSAndreas Gohr $newcols = array(); 288fd69a32cSAndreas Gohr 289665af209SAdrian Lang for($i=0;$i<count($oldcols);$i++){ 290fd69a32cSAndreas Gohr $colparts = preg_split("/[\s]+/",$oldcols[$i],-1,PREG_SPLIT_NO_EMPTY); 291fd69a32cSAndreas Gohr $oldcols[$i] = $colparts[0]; 292fd69a32cSAndreas Gohr $newcols[$colparts[0]] = $colparts[0]; 293fd69a32cSAndreas Gohr } 294fd69a32cSAndreas Gohr $newcolumns = ''; 295fd69a32cSAndreas Gohr $oldcolumns = ''; 296fd69a32cSAndreas Gohr reset($newcols); 297fd69a32cSAndreas Gohr while(list($key,$val) = each($newcols)){ 298fd69a32cSAndreas Gohr $newcolumns .= ($newcolumns?', ':'').$val; 299fd69a32cSAndreas Gohr $oldcolumns .= ($oldcolumns?', ':'').$key; 300fd69a32cSAndreas Gohr } 301fd69a32cSAndreas Gohr $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table; 302fd69a32cSAndreas Gohr $dropoldsql = 'DROP TABLE '.$table; 303fd69a32cSAndreas Gohr $createtesttableSQL = $createtemptableSQL; 304fd69a32cSAndreas Gohr 305fd69a32cSAndreas Gohr foreach($defs as $def){ 306fd69a32cSAndreas Gohr $defparts = preg_split("/[\s]+/",$def,-1,PREG_SPLIT_NO_EMPTY); 307fd69a32cSAndreas Gohr $action = strtolower($defparts[0]); 308fd69a32cSAndreas Gohr switch($action){ 309fd69a32cSAndreas Gohr case 'add': 310665af209SAdrian Lang if(count($defparts) < 2){ 311fd69a32cSAndreas Gohr msg('ALTER TABLE: not enough arguments for ADD statement',-1); 312fd69a32cSAndreas Gohr return false; 313fd69a32cSAndreas Gohr } 314fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,strlen($createtesttableSQL)-1).','; 315665af209SAdrian Lang for($i=1;$i<count($defparts);$i++) 316fd69a32cSAndreas Gohr $createtesttableSQL.=' '.$defparts[$i]; 317fd69a32cSAndreas Gohr $createtesttableSQL.=')'; 318fd69a32cSAndreas Gohr break; 319fd69a32cSAndreas Gohr 320fd69a32cSAndreas Gohr case 'change': 321665af209SAdrian Lang if(count($defparts) <= 3){ 322fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').($defparts[2]?' '.$defparts[2]:'').'": syntax error',-1); 323fd69a32cSAndreas Gohr return false; 324fd69a32cSAndreas Gohr } 325fd69a32cSAndreas Gohr 326fd69a32cSAndreas Gohr if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){ 327fd69a32cSAndreas Gohr if($newcols[$defparts[1]] != $defparts[1]){ 328fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 329fd69a32cSAndreas Gohr return false; 330fd69a32cSAndreas Gohr } 331fd69a32cSAndreas Gohr $newcols[$defparts[1]] = $defparts[2]; 332fd69a32cSAndreas Gohr $nextcommapos = strpos($createtesttableSQL,',',$severpos); 333fd69a32cSAndreas Gohr $insertval = ''; 334665af209SAdrian Lang for($i=2;$i<count($defparts);$i++) 335fd69a32cSAndreas Gohr $insertval.=' '.$defparts[$i]; 336fd69a32cSAndreas Gohr if($nextcommapos) 337fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos).$insertval.substr($createtesttableSQL,$nextcommapos); 338fd69a32cSAndreas Gohr else 339fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1)).$insertval.')'; 340fd69a32cSAndreas Gohr } else { 341fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 342fd69a32cSAndreas Gohr return false; 343fd69a32cSAndreas Gohr } 344fd69a32cSAndreas Gohr break; 345fd69a32cSAndreas Gohr case 'drop': 346665af209SAdrian Lang if(count($defparts) < 2){ 347fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').'": syntax error',-1); 348fd69a32cSAndreas Gohr return false; 349fd69a32cSAndreas Gohr } 350fd69a32cSAndreas Gohr if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){ 351fd69a32cSAndreas Gohr $nextcommapos = strpos($createtesttableSQL,',',$severpos); 352fd69a32cSAndreas Gohr if($nextcommapos) 353fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos).substr($createtesttableSQL,$nextcommapos + 1); 354fd69a32cSAndreas Gohr else 355fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1) - 1).')'; 356fd69a32cSAndreas Gohr unset($newcols[$defparts[1]]); 357fd69a32cSAndreas Gohr }else{ 358fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 359fd69a32cSAndreas Gohr return false; 360fd69a32cSAndreas Gohr } 361fd69a32cSAndreas Gohr break; 362fd69a32cSAndreas Gohr default: 363fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$prevword.'": syntax error',-1); 364fd69a32cSAndreas Gohr return false; 365fd69a32cSAndreas Gohr } 366665af209SAdrian Lang $prevword = $defparts[count($defparts)-1]; 367fd69a32cSAndreas Gohr } 368fd69a32cSAndreas Gohr 369fd69a32cSAndreas Gohr // this block of code generates a test table simply to verify that the 370fd69a32cSAndreas Gohr // columns specifed are valid in an sql statement 371fd69a32cSAndreas Gohr // this ensures that no reserved words are used as columns, for example 372fd69a32cSAndreas Gohr $res = $this->query($createtesttableSQL); 373fd69a32cSAndreas Gohr if($res === false) return false; 374fd69a32cSAndreas Gohr 375fd69a32cSAndreas Gohr $droptempsql = 'DROP TABLE '.$tmpname; 376fd69a32cSAndreas Gohr $res = $this->query($droptempsql); 377fd69a32cSAndreas Gohr if($res === false) return false; 378fd69a32cSAndreas Gohr 379fd69a32cSAndreas Gohr 380fd69a32cSAndreas Gohr $createnewtableSQL = 'CREATE '.substr(trim(preg_replace("'".$tmpname."'",$table,$createtesttableSQL,1)),17); 381fd69a32cSAndreas Gohr $newcolumns = ''; 382fd69a32cSAndreas Gohr $oldcolumns = ''; 383fd69a32cSAndreas Gohr reset($newcols); 384fd69a32cSAndreas Gohr while(list($key,$val) = each($newcols)){ 385fd69a32cSAndreas Gohr $newcolumns .= ($newcolumns?', ':'').$val; 386fd69a32cSAndreas Gohr $oldcolumns .= ($oldcolumns?', ':'').$key; 387fd69a32cSAndreas Gohr } 388fd69a32cSAndreas Gohr 389fd69a32cSAndreas Gohr $copytonewsql = 'INSERT INTO '.$table.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$tmpname; 390fd69a32cSAndreas Gohr 391fd69a32cSAndreas Gohr $res = $this->query($createtemptableSQL); //create temp table 392fd69a32cSAndreas Gohr if($res === false) return false; 393fd69a32cSAndreas Gohr $res = $this->query($copytotempsql); //copy to table 394fd69a32cSAndreas Gohr if($res === false) return false; 395fd69a32cSAndreas Gohr $res = $this->query($dropoldsql); //drop old table 396fd69a32cSAndreas Gohr if($res === false) return false; 397fd69a32cSAndreas Gohr 398fd69a32cSAndreas Gohr $res = $this->query($createnewtableSQL); //recreate original table 399fd69a32cSAndreas Gohr if($res === false) return false; 400fd69a32cSAndreas Gohr $res = $this->query($copytonewsql); //copy back to original table 401fd69a32cSAndreas Gohr if($res === false) return false; 402fb394683SAndreas Gohr 403fb394683SAndreas Gohr foreach($indexes as $index){ // readd indexes 404fb394683SAndreas Gohr $res = $this->query($index['sql']); 405fb394683SAndreas Gohr if($res === false) return false; 406fb394683SAndreas Gohr } 407fb394683SAndreas Gohr 408fd69a32cSAndreas Gohr $res = $this->query($droptempsql); //drop temp table 409fd69a32cSAndreas Gohr if($res === false) return false; 410fd69a32cSAndreas Gohr 411fd69a32cSAndreas Gohr return $res; // return a valid resource 412fd69a32cSAndreas Gohr } 413fd69a32cSAndreas Gohr 414fd69a32cSAndreas Gohr /** 415a1e6784eSAndreas Gohr * Execute a query with the given parameters. 416a1e6784eSAndreas Gohr * 417a1e6784eSAndreas Gohr * Takes care of escaping 418a1e6784eSAndreas Gohr * 419a1e6784eSAndreas Gohr * @param string $sql - the statement 420a1e6784eSAndreas Gohr * @param arguments... 421a1e6784eSAndreas Gohr */ 422a1e6784eSAndreas Gohr function query(){ 423a1e6784eSAndreas Gohr if(!$this->db) return false; 424a1e6784eSAndreas Gohr 425a1e6784eSAndreas Gohr // get function arguments 426a1e6784eSAndreas Gohr $args = func_get_args(); 427a1e6784eSAndreas Gohr $sql = trim(array_shift($args)); 428fd69a32cSAndreas Gohr $sql = rtrim($sql,';'); 429a1e6784eSAndreas Gohr 430a1e6784eSAndreas Gohr if(!$sql){ 431a1e6784eSAndreas Gohr msg('No SQL statement given',-1); 432a1e6784eSAndreas Gohr return false; 433a1e6784eSAndreas Gohr } 434a1e6784eSAndreas Gohr 435a1e6784eSAndreas Gohr $argc = count($args); 43612f8c9c7SAdrian Lang if($argc > 0 && is_array($args[0])) { 43712f8c9c7SAdrian Lang $args = $args[0]; 43812f8c9c7SAdrian Lang $argc = count($args); 43912f8c9c7SAdrian Lang } 440a1e6784eSAndreas Gohr 441a1e6784eSAndreas Gohr // check number of arguments 442a1e6784eSAndreas Gohr if($argc < substr_count($sql,'?')){ 443a1e6784eSAndreas Gohr msg('Not enough arguments passed for statement. '. 444a1e6784eSAndreas Gohr 'Expected '.substr_count($sql,'?').' got '. 445a1e6784eSAndreas Gohr $argc.' - '.hsc($sql),-1); 446a1e6784eSAndreas Gohr return false; 447a1e6784eSAndreas Gohr } 448a1e6784eSAndreas Gohr 449a1e6784eSAndreas Gohr // explode at wildcard, then join again 450a1e6784eSAndreas Gohr $parts = explode('?',$sql,$argc+1); 451a1e6784eSAndreas Gohr $args = array_map(array($this,'quote_string'),$args); 452a1e6784eSAndreas Gohr $sql = ''; 453a1e6784eSAndreas Gohr 454a1e6784eSAndreas Gohr while( ($part = array_shift($parts)) !== null ){ 455a1e6784eSAndreas Gohr $sql .= $part; 456a1e6784eSAndreas Gohr $sql .= array_shift($args); 457a1e6784eSAndreas Gohr } 458a1e6784eSAndreas Gohr 459fd69a32cSAndreas Gohr // intercept ALTER TABLE statements 46087fa2c18Sstretchyboy $match = null; 461fd69a32cSAndreas Gohr if(preg_match('/^ALTER\s+TABLE\s+([\w\.]+)\s+(.*)/i',$sql,$match)){ 462fd69a32cSAndreas Gohr return $this->_altertable($match[1],$match[2]); 463fd69a32cSAndreas Gohr } 464fd69a32cSAndreas Gohr 465a1e6784eSAndreas Gohr // execute query 466a1e6784eSAndreas Gohr $err = ''; 46787fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 46887fa2c18Sstretchyboy { 469a1e6784eSAndreas Gohr $res = @sqlite_query($this->db,$sql,SQLITE_ASSOC,$err); 470a1e6784eSAndreas Gohr if($err){ 471d9cff31cSAndreas Gohr msg($err.':<br /><pre>'.hsc($sql).'</pre>',-1); 472a1e6784eSAndreas Gohr return false; 473a1e6784eSAndreas Gohr }elseif(!$res){ 474a1e6784eSAndreas Gohr msg(sqlite_error_string(sqlite_last_error($this->db)). 475d9cff31cSAndreas Gohr ':<br /><pre>'.hsc($sql).'</pre>',-1); 476a1e6784eSAndreas Gohr return false; 477a1e6784eSAndreas Gohr } 47887fa2c18Sstretchyboy } 47987fa2c18Sstretchyboy else 48087fa2c18Sstretchyboy { 48187fa2c18Sstretchyboy $result = false; 482a1e6784eSAndreas Gohr 48387fa2c18Sstretchyboy $res = $this->db->query($sql); 48487fa2c18Sstretchyboy 48587fa2c18Sstretchyboy if(!$res){ 48687fa2c18Sstretchyboy $err = $this->db->errorInfo(); 48787fa2c18Sstretchyboy msg($err[2].':<br /><pre>'.hsc($sql).'</pre>',-1); 48887fa2c18Sstretchyboy return false; 48987fa2c18Sstretchyboy } 49087fa2c18Sstretchyboy } 491a1e6784eSAndreas Gohr return $res; 492a1e6784eSAndreas Gohr } 493a1e6784eSAndreas Gohr 494a1e6784eSAndreas Gohr /** 495a1e6784eSAndreas Gohr * Returns a complete result set as array 496a1e6784eSAndreas Gohr */ 497a1e6784eSAndreas Gohr function res2arr($res){ 498a1e6784eSAndreas Gohr $data = array(); 49987fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 50087fa2c18Sstretchyboy { 501a1e6784eSAndreas Gohr if(!sqlite_num_rows($res)) return $data; 502a1e6784eSAndreas Gohr sqlite_rewind($res); 503a1e6784eSAndreas Gohr while(($row = sqlite_fetch_array($res)) !== false){ 504a1e6784eSAndreas Gohr $data[] = $row; 505a1e6784eSAndreas Gohr } 50687fa2c18Sstretchyboy } 50787fa2c18Sstretchyboy else 50887fa2c18Sstretchyboy { 50987fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_ASSOC); 51087fa2c18Sstretchyboy if(!count(data)) 51187fa2c18Sstretchyboy { 51287fa2c18Sstretchyboy return false; 51387fa2c18Sstretchyboy } 51487fa2c18Sstretchyboy } 515a1e6784eSAndreas Gohr return $data; 516a1e6784eSAndreas Gohr } 517a1e6784eSAndreas Gohr 518a1e6784eSAndreas Gohr /** 519a1e6784eSAndreas Gohr * Return the wanted row from a given result set as 520a1e6784eSAndreas Gohr * associative array 521a1e6784eSAndreas Gohr */ 522a1e6784eSAndreas Gohr function res2row($res,$rownum=0){ 52387fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 52487fa2c18Sstretchyboy { 525a1e6784eSAndreas Gohr if(!@sqlite_seek($res,$rownum)){ 526a1e6784eSAndreas Gohr return false; 527a1e6784eSAndreas Gohr } 528a1e6784eSAndreas Gohr return sqlite_fetch_array($res); 529a1e6784eSAndreas Gohr } 53087fa2c18Sstretchyboy else 53187fa2c18Sstretchyboy { 53287fa2c18Sstretchyboy //very dirty replication of the same functionality (really must look at cursors) 53387fa2c18Sstretchyboy $data = array(); 53487fa2c18Sstretchyboy //do we need to rewind? 53587fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_ASSOC); 53687fa2c18Sstretchyboy if(!count(data)) 53787fa2c18Sstretchyboy { 53887fa2c18Sstretchyboy return false; 53987fa2c18Sstretchyboy } 54087fa2c18Sstretchyboy 54187fa2c18Sstretchyboy if(!isset($data[$rownum])) 54287fa2c18Sstretchyboy { 54387fa2c18Sstretchyboy return false; 54487fa2c18Sstretchyboy } 54587fa2c18Sstretchyboy else 54687fa2c18Sstretchyboy { 54787fa2c18Sstretchyboy return $data[$rownum]; 54887fa2c18Sstretchyboy } 54987fa2c18Sstretchyboy } 55087fa2c18Sstretchyboy } 551a1e6784eSAndreas Gohr 552fee3b689Sstretchyboy /** 553fee3b689Sstretchyboy * Return the first value from the first row. 554fee3b689Sstretchyboy */ 555fee3b689Sstretchyboy function res2single($res) 556fee3b689Sstretchyboy { 55787fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 55887fa2c18Sstretchyboy { 559fee3b689Sstretchyboy return sqlite_fetch_single($res); 560fee3b689Sstretchyboy } 56187fa2c18Sstretchyboy else 56287fa2c18Sstretchyboy { 56387fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_NUM); 56487fa2c18Sstretchyboy if(!count(data)) 56587fa2c18Sstretchyboy { 56687fa2c18Sstretchyboy return false; 56787fa2c18Sstretchyboy } 56887fa2c18Sstretchyboy return $data[0][0]; 56987fa2c18Sstretchyboy } 57087fa2c18Sstretchyboy } 571a1e6784eSAndreas Gohr 572a1e6784eSAndreas Gohr /** 573a1e6784eSAndreas Gohr * Join the given values and quote them for SQL insertion 574a1e6784eSAndreas Gohr */ 575a1e6784eSAndreas Gohr function quote_and_join($vals,$sep=',') { 576a1e6784eSAndreas Gohr $vals = array_map(array('helper_plugin_sqlite','quote_string'),$vals); 577a1e6784eSAndreas Gohr return join($sep,$vals); 578a1e6784eSAndreas Gohr } 579a1e6784eSAndreas Gohr 580a1e6784eSAndreas Gohr /** 581a1e6784eSAndreas Gohr * Run sqlite_escape_string() on the given string and surround it 582a1e6784eSAndreas Gohr * with quotes 583a1e6784eSAndreas Gohr */ 584a1e6784eSAndreas Gohr function quote_string($string){ 58587fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 58687fa2c18Sstretchyboy { 587a1e6784eSAndreas Gohr return "'".sqlite_escape_string($string)."'"; 588a1e6784eSAndreas Gohr } 58987fa2c18Sstretchyboy else 59087fa2c18Sstretchyboy { 59187fa2c18Sstretchyboy return $this->db->quote($string); 59287fa2c18Sstretchyboy } 59387fa2c18Sstretchyboy } 594a1e6784eSAndreas Gohr 595a1e6784eSAndreas Gohr 596b5b947d7SAndreas Gohr /** 597fee3b689Sstretchyboy * Escape string for sql 598fee3b689Sstretchyboy */ 599fee3b689Sstretchyboy function escape_string($str) 600fee3b689Sstretchyboy { 60187fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 60287fa2c18Sstretchyboy { 603fee3b689Sstretchyboy return sqlite_escape_string($str); 604fee3b689Sstretchyboy } 60587fa2c18Sstretchyboy else 60687fa2c18Sstretchyboy { 60787fa2c18Sstretchyboy return trim($this->db->quote($str), "'"); 60887fa2c18Sstretchyboy } 60987fa2c18Sstretchyboy } 610fee3b689Sstretchyboy 611fee3b689Sstretchyboy 612ff97cc8fSstretchyboy 613fee3b689Sstretchyboy /** 614b5b947d7SAndreas Gohr * Aggregation function for SQLite 615b5b947d7SAndreas Gohr * 616b5b947d7SAndreas Gohr * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 617b5b947d7SAndreas Gohr */ 618ff97cc8fSstretchyboy function _sqlite_group_concat_step(&$context, $string, $separator = ',') { 619ff97cc8fSstretchyboy $context['sep'] = $separator; 620ff97cc8fSstretchyboy $context['data'][] = $string; 621ff97cc8fSstretchyboy } 622ff97cc8fSstretchyboy 623ff97cc8fSstretchyboy /** 624ff97cc8fSstretchyboy * Aggregation function for SQLite 625ff97cc8fSstretchyboy * 626ff97cc8fSstretchyboy * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 627ff97cc8fSstretchyboy */ 628ff97cc8fSstretchyboy function _sqlite_group_concat_finalize(&$context) { 629ff97cc8fSstretchyboy $context['data'] = array_unique($context['data']); 630ff97cc8fSstretchyboy return join($context['sep'],$context['data']); 631ff97cc8fSstretchyboy } 632ff97cc8fSstretchyboy 633ff97cc8fSstretchyboy 634ff97cc8fSstretchyboy /** 635ff97cc8fSstretchyboy * Aggregation function for SQLite via PDO 636ff97cc8fSstretchyboy * 637ff97cc8fSstretchyboy * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 638ff97cc8fSstretchyboy */ 639ff97cc8fSstretchyboy function _pdo_group_concat_step(&$context, $rownumber, $string, $separator = ',') { 64087fa2c18Sstretchyboy if(is_null($context)) 64187fa2c18Sstretchyboy { 64287fa2c18Sstretchyboy $context = array( 64387fa2c18Sstretchyboy 'sep' => $separator, 64487fa2c18Sstretchyboy 'data' => array() 64587fa2c18Sstretchyboy ); 64687fa2c18Sstretchyboy } 64787fa2c18Sstretchyboy 648b5b947d7SAndreas Gohr $context['data'][] = $string; 64987fa2c18Sstretchyboy return $context; 650b5b947d7SAndreas Gohr } 651b5b947d7SAndreas Gohr 652b5b947d7SAndreas Gohr /** 653ff97cc8fSstretchyboy * Aggregation function for SQLite via PDO 654b5b947d7SAndreas Gohr * 655b5b947d7SAndreas Gohr * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 656b5b947d7SAndreas Gohr */ 657ff97cc8fSstretchyboy function _pdo_group_concat_finalize(&$context, $rownumber) 65887fa2c18Sstretchyboy { 65987fa2c18Sstretchyboy if(!is_array($context)) 66087fa2c18Sstretchyboy { 66187fa2c18Sstretchyboy return null; 66287fa2c18Sstretchyboy } 663b5b947d7SAndreas Gohr $context['data'] = array_unique($context['data']); 664b5b947d7SAndreas Gohr return join($context['sep'],$context['data']); 665b5b947d7SAndreas Gohr } 666b5b947d7SAndreas Gohr 667e7112ccbSAdrian Lang /** 668e7112ccbSAdrian Lang * Keep separate instances for every call to keep database connections 669e7112ccbSAdrian Lang */ 670e7112ccbSAdrian Lang function isSingleton() { 671e7112ccbSAdrian Lang return false; 672e7112ccbSAdrian Lang } 673fee3b689Sstretchyboy 674fee3b689Sstretchyboy /** 675fee3b689Sstretchyboy * fetch the next row as zero indexed array 676fee3b689Sstretchyboy */ 677fee3b689Sstretchyboy function res_fetch_array($res) 678fee3b689Sstretchyboy { 67987fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 68087fa2c18Sstretchyboy { 681fee3b689Sstretchyboy return sqlite_fetch_array($res, SQLITE_NUM); 682fee3b689Sstretchyboy } 68387fa2c18Sstretchyboy else 68487fa2c18Sstretchyboy { 68587fa2c18Sstretchyboy return $res->fetch(PDO::FETCH_NUM); 68687fa2c18Sstretchyboy } 68787fa2c18Sstretchyboy } 688fee3b689Sstretchyboy 689fee3b689Sstretchyboy 690fee3b689Sstretchyboy /** 691fee3b689Sstretchyboy * fetch the next row as assocative array 692fee3b689Sstretchyboy */ 693fee3b689Sstretchyboy function res_fetch_assoc($res) 694fee3b689Sstretchyboy { 69587fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 69687fa2c18Sstretchyboy { 697fee3b689Sstretchyboy return sqlite_fetch_array($res, SQLITE_ASSOC); 698fee3b689Sstretchyboy } 69987fa2c18Sstretchyboy else 70087fa2c18Sstretchyboy { 70187fa2c18Sstretchyboy return $res->fetch(PDO::FETCH_ASSOC); 70287fa2c18Sstretchyboy } 70387fa2c18Sstretchyboy } 704fee3b689Sstretchyboy 705fee3b689Sstretchyboy 706fee3b689Sstretchyboy /** 707fee3b689Sstretchyboy * Count the number of records in rsult 708fee3b689Sstretchyboy */ 709fee3b689Sstretchyboy function res2count($res) { 71087fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 71187fa2c18Sstretchyboy { 712fee3b689Sstretchyboy return sqlite_num_rows($res); 713fee3b689Sstretchyboy } 71487fa2c18Sstretchyboy else 71587fa2c18Sstretchyboy { 71687fa2c18Sstretchyboy $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i'; 71787fa2c18Sstretchyboy if (preg_match($regex, $res->queryString, $output) > 0) { 71887fa2c18Sstretchyboy $stmt = $this->db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM); 71987fa2c18Sstretchyboy 72087fa2c18Sstretchyboy return $stmt->fetchColumn(); 72187fa2c18Sstretchyboy } 72287fa2c18Sstretchyboy 72387fa2c18Sstretchyboy return false; 72487fa2c18Sstretchyboy } 72587fa2c18Sstretchyboy } 72624a03f6cSstretchyboy 72724a03f6cSstretchyboy 72824a03f6cSstretchyboy /** 72924a03f6cSstretchyboy * Count the number of records changed last time 73024a03f6cSstretchyboy */ 73124a03f6cSstretchyboy function countChanges($db, $res) 73224a03f6cSstretchyboy { 73387fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 73487fa2c18Sstretchyboy { 73524a03f6cSstretchyboy return sqlite_changes($db); 73624a03f6cSstretchyboy } 73787fa2c18Sstretchyboy else 73887fa2c18Sstretchyboy { 73987fa2c18Sstretchyboy return $res->rowCount(); 74087fa2c18Sstretchyboy } 74187fa2c18Sstretchyboy } 742a1e6784eSAndreas Gohr} 743a1e6784eSAndreas Gohr 744a1e6784eSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8: 745