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 9387fa2c18Sstretchyboy $fileextension = '.sqlite'; 9487fa2c18Sstretchyboy 95ff97cc8fSstretchyboy $this->dbfile = $conf['metadir'].'/'.$dbname.$fileextension; 96ff97cc8fSstretchyboy 97ff97cc8fSstretchyboy $init = (!@file_exists($this->dbfile) || ((int) @filesize($this->dbfile)) < 3); 98a1e6784eSAndreas Gohr 990098e1a7SKlap-in //first line tell the format of db file http://marc.info/?l=sqlite-users&m=109383875408202 1000098e1a7SKlap-in $firstline=file_get_contents($this->dbfile,false,null,0,15); 1010098e1a7SKlap-in 10287fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 10387fa2c18Sstretchyboy { 1040098e1a7SKlap-in if($firstline=='SQLite format 3'){ 1050098e1a7SKlap-in msg("SQLite: failed to open SQLite '".$this->dbname."' database (DB has a sqlite3 format instead of sqlite2 format.)",-1); 1060098e1a7SKlap-in return false; 1070098e1a7SKlap-in } 1080098e1a7SKlap-in 109a1e6784eSAndreas Gohr $error=''; 110ff97cc8fSstretchyboy $this->db = sqlite_open($this->dbfile, 0666, $error); 111a1e6784eSAndreas Gohr if(!$this->db){ 112be16ec27SKlap-in msg("SQLite: failed to open SQLite '".$this->dbname."' database ($error)",-1); 113a1e6784eSAndreas Gohr return false; 114a1e6784eSAndreas Gohr } 115a1e6784eSAndreas Gohr 116b5b947d7SAndreas Gohr // register our custom aggregate function 117b5b947d7SAndreas Gohr sqlite_create_aggregate($this->db,'group_concat', 118b5b947d7SAndreas Gohr array($this,'_sqlite_group_concat_step'), 119b5b947d7SAndreas Gohr array($this,'_sqlite_group_concat_finalize'), 2); 12087fa2c18Sstretchyboy } 12187fa2c18Sstretchyboy else 12287fa2c18Sstretchyboy { 1230098e1a7SKlap-in if($firstline!='SQLite format 3'){ 1240098e1a7SKlap-in msg("SQLite: failed to open SQLite '".$this->dbname."' database (DB has not a sqlite3 format.)",-1); 1250098e1a7SKlap-in return false; 1260098e1a7SKlap-in } 1270098e1a7SKlap-in 128ff97cc8fSstretchyboy $dsn = 'sqlite:'.$this->dbfile; 12987fa2c18Sstretchyboy 13087fa2c18Sstretchyboy try { 13187fa2c18Sstretchyboy $this->db = new PDO($dsn); 13287fa2c18Sstretchyboy } catch (PDOException $e) { 133be16ec27SKlap-in msg("SQLite: failed to open SQLite '".$this->dbname."' database (".$e->getMessage().")",-1); 13487fa2c18Sstretchyboy return false; 13587fa2c18Sstretchyboy } 13687fa2c18Sstretchyboy $this->db->sqliteCreateAggregate('group_concat', 137ff97cc8fSstretchyboy array($this,'_pdo_group_concat_step'), 138ff97cc8fSstretchyboy array($this,'_pdo_group_concat_finalize')); 13987fa2c18Sstretchyboy } 140b5b947d7SAndreas Gohr 141a1e6784eSAndreas Gohr $this->_updatedb($init,$updatedir); 142a1e6784eSAndreas Gohr return true; 143a1e6784eSAndreas Gohr } 144a1e6784eSAndreas Gohr 145a1e6784eSAndreas Gohr /** 146a1e6784eSAndreas Gohr * Return the current Database Version 147a1e6784eSAndreas Gohr */ 148a1e6784eSAndreas Gohr function _currentDBversion(){ 149a1e6784eSAndreas Gohr $sql = "SELECT val FROM opts WHERE opt = 'dbversion';"; 150a1e6784eSAndreas Gohr $res = $this->query($sql); 151a1e6784eSAndreas Gohr if(!$res) return false; 152a1e6784eSAndreas Gohr $row = $this->res2row($res,0); 153a1e6784eSAndreas Gohr return (int) $row['val']; 154a1e6784eSAndreas Gohr } 155a1e6784eSAndreas Gohr /** 156a1e6784eSAndreas Gohr * Update the database if needed 157a1e6784eSAndreas Gohr * 158a1e6784eSAndreas Gohr * @param bool $init - true if this is a new database to initialize 159a1e6784eSAndreas Gohr * @param string $updatedir - Database update infos 160a1e6784eSAndreas Gohr */ 1614d9093b4Sstretchyboy function _updatedb($init,$updatedir) 1624d9093b4Sstretchyboy { 163a1e6784eSAndreas Gohr if($init){ 1644d9093b4Sstretchyboy 165a1e6784eSAndreas Gohr $current = 0; 166a1e6784eSAndreas Gohr }else{ 167a1e6784eSAndreas Gohr $current = $this->_currentDBversion(); 168a1e6784eSAndreas Gohr if(!$current){ 169be16ec27SKlap-in msg("SQLite: no DB version found. '".$this->dbname."' DB probably broken.",-1); 170a1e6784eSAndreas Gohr return false; 171a1e6784eSAndreas Gohr } 172a1e6784eSAndreas Gohr } 173a1e6784eSAndreas Gohr 174a1e6784eSAndreas Gohr // in case of init, add versioning table 175a1e6784eSAndreas Gohr if($init){ 176a1e6784eSAndreas Gohr if(!$this->_runupdatefile(dirname(__FILE__).'/db.sql',0)){ 177be16ec27SKlap-in msg("SQLite: '".$this->dbname."' database upgrade failed for version ", -1); 178a1e6784eSAndreas Gohr return false; 179a1e6784eSAndreas Gohr } 180a1e6784eSAndreas Gohr } 181a1e6784eSAndreas Gohr 182a1e6784eSAndreas Gohr $latest = (int) trim(io_readFile($updatedir.'/latest.version')); 183a1e6784eSAndreas Gohr 184a1e6784eSAndreas Gohr // all up to date? 185a1e6784eSAndreas Gohr if($current >= $latest) return true; 186a1e6784eSAndreas Gohr for($i=$current+1; $i<=$latest; $i++){ 187a1e6784eSAndreas Gohr $file = sprintf($updatedir.'/update%04d.sql',$i); 188a1e6784eSAndreas Gohr if(file_exists($file)){ 189a1e6784eSAndreas Gohr if(!$this->_runupdatefile($file,$i)){ 190be16ec27SKlap-in msg("SQLite: '".$this->dbname."' database upgrade failed for version ".$i, -1); 191a1e6784eSAndreas Gohr return false; 192a1e6784eSAndreas Gohr } 193a1e6784eSAndreas Gohr } 194a1e6784eSAndreas Gohr } 195a1e6784eSAndreas Gohr return true; 196a1e6784eSAndreas Gohr } 197a1e6784eSAndreas Gohr 198a1e6784eSAndreas Gohr /** 199a1e6784eSAndreas Gohr * Updates the database structure using the given file to 200a1e6784eSAndreas Gohr * the given version. 201a1e6784eSAndreas Gohr */ 202a1e6784eSAndreas Gohr function _runupdatefile($file,$version){ 203a1e6784eSAndreas Gohr $sql = io_readFile($file,false); 204a1e6784eSAndreas Gohr 205a1e6784eSAndreas Gohr $sql = explode(";",$sql); 206a1e6784eSAndreas Gohr array_unshift($sql,'BEGIN TRANSACTION'); 207a1e6784eSAndreas Gohr array_push($sql,"INSERT OR REPLACE INTO opts (val,opt) VALUES ($version,'dbversion')"); 208a1e6784eSAndreas Gohr array_push($sql,"COMMIT TRANSACTION"); 209a1e6784eSAndreas Gohr 210a1e6784eSAndreas Gohr foreach($sql as $s){ 211a1e6784eSAndreas Gohr $s = preg_replace('!^\s*--.*$!m', '', $s); 212a1e6784eSAndreas Gohr $s = trim($s); 213a1e6784eSAndreas Gohr if(!$s) continue; 214fd69a32cSAndreas Gohr 215fd69a32cSAndreas Gohr 216a1e6784eSAndreas Gohr $res = $this->query("$s;"); 217a1e6784eSAndreas Gohr if ($res === false) { 21887fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE) 21987fa2c18Sstretchyboy { 220a1e6784eSAndreas Gohr sqlite_query($this->db, 'ROLLBACK TRANSACTION'); 221a1e6784eSAndreas Gohr return false; 222a1e6784eSAndreas Gohr } 22305f176edSstretchyboy else 22405f176edSstretchyboy { 22505f176edSstretchyboy return false; 22605f176edSstretchyboy } 227a1e6784eSAndreas Gohr } 22887fa2c18Sstretchyboy } 229a1e6784eSAndreas Gohr 230a1e6784eSAndreas Gohr return ($version == $this->_currentDBversion()); 231a1e6784eSAndreas Gohr } 232a1e6784eSAndreas Gohr 233a1e6784eSAndreas Gohr /** 234fd69a32cSAndreas Gohr * Emulate ALTER TABLE 235fd69a32cSAndreas Gohr * 236fd69a32cSAndreas Gohr * The ALTER TABLE syntax is parsed and then emulated using a 237fd69a32cSAndreas Gohr * temporary table 238fd69a32cSAndreas Gohr * 239fd69a32cSAndreas Gohr * @author <jon@jenseng.com> 240fd69a32cSAndreas Gohr * @link http://code.jenseng.com/db/ 241fb394683SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 242fd69a32cSAndreas Gohr */ 243fd69a32cSAndreas Gohr function _altertable($table,$alterdefs){ 244fb394683SAndreas Gohr 245fb394683SAndreas Gohr // load original table definition SQL 246fd69a32cSAndreas Gohr $result = $this->query("SELECT sql,name,type 247fd69a32cSAndreas Gohr FROM sqlite_master 248fd69a32cSAndreas Gohr WHERE tbl_name = '$table' 249fb394683SAndreas Gohr AND type = 'table'"); 250fb394683SAndreas Gohr 25187fa2c18Sstretchyboy if(($result === false) || ($this->extension == DOKU_EXT_SQLITE && sqlite_num_rows($result)<=0)){ 252fd69a32cSAndreas Gohr msg("ALTER TABLE failed, no such table '".hsc($table)."'",-1); 253fd69a32cSAndreas Gohr return false; 254fd69a32cSAndreas Gohr } 25587fa2c18Sstretchyboy 25687fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 25787fa2c18Sstretchyboy { 258fb394683SAndreas Gohr $row = sqlite_fetch_array($result); 25987fa2c18Sstretchyboy } 26087fa2c18Sstretchyboy else 26187fa2c18Sstretchyboy { 26287fa2c18Sstretchyboy $row = $result->fetch(PDO::FETCH_ASSOC); 26387fa2c18Sstretchyboy } 26487fa2c18Sstretchyboy 26587fa2c18Sstretchyboy if($row === false){ 26687fa2c18Sstretchyboy msg("ALTER TABLE failed, table '".hsc($table)."' had no master data",-1); 26787fa2c18Sstretchyboy return false; 26887fa2c18Sstretchyboy } 26987fa2c18Sstretchyboy 270fd69a32cSAndreas Gohr 271fb394683SAndreas Gohr // prepare temporary table SQL 272fd69a32cSAndreas Gohr $tmpname = 't'.time(); 273fd69a32cSAndreas Gohr $origsql = trim(preg_replace("/[\s]+/"," ", 274fd69a32cSAndreas Gohr str_replace(",",", ", 275fd69a32cSAndreas Gohr preg_replace('/\)$/',' )', 276fd69a32cSAndreas Gohr preg_replace("/[\(]/","( ",$row['sql'],1))))); 277fd69a32cSAndreas Gohr $createtemptableSQL = 'CREATE TEMPORARY '.substr(trim(preg_replace("'".$table."'",$tmpname,$origsql,1)),6); 278fd69a32cSAndreas Gohr $createindexsql = array(); 279fb394683SAndreas Gohr 280fb394683SAndreas Gohr // load indexes to reapply later 281fb394683SAndreas Gohr $result = $this->query("SELECT sql,name,type 282fb394683SAndreas Gohr FROM sqlite_master 283fb394683SAndreas Gohr WHERE tbl_name = '$table' 284fb394683SAndreas Gohr AND type = 'index'"); 285fb394683SAndreas Gohr if(!$result){ 286fb394683SAndreas Gohr $indexes = array(); 287fb394683SAndreas Gohr }else{ 288fb394683SAndreas Gohr $indexes = $this->res2arr($result); 289fb394683SAndreas Gohr } 290fb394683SAndreas Gohr 291fb394683SAndreas Gohr 292fd69a32cSAndreas Gohr $i = 0; 293fd69a32cSAndreas Gohr $defs = preg_split("/[,]+/",$alterdefs,-1,PREG_SPLIT_NO_EMPTY); 294fd69a32cSAndreas Gohr $prevword = $table; 295fd69a32cSAndreas Gohr $oldcols = preg_split("/[,]+/",substr(trim($createtemptableSQL),strpos(trim($createtemptableSQL),'(')+1),-1,PREG_SPLIT_NO_EMPTY); 296fd69a32cSAndreas Gohr $newcols = array(); 297fd69a32cSAndreas Gohr 298665af209SAdrian Lang for($i=0;$i<count($oldcols);$i++){ 299fd69a32cSAndreas Gohr $colparts = preg_split("/[\s]+/",$oldcols[$i],-1,PREG_SPLIT_NO_EMPTY); 300fd69a32cSAndreas Gohr $oldcols[$i] = $colparts[0]; 301fd69a32cSAndreas Gohr $newcols[$colparts[0]] = $colparts[0]; 302fd69a32cSAndreas Gohr } 303fd69a32cSAndreas Gohr $newcolumns = ''; 304fd69a32cSAndreas Gohr $oldcolumns = ''; 305fd69a32cSAndreas Gohr reset($newcols); 306fd69a32cSAndreas Gohr while(list($key,$val) = each($newcols)){ 307fd69a32cSAndreas Gohr $newcolumns .= ($newcolumns?', ':'').$val; 308fd69a32cSAndreas Gohr $oldcolumns .= ($oldcolumns?', ':'').$key; 309fd69a32cSAndreas Gohr } 310fd69a32cSAndreas Gohr $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table; 311fd69a32cSAndreas Gohr $dropoldsql = 'DROP TABLE '.$table; 312fd69a32cSAndreas Gohr $createtesttableSQL = $createtemptableSQL; 313fd69a32cSAndreas Gohr 314fd69a32cSAndreas Gohr foreach($defs as $def){ 315fd69a32cSAndreas Gohr $defparts = preg_split("/[\s]+/",$def,-1,PREG_SPLIT_NO_EMPTY); 316fd69a32cSAndreas Gohr $action = strtolower($defparts[0]); 317fd69a32cSAndreas Gohr switch($action){ 318fd69a32cSAndreas Gohr case 'add': 319665af209SAdrian Lang if(count($defparts) < 2){ 320fd69a32cSAndreas Gohr msg('ALTER TABLE: not enough arguments for ADD statement',-1); 321fd69a32cSAndreas Gohr return false; 322fd69a32cSAndreas Gohr } 323fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,strlen($createtesttableSQL)-1).','; 324665af209SAdrian Lang for($i=1;$i<count($defparts);$i++) 325fd69a32cSAndreas Gohr $createtesttableSQL.=' '.$defparts[$i]; 326fd69a32cSAndreas Gohr $createtesttableSQL.=')'; 327fd69a32cSAndreas Gohr break; 328fd69a32cSAndreas Gohr 329fd69a32cSAndreas Gohr case 'change': 330665af209SAdrian Lang if(count($defparts) <= 3){ 331fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').($defparts[2]?' '.$defparts[2]:'').'": syntax error',-1); 332fd69a32cSAndreas Gohr return false; 333fd69a32cSAndreas Gohr } 334fd69a32cSAndreas Gohr 335fd69a32cSAndreas Gohr if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){ 336fd69a32cSAndreas Gohr if($newcols[$defparts[1]] != $defparts[1]){ 337fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 338fd69a32cSAndreas Gohr return false; 339fd69a32cSAndreas Gohr } 340fd69a32cSAndreas Gohr $newcols[$defparts[1]] = $defparts[2]; 341fd69a32cSAndreas Gohr $nextcommapos = strpos($createtesttableSQL,',',$severpos); 342fd69a32cSAndreas Gohr $insertval = ''; 343665af209SAdrian Lang for($i=2;$i<count($defparts);$i++) 344fd69a32cSAndreas Gohr $insertval.=' '.$defparts[$i]; 345fd69a32cSAndreas Gohr if($nextcommapos) 346fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos).$insertval.substr($createtesttableSQL,$nextcommapos); 347fd69a32cSAndreas Gohr else 348fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1)).$insertval.')'; 349fd69a32cSAndreas Gohr } else { 350fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 351fd69a32cSAndreas Gohr return false; 352fd69a32cSAndreas Gohr } 353fd69a32cSAndreas Gohr break; 354fd69a32cSAndreas Gohr case 'drop': 355665af209SAdrian Lang if(count($defparts) < 2){ 356fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').'": syntax error',-1); 357fd69a32cSAndreas Gohr return false; 358fd69a32cSAndreas Gohr } 359fd69a32cSAndreas Gohr if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){ 360fd69a32cSAndreas Gohr $nextcommapos = strpos($createtesttableSQL,',',$severpos); 361fd69a32cSAndreas Gohr if($nextcommapos) 362fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos).substr($createtesttableSQL,$nextcommapos + 1); 363fd69a32cSAndreas Gohr else 364fd69a32cSAndreas Gohr $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1) - 1).')'; 365fd69a32cSAndreas Gohr unset($newcols[$defparts[1]]); 366fd69a32cSAndreas Gohr }else{ 367fd69a32cSAndreas Gohr msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1); 368fd69a32cSAndreas Gohr return false; 369fd69a32cSAndreas Gohr } 370fd69a32cSAndreas Gohr break; 371fd69a32cSAndreas Gohr default: 372fd69a32cSAndreas Gohr msg('ALTER TABLE: near "'.$prevword.'": syntax error',-1); 373fd69a32cSAndreas Gohr return false; 374fd69a32cSAndreas Gohr } 375665af209SAdrian Lang $prevword = $defparts[count($defparts)-1]; 376fd69a32cSAndreas Gohr } 377fd69a32cSAndreas Gohr 378fd69a32cSAndreas Gohr // this block of code generates a test table simply to verify that the 379fd69a32cSAndreas Gohr // columns specifed are valid in an sql statement 380fd69a32cSAndreas Gohr // this ensures that no reserved words are used as columns, for example 381fd69a32cSAndreas Gohr $res = $this->query($createtesttableSQL); 382fd69a32cSAndreas Gohr if($res === false) return false; 383fd69a32cSAndreas Gohr 384fd69a32cSAndreas Gohr $droptempsql = 'DROP TABLE '.$tmpname; 385fd69a32cSAndreas Gohr $res = $this->query($droptempsql); 386fd69a32cSAndreas Gohr if($res === false) return false; 387fd69a32cSAndreas Gohr 388fd69a32cSAndreas Gohr 389fd69a32cSAndreas Gohr $createnewtableSQL = 'CREATE '.substr(trim(preg_replace("'".$tmpname."'",$table,$createtesttableSQL,1)),17); 390fd69a32cSAndreas Gohr $newcolumns = ''; 391fd69a32cSAndreas Gohr $oldcolumns = ''; 392fd69a32cSAndreas Gohr reset($newcols); 393fd69a32cSAndreas Gohr while(list($key,$val) = each($newcols)){ 394fd69a32cSAndreas Gohr $newcolumns .= ($newcolumns?', ':'').$val; 395fd69a32cSAndreas Gohr $oldcolumns .= ($oldcolumns?', ':'').$key; 396fd69a32cSAndreas Gohr } 397fd69a32cSAndreas Gohr 398fd69a32cSAndreas Gohr $copytonewsql = 'INSERT INTO '.$table.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$tmpname; 399fd69a32cSAndreas Gohr 400fd69a32cSAndreas Gohr $res = $this->query($createtemptableSQL); //create temp table 401fd69a32cSAndreas Gohr if($res === false) return false; 402fd69a32cSAndreas Gohr $res = $this->query($copytotempsql); //copy to table 403fd69a32cSAndreas Gohr if($res === false) return false; 404fd69a32cSAndreas Gohr $res = $this->query($dropoldsql); //drop old table 405fd69a32cSAndreas Gohr if($res === false) return false; 406fd69a32cSAndreas Gohr 407fd69a32cSAndreas Gohr $res = $this->query($createnewtableSQL); //recreate original table 408fd69a32cSAndreas Gohr if($res === false) return false; 409fd69a32cSAndreas Gohr $res = $this->query($copytonewsql); //copy back to original table 410fd69a32cSAndreas Gohr if($res === false) return false; 411fb394683SAndreas Gohr 412fb394683SAndreas Gohr foreach($indexes as $index){ // readd indexes 413fb394683SAndreas Gohr $res = $this->query($index['sql']); 414fb394683SAndreas Gohr if($res === false) return false; 415fb394683SAndreas Gohr } 416fb394683SAndreas Gohr 417fd69a32cSAndreas Gohr $res = $this->query($droptempsql); //drop temp table 418fd69a32cSAndreas Gohr if($res === false) return false; 419fd69a32cSAndreas Gohr 420fd69a32cSAndreas Gohr return $res; // return a valid resource 421fd69a32cSAndreas Gohr } 422fd69a32cSAndreas Gohr 423fd69a32cSAndreas Gohr /** 4243ae3f79eSKlap-in * Registers a User Defined Function for use in SQL statements 4253ae3f79eSKlap-in */ 4263ae3f79eSKlap-in function create_function($function_name,$callback,$num_args){ 4273ae3f79eSKlap-in if($this->extension == DOKU_EXT_SQLITE ) 4283ae3f79eSKlap-in { 4293ae3f79eSKlap-in sqlite_create_function($this->db,$function_name,$callback,$num_args); 4303ae3f79eSKlap-in } 4313ae3f79eSKlap-in else 4323ae3f79eSKlap-in { 433*edc99285SKlap-in $this->db->sqliteCreateFunction($function_name,$callback,$num_args); 4343ae3f79eSKlap-in } 4353ae3f79eSKlap-in } 4363ae3f79eSKlap-in 4373ae3f79eSKlap-in /** 438a1e6784eSAndreas Gohr * Execute a query with the given parameters. 439a1e6784eSAndreas Gohr * 440a1e6784eSAndreas Gohr * Takes care of escaping 441a1e6784eSAndreas Gohr * 442a1e6784eSAndreas Gohr * @param string $sql - the statement 443a1e6784eSAndreas Gohr * @param arguments... 444a1e6784eSAndreas Gohr */ 445a1e6784eSAndreas Gohr function query(){ 446a1e6784eSAndreas Gohr if(!$this->db) return false; 447a1e6784eSAndreas Gohr 448a1e6784eSAndreas Gohr // get function arguments 449a1e6784eSAndreas Gohr $args = func_get_args(); 450a1e6784eSAndreas Gohr $sql = trim(array_shift($args)); 451fd69a32cSAndreas Gohr $sql = rtrim($sql,';'); 452a1e6784eSAndreas Gohr 453a1e6784eSAndreas Gohr if(!$sql){ 454a1e6784eSAndreas Gohr msg('No SQL statement given',-1); 455a1e6784eSAndreas Gohr return false; 456a1e6784eSAndreas Gohr } 457a1e6784eSAndreas Gohr 458a1e6784eSAndreas Gohr $argc = count($args); 45912f8c9c7SAdrian Lang if($argc > 0 && is_array($args[0])) { 46012f8c9c7SAdrian Lang $args = $args[0]; 46112f8c9c7SAdrian Lang $argc = count($args); 46212f8c9c7SAdrian Lang } 463a1e6784eSAndreas Gohr 464a1e6784eSAndreas Gohr // check number of arguments 465a1e6784eSAndreas Gohr if($argc < substr_count($sql,'?')){ 466a1e6784eSAndreas Gohr msg('Not enough arguments passed for statement. '. 467a1e6784eSAndreas Gohr 'Expected '.substr_count($sql,'?').' got '. 468a1e6784eSAndreas Gohr $argc.' - '.hsc($sql),-1); 469a1e6784eSAndreas Gohr return false; 470a1e6784eSAndreas Gohr } 471a1e6784eSAndreas Gohr 472a1e6784eSAndreas Gohr // explode at wildcard, then join again 473a1e6784eSAndreas Gohr $parts = explode('?',$sql,$argc+1); 474a1e6784eSAndreas Gohr $args = array_map(array($this,'quote_string'),$args); 475a1e6784eSAndreas Gohr $sql = ''; 476a1e6784eSAndreas Gohr 477a1e6784eSAndreas Gohr while( ($part = array_shift($parts)) !== null ){ 478a1e6784eSAndreas Gohr $sql .= $part; 479a1e6784eSAndreas Gohr $sql .= array_shift($args); 480a1e6784eSAndreas Gohr } 481a1e6784eSAndreas Gohr 482fd69a32cSAndreas Gohr // intercept ALTER TABLE statements 48387fa2c18Sstretchyboy $match = null; 484fd69a32cSAndreas Gohr if(preg_match('/^ALTER\s+TABLE\s+([\w\.]+)\s+(.*)/i',$sql,$match)){ 485fd69a32cSAndreas Gohr return $this->_altertable($match[1],$match[2]); 486fd69a32cSAndreas Gohr } 487fd69a32cSAndreas Gohr 488a1e6784eSAndreas Gohr // execute query 489a1e6784eSAndreas Gohr $err = ''; 49087fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 49187fa2c18Sstretchyboy { 492a1e6784eSAndreas Gohr $res = @sqlite_query($this->db,$sql,SQLITE_ASSOC,$err); 493a1e6784eSAndreas Gohr if($err){ 494d9cff31cSAndreas Gohr msg($err.':<br /><pre>'.hsc($sql).'</pre>',-1); 495a1e6784eSAndreas Gohr return false; 496a1e6784eSAndreas Gohr }elseif(!$res){ 497a1e6784eSAndreas Gohr msg(sqlite_error_string(sqlite_last_error($this->db)). 498d9cff31cSAndreas Gohr ':<br /><pre>'.hsc($sql).'</pre>',-1); 499a1e6784eSAndreas Gohr return false; 500a1e6784eSAndreas Gohr } 50187fa2c18Sstretchyboy } 50287fa2c18Sstretchyboy else 50387fa2c18Sstretchyboy { 50487fa2c18Sstretchyboy $res = $this->db->query($sql); 50587fa2c18Sstretchyboy 50687fa2c18Sstretchyboy if(!$res){ 50787fa2c18Sstretchyboy $err = $this->db->errorInfo(); 50887fa2c18Sstretchyboy msg($err[2].':<br /><pre>'.hsc($sql).'</pre>',-1); 50987fa2c18Sstretchyboy return false; 51087fa2c18Sstretchyboy } 51187fa2c18Sstretchyboy } 512a1e6784eSAndreas Gohr return $res; 513a1e6784eSAndreas Gohr } 514a1e6784eSAndreas Gohr 515a1e6784eSAndreas Gohr /** 516a1e6784eSAndreas Gohr * Returns a complete result set as array 517a1e6784eSAndreas Gohr */ 518a1e6784eSAndreas Gohr function res2arr($res){ 519a1e6784eSAndreas Gohr $data = array(); 52087fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 52187fa2c18Sstretchyboy { 522a1e6784eSAndreas Gohr if(!sqlite_num_rows($res)) return $data; 523a1e6784eSAndreas Gohr sqlite_rewind($res); 524e598d748SAdrian Lang while(($row = sqlite_fetch_array($res, SQLITE_ASSOC)) !== false){ 525a1e6784eSAndreas Gohr $data[] = $row; 526a1e6784eSAndreas Gohr } 52787fa2c18Sstretchyboy } 52887fa2c18Sstretchyboy else 52987fa2c18Sstretchyboy { 53087fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_ASSOC); 53187fa2c18Sstretchyboy if(!count(data)) 53287fa2c18Sstretchyboy { 53387fa2c18Sstretchyboy return false; 53487fa2c18Sstretchyboy } 53587fa2c18Sstretchyboy } 536a1e6784eSAndreas Gohr return $data; 537a1e6784eSAndreas Gohr } 538a1e6784eSAndreas Gohr 539a1e6784eSAndreas Gohr /** 540a1e6784eSAndreas Gohr * Return the wanted row from a given result set as 541a1e6784eSAndreas Gohr * associative array 542a1e6784eSAndreas Gohr */ 543a1e6784eSAndreas Gohr function res2row($res,$rownum=0){ 54487fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 54587fa2c18Sstretchyboy { 546a1e6784eSAndreas Gohr if(!@sqlite_seek($res,$rownum)){ 547a1e6784eSAndreas Gohr return false; 548a1e6784eSAndreas Gohr } 549e598d748SAdrian Lang return sqlite_fetch_array($res, SQLITE_ASSOC); 550a1e6784eSAndreas Gohr } 55187fa2c18Sstretchyboy else 55287fa2c18Sstretchyboy { 55387fa2c18Sstretchyboy //very dirty replication of the same functionality (really must look at cursors) 55487fa2c18Sstretchyboy $data = array(); 55587fa2c18Sstretchyboy //do we need to rewind? 55687fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_ASSOC); 55787fa2c18Sstretchyboy if(!count(data)) 55887fa2c18Sstretchyboy { 55987fa2c18Sstretchyboy return false; 56087fa2c18Sstretchyboy } 56187fa2c18Sstretchyboy 56287fa2c18Sstretchyboy if(!isset($data[$rownum])) 56387fa2c18Sstretchyboy { 56487fa2c18Sstretchyboy return false; 56587fa2c18Sstretchyboy } 56687fa2c18Sstretchyboy else 56787fa2c18Sstretchyboy { 56887fa2c18Sstretchyboy return $data[$rownum]; 56987fa2c18Sstretchyboy } 57087fa2c18Sstretchyboy } 57187fa2c18Sstretchyboy } 572a1e6784eSAndreas Gohr 573fee3b689Sstretchyboy /** 574fee3b689Sstretchyboy * Return the first value from the first row. 575fee3b689Sstretchyboy */ 576fee3b689Sstretchyboy function res2single($res) 577fee3b689Sstretchyboy { 57887fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 57987fa2c18Sstretchyboy { 580fee3b689Sstretchyboy return sqlite_fetch_single($res); 581fee3b689Sstretchyboy } 58287fa2c18Sstretchyboy else 58387fa2c18Sstretchyboy { 58487fa2c18Sstretchyboy $data = $res->fetchAll(PDO::FETCH_NUM); 58587fa2c18Sstretchyboy if(!count(data)) 58687fa2c18Sstretchyboy { 58787fa2c18Sstretchyboy return false; 58887fa2c18Sstretchyboy } 58987fa2c18Sstretchyboy return $data[0][0]; 59087fa2c18Sstretchyboy } 59187fa2c18Sstretchyboy } 592a1e6784eSAndreas Gohr 593a1e6784eSAndreas Gohr /** 594a1e6784eSAndreas Gohr * Join the given values and quote them for SQL insertion 595a1e6784eSAndreas Gohr */ 596a1e6784eSAndreas Gohr function quote_and_join($vals,$sep=',') { 597a1e6784eSAndreas Gohr $vals = array_map(array('helper_plugin_sqlite','quote_string'),$vals); 598a1e6784eSAndreas Gohr return join($sep,$vals); 599a1e6784eSAndreas Gohr } 600a1e6784eSAndreas Gohr 601a1e6784eSAndreas Gohr /** 602a1e6784eSAndreas Gohr * Run sqlite_escape_string() on the given string and surround it 603a1e6784eSAndreas Gohr * with quotes 604a1e6784eSAndreas Gohr */ 605a1e6784eSAndreas Gohr function quote_string($string){ 60687fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 60787fa2c18Sstretchyboy { 608a1e6784eSAndreas Gohr return "'".sqlite_escape_string($string)."'"; 609a1e6784eSAndreas Gohr } 61087fa2c18Sstretchyboy else 61187fa2c18Sstretchyboy { 61287fa2c18Sstretchyboy return $this->db->quote($string); 61387fa2c18Sstretchyboy } 61487fa2c18Sstretchyboy } 615a1e6784eSAndreas Gohr 616a1e6784eSAndreas Gohr 617b5b947d7SAndreas Gohr /** 618fee3b689Sstretchyboy * Escape string for sql 619fee3b689Sstretchyboy */ 620fee3b689Sstretchyboy function escape_string($str) 621fee3b689Sstretchyboy { 62287fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 62387fa2c18Sstretchyboy { 624fee3b689Sstretchyboy return sqlite_escape_string($str); 625fee3b689Sstretchyboy } 62687fa2c18Sstretchyboy else 62787fa2c18Sstretchyboy { 62887fa2c18Sstretchyboy return trim($this->db->quote($str), "'"); 62987fa2c18Sstretchyboy } 63087fa2c18Sstretchyboy } 631fee3b689Sstretchyboy 632fee3b689Sstretchyboy 633ff97cc8fSstretchyboy 634fee3b689Sstretchyboy /** 635b5b947d7SAndreas Gohr * Aggregation function for SQLite 636b5b947d7SAndreas Gohr * 637b5b947d7SAndreas Gohr * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 638b5b947d7SAndreas Gohr */ 639ff97cc8fSstretchyboy function _sqlite_group_concat_step(&$context, $string, $separator = ',') { 640ff97cc8fSstretchyboy $context['sep'] = $separator; 641ff97cc8fSstretchyboy $context['data'][] = $string; 642ff97cc8fSstretchyboy } 643ff97cc8fSstretchyboy 644ff97cc8fSstretchyboy /** 645ff97cc8fSstretchyboy * Aggregation function for SQLite 646ff97cc8fSstretchyboy * 647ff97cc8fSstretchyboy * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 648ff97cc8fSstretchyboy */ 649ff97cc8fSstretchyboy function _sqlite_group_concat_finalize(&$context) { 650ff97cc8fSstretchyboy $context['data'] = array_unique($context['data']); 651ff97cc8fSstretchyboy return join($context['sep'],$context['data']); 652ff97cc8fSstretchyboy } 653ff97cc8fSstretchyboy 654ff97cc8fSstretchyboy 655ff97cc8fSstretchyboy /** 656ff97cc8fSstretchyboy * Aggregation function for SQLite via PDO 657ff97cc8fSstretchyboy * 658ff97cc8fSstretchyboy * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 659ff97cc8fSstretchyboy */ 660ff97cc8fSstretchyboy function _pdo_group_concat_step(&$context, $rownumber, $string, $separator = ',') { 66187fa2c18Sstretchyboy if(is_null($context)) 66287fa2c18Sstretchyboy { 66387fa2c18Sstretchyboy $context = array( 66487fa2c18Sstretchyboy 'sep' => $separator, 66587fa2c18Sstretchyboy 'data' => array() 66687fa2c18Sstretchyboy ); 66787fa2c18Sstretchyboy } 66887fa2c18Sstretchyboy 669b5b947d7SAndreas Gohr $context['data'][] = $string; 67087fa2c18Sstretchyboy return $context; 671b5b947d7SAndreas Gohr } 672b5b947d7SAndreas Gohr 673b5b947d7SAndreas Gohr /** 674ff97cc8fSstretchyboy * Aggregation function for SQLite via PDO 675b5b947d7SAndreas Gohr * 676b5b947d7SAndreas Gohr * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 677b5b947d7SAndreas Gohr */ 678ff97cc8fSstretchyboy function _pdo_group_concat_finalize(&$context, $rownumber) 67987fa2c18Sstretchyboy { 68087fa2c18Sstretchyboy if(!is_array($context)) 68187fa2c18Sstretchyboy { 68287fa2c18Sstretchyboy return null; 68387fa2c18Sstretchyboy } 684b5b947d7SAndreas Gohr $context['data'] = array_unique($context['data']); 685b5b947d7SAndreas Gohr return join($context['sep'],$context['data']); 686b5b947d7SAndreas Gohr } 687b5b947d7SAndreas Gohr 688e7112ccbSAdrian Lang /** 689e7112ccbSAdrian Lang * Keep separate instances for every call to keep database connections 690e7112ccbSAdrian Lang */ 691e7112ccbSAdrian Lang function isSingleton() { 692e7112ccbSAdrian Lang return false; 693e7112ccbSAdrian Lang } 694fee3b689Sstretchyboy 695fee3b689Sstretchyboy /** 696fee3b689Sstretchyboy * fetch the next row as zero indexed array 697fee3b689Sstretchyboy */ 698fee3b689Sstretchyboy function res_fetch_array($res) 699fee3b689Sstretchyboy { 70087fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 70187fa2c18Sstretchyboy { 702fee3b689Sstretchyboy return sqlite_fetch_array($res, SQLITE_NUM); 703fee3b689Sstretchyboy } 70487fa2c18Sstretchyboy else 70587fa2c18Sstretchyboy { 70687fa2c18Sstretchyboy return $res->fetch(PDO::FETCH_NUM); 70787fa2c18Sstretchyboy } 70887fa2c18Sstretchyboy } 709fee3b689Sstretchyboy 710fee3b689Sstretchyboy 711fee3b689Sstretchyboy /** 712fee3b689Sstretchyboy * fetch the next row as assocative array 713fee3b689Sstretchyboy */ 714fee3b689Sstretchyboy function res_fetch_assoc($res) 715fee3b689Sstretchyboy { 71687fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 71787fa2c18Sstretchyboy { 718fee3b689Sstretchyboy return sqlite_fetch_array($res, SQLITE_ASSOC); 719fee3b689Sstretchyboy } 72087fa2c18Sstretchyboy else 72187fa2c18Sstretchyboy { 72287fa2c18Sstretchyboy return $res->fetch(PDO::FETCH_ASSOC); 72387fa2c18Sstretchyboy } 72487fa2c18Sstretchyboy } 725fee3b689Sstretchyboy 726fee3b689Sstretchyboy 727fee3b689Sstretchyboy /** 728fee3b689Sstretchyboy * Count the number of records in rsult 729fee3b689Sstretchyboy */ 730fee3b689Sstretchyboy function res2count($res) { 73187fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 73287fa2c18Sstretchyboy { 733fee3b689Sstretchyboy return sqlite_num_rows($res); 734fee3b689Sstretchyboy } 73587fa2c18Sstretchyboy else 73687fa2c18Sstretchyboy { 73787fa2c18Sstretchyboy $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i'; 73887fa2c18Sstretchyboy if (preg_match($regex, $res->queryString, $output) > 0) { 73987fa2c18Sstretchyboy $stmt = $this->db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM); 74087fa2c18Sstretchyboy 74187fa2c18Sstretchyboy return $stmt->fetchColumn(); 74287fa2c18Sstretchyboy } 74387fa2c18Sstretchyboy 74487fa2c18Sstretchyboy return false; 74587fa2c18Sstretchyboy } 74687fa2c18Sstretchyboy } 74724a03f6cSstretchyboy 74824a03f6cSstretchyboy 74924a03f6cSstretchyboy /** 75024a03f6cSstretchyboy * Count the number of records changed last time 75124a03f6cSstretchyboy */ 75224a03f6cSstretchyboy function countChanges($db, $res) 75324a03f6cSstretchyboy { 75487fa2c18Sstretchyboy if($this->extension == DOKU_EXT_SQLITE ) 75587fa2c18Sstretchyboy { 75624a03f6cSstretchyboy return sqlite_changes($db); 75724a03f6cSstretchyboy } 75887fa2c18Sstretchyboy else 75987fa2c18Sstretchyboy { 76087fa2c18Sstretchyboy return $res->rowCount(); 76187fa2c18Sstretchyboy } 76287fa2c18Sstretchyboy } 763a1e6784eSAndreas Gohr} 764a1e6784eSAndreas Gohr 765a1e6784eSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8: 766