|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved. 00004 Released under both BSD license and Lesser GPL library license. 00005 Whenever there is any discrepancy between the two licenses, 00006 the BSD license will take precedence. 00007 00008 Latest version is available at http://adodb.sourceforge.net 00009 00010 SQLite info: http://www.hwaci.com/sw/sqlite/ 00011 00012 Install Instructions: 00013 ==================== 00014 1. Place this in adodb/drivers 00015 2. Rename the file, remove the .txt prefix. 00016 */ 00017 00018 // security - hide paths 00019 if (!defined('ADODB_DIR')) die(); 00020 00021 class ADODB_sqlite extends ADOConnection { 00022 var $databaseType = "sqlite"; 00023 var $replaceQuote = "''"; // string to use to replace quotes 00024 var $concat_operator='||'; 00025 var $_errorNo = 0; 00026 var $hasLimit = true; 00027 var $hasInsertID = true; 00028 var $hasAffectedRows = true; 00029 var $metaTablesSQL = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"; 00030 var $sysDate = "adodb_date('Y-m-d')"; 00031 var $sysTimeStamp = "adodb_date('Y-m-d H:i:s')"; 00032 var $fmtTimeStamp = "'Y-m-d H:i:s'"; 00033 00034 function ADODB_sqlite() 00035 { 00036 } 00037 00038 /* 00039 function __get($name) 00040 { 00041 switch($name) { 00042 case 'sysDate': return "'".date($this->fmtDate)."'"; 00043 case 'sysTimeStamp' : return "'".date($this->sysTimeStamp)."'"; 00044 } 00045 }*/ 00046 00047 function ServerInfo() 00048 { 00049 $arr['version'] = sqlite_libversion(); 00050 $arr['description'] = 'SQLite '; 00051 $arr['encoding'] = sqlite_libencoding(); 00052 return $arr; 00053 } 00054 00055 function BeginTrans() 00056 { 00057 if ($this->transOff) return true; 00058 $ret = $this->Execute("BEGIN TRANSACTION"); 00059 $this->transCnt += 1; 00060 return true; 00061 } 00062 00063 function CommitTrans($ok=true) 00064 { 00065 if ($this->transOff) return true; 00066 if (!$ok) return $this->RollbackTrans(); 00067 $ret = $this->Execute("COMMIT"); 00068 if ($this->transCnt>0)$this->transCnt -= 1; 00069 return !empty($ret); 00070 } 00071 00072 function RollbackTrans() 00073 { 00074 if ($this->transOff) return true; 00075 $ret = $this->Execute("ROLLBACK"); 00076 if ($this->transCnt>0)$this->transCnt -= 1; 00077 return !empty($ret); 00078 } 00079 00080 // mark newnham 00081 function MetaColumns($table, $normalize=true) 00082 { 00083 global $ADODB_FETCH_MODE; 00084 $false = false; 00085 $save = $ADODB_FETCH_MODE; 00086 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 00087 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false); 00088 $rs = $this->Execute("PRAGMA table_info('$table')"); 00089 if (isset($savem)) $this->SetFetchMode($savem); 00090 if (!$rs) { 00091 $ADODB_FETCH_MODE = $save; 00092 return $false; 00093 } 00094 $arr = array(); 00095 while ($r = $rs->FetchRow()) { 00096 $type = explode('(',$r['type']); 00097 $size = ''; 00098 if (sizeof($type)==2) 00099 $size = trim($type[1],')'); 00100 $fn = strtoupper($r['name']); 00101 $fld = new ADOFieldObject; 00102 $fld->name = $r['name']; 00103 $fld->type = $type[0]; 00104 $fld->max_length = $size; 00105 $fld->not_null = $r['notnull']; 00106 $fld->default_value = $r['dflt_value']; 00107 $fld->scale = 0; 00108 if (isset($r['pk']) && $r['pk']) $fld->primary_key=1; 00109 if ($save == ADODB_FETCH_NUM) $arr[] = $fld; 00110 else $arr[strtoupper($fld->name)] = $fld; 00111 } 00112 $rs->Close(); 00113 $ADODB_FETCH_MODE = $save; 00114 return $arr; 00115 } 00116 00117 function _init($parentDriver) 00118 { 00119 00120 $parentDriver->hasTransactions = false; 00121 $parentDriver->hasInsertID = true; 00122 } 00123 00124 function _insertid() 00125 { 00126 return sqlite_last_insert_rowid($this->_connectionID); 00127 } 00128 00129 function _affectedrows() 00130 { 00131 return sqlite_changes($this->_connectionID); 00132 } 00133 00134 function ErrorMsg() 00135 { 00136 if ($this->_logsql) return $this->_errorMsg; 00137 return ($this->_errorNo) ? sqlite_error_string($this->_errorNo) : ''; 00138 } 00139 00140 function ErrorNo() 00141 { 00142 return $this->_errorNo; 00143 } 00144 00145 function SQLDate($fmt, $col=false) 00146 { 00147 $fmt = $this->qstr($fmt); 00148 return ($col) ? "adodb_date2($fmt,$col)" : "adodb_date($fmt)"; 00149 } 00150 00151 00152 function _createFunctions() 00153 { 00154 @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1); 00155 @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2); 00156 } 00157 00158 00159 // returns true or false 00160 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename) 00161 { 00162 if (!function_exists('sqlite_open')) return null; 00163 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename; 00164 00165 $this->_connectionID = sqlite_open($argHostname); 00166 if ($this->_connectionID === false) return false; 00167 $this->_createFunctions(); 00168 return true; 00169 } 00170 00171 // returns true or false 00172 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) 00173 { 00174 if (!function_exists('sqlite_open')) return null; 00175 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename; 00176 00177 $this->_connectionID = sqlite_popen($argHostname); 00178 if ($this->_connectionID === false) return false; 00179 $this->_createFunctions(); 00180 return true; 00181 } 00182 00183 // returns query ID if successful, otherwise false 00184 function _query($sql,$inputarr=false) 00185 { 00186 $rez = sqlite_query($sql,$this->_connectionID); 00187 if (!$rez) { 00188 $this->_errorNo = sqlite_last_error($this->_connectionID); 00189 } 00190 00191 return $rez; 00192 } 00193 00194 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 00195 { 00196 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : ''; 00197 $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : ''); 00198 if ($secs2cache) 00199 $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr); 00200 else 00201 $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr); 00202 00203 return $rs; 00204 } 00205 00206 /* 00207 This algorithm is not very efficient, but works even if table locking 00208 is not available. 00209 00210 Will return false if unable to generate an ID after $MAXLOOPS attempts. 00211 */ 00212 var $_genSeqSQL = "create table %s (id integer)"; 00213 00214 function GenID($seq='adodbseq',$start=1) 00215 { 00216 // if you have to modify the parameter below, your database is overloaded, 00217 // or you need to implement generation of id's yourself! 00218 $MAXLOOPS = 100; 00219 //$this->debug=1; 00220 while (--$MAXLOOPS>=0) { 00221 @($num = $this->GetOne("select id from $seq")); 00222 if ($num === false) { 00223 $this->Execute(sprintf($this->_genSeqSQL ,$seq)); 00224 $start -= 1; 00225 $num = '0'; 00226 $ok = $this->Execute("insert into $seq values($start)"); 00227 if (!$ok) return false; 00228 } 00229 $this->Execute("update $seq set id=id+1 where id=$num"); 00230 00231 if ($this->affected_rows() > 0) { 00232 $num += 1; 00233 $this->genID = $num; 00234 return $num; 00235 } 00236 } 00237 if ($fn = $this->raiseErrorFn) { 00238 $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num); 00239 } 00240 return false; 00241 } 00242 00243 function CreateSequence($seqname='adodbseq',$start=1) 00244 { 00245 if (empty($this->_genSeqSQL)) return false; 00246 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname)); 00247 if (!$ok) return false; 00248 $start -= 1; 00249 return $this->Execute("insert into $seqname values($start)"); 00250 } 00251 00252 var $_dropSeqSQL = 'drop table %s'; 00253 function DropSequence($seqname) 00254 { 00255 if (empty($this->_dropSeqSQL)) return false; 00256 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname)); 00257 } 00258 00259 // returns true or false 00260 function _close() 00261 { 00262 return @sqlite_close($this->_connectionID); 00263 } 00264 00265 function MetaIndexes($table, $primary = FALSE, $owner=false, $owner = false) 00266 { 00267 $false = false; 00268 // save old fetch mode 00269 global $ADODB_FETCH_MODE; 00270 $save = $ADODB_FETCH_MODE; 00271 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 00272 if ($this->fetchMode !== FALSE) { 00273 $savem = $this->SetFetchMode(FALSE); 00274 } 00275 $SQL=sprintf("SELECT name,sql FROM sqlite_master WHERE type='index' AND tbl_name='%s'", strtolower($table)); 00276 $rs = $this->Execute($SQL); 00277 if (!is_object($rs)) { 00278 if (isset($savem)) 00279 $this->SetFetchMode($savem); 00280 $ADODB_FETCH_MODE = $save; 00281 return $false; 00282 } 00283 00284 $indexes = array (); 00285 while ($row = $rs->FetchRow()) { 00286 if ($primary && preg_match("/primary/i",$row[1]) == 0) continue; 00287 if (!isset($indexes[$row[0]])) { 00288 00289 $indexes[$row[0]] = array( 00290 'unique' => preg_match("/unique/i",$row[1]), 00291 'columns' => array()); 00292 } 00299 $cols = explode("(",$row[1]); 00300 $cols = explode(")",$cols[1]); 00301 array_pop($cols); 00302 $indexes[$row[0]]['columns'] = $cols; 00303 } 00304 if (isset($savem)) { 00305 $this->SetFetchMode($savem); 00306 $ADODB_FETCH_MODE = $save; 00307 } 00308 return $indexes; 00309 } 00310 00311 } 00312 00313 /*-------------------------------------------------------------------------------------- 00314 Class Name: Recordset 00315 --------------------------------------------------------------------------------------*/ 00316 00317 class ADORecordset_sqlite extends ADORecordSet { 00318 00319 var $databaseType = "sqlite"; 00320 var $bind = false; 00321 00322 function ADORecordset_sqlite($queryID,$mode=false) 00323 { 00324 00325 if ($mode === false) { 00326 global $ADODB_FETCH_MODE; 00327 $mode = $ADODB_FETCH_MODE; 00328 } 00329 switch($mode) { 00330 case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break; 00331 case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break; 00332 default: $this->fetchMode = SQLITE_BOTH; break; 00333 } 00334 $this->adodbFetchMode = $mode; 00335 00336 $this->_queryID = $queryID; 00337 00338 $this->_inited = true; 00339 $this->fields = array(); 00340 if ($queryID) { 00341 $this->_currentRow = 0; 00342 $this->EOF = !$this->_fetch(); 00343 @$this->_initrs(); 00344 } else { 00345 $this->_numOfRows = 0; 00346 $this->_numOfFields = 0; 00347 $this->EOF = true; 00348 } 00349 00350 return $this->_queryID; 00351 } 00352 00353 00354 function FetchField($fieldOffset = -1) 00355 { 00356 $fld = new ADOFieldObject; 00357 $fld->name = sqlite_field_name($this->_queryID, $fieldOffset); 00358 $fld->type = 'VARCHAR'; 00359 $fld->max_length = -1; 00360 return $fld; 00361 } 00362 00363 function _initrs() 00364 { 00365 $this->_numOfRows = @sqlite_num_rows($this->_queryID); 00366 $this->_numOfFields = @sqlite_num_fields($this->_queryID); 00367 } 00368 00369 function Fields($colname) 00370 { 00371 if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname]; 00372 if (!$this->bind) { 00373 $this->bind = array(); 00374 for ($i=0; $i < $this->_numOfFields; $i++) { 00375 $o = $this->FetchField($i); 00376 $this->bind[strtoupper($o->name)] = $i; 00377 } 00378 } 00379 00380 return $this->fields[$this->bind[strtoupper($colname)]]; 00381 } 00382 00383 function _seek($row) 00384 { 00385 return sqlite_seek($this->_queryID, $row); 00386 } 00387 00388 function _fetch($ignore_fields=false) 00389 { 00390 $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode); 00391 return !empty($this->fields); 00392 } 00393 00394 function _close() 00395 { 00396 } 00397 00398 } 00399 ?>