Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/adodb/drivers/adodb-pdo_sqlite.inc.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /* 
00004  V5.14 8 Sept 2011  (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
00005   Released under both BSD license and Lesser GPL library license. 
00006   Whenever there is any discrepancy between the two licenses, 
00007   the BSD license will take precedence. See License.txt. 
00008   Set tabs to 4 for best viewing.
00009   
00010   Latest version is available at http://adodb.sourceforge.net
00011   
00012   Thanks Diogo Toscano (diogo#scriptcase.net) for the code.
00013         And also Sid Dunayer [sdunayer#interserv.com] for extensive fixes.
00014 */
00015 
00016 class ADODB_pdo_sqlite extends ADODB_pdo {
00017         var $metaTablesSQL   = "SELECT name FROM sqlite_master WHERE type='table'";
00018         var $sysDate         = 'current_date';
00019         var $sysTimeStamp    = 'current_timestamp';
00020         var $nameQuote       = '`';
00021         var $replaceQuote    = "''";
00022         var $hasGenID        = true;
00023         var $_genIDSQL       = "UPDATE %s SET id=id+1 WHERE id=%s";
00024         var $_genSeqSQL      = "CREATE TABLE %s (id integer)";
00025         var $_genSeqCountSQL = 'SELECT COUNT(*) FROM %s';
00026         var $_genSeq2SQL     = 'INSERT INTO %s VALUES(%s)';
00027         var $_dropSeqSQL     = 'DROP TABLE %s';
00028         var $concat_operator = '||';
00029     var $pdoDriver       = false;
00030         var $random='abs(random())';
00031     
00032         function _init($parentDriver)
00033         {
00034                 $this->pdoDriver = $parentDriver;
00035                 $parentDriver->_bindInputArray = true;
00036                 $parentDriver->hasTransactions = false; // // should be set to false because of PDO SQLite driver not supporting changing autocommit mode
00037                 $parentDriver->hasInsertID = true;
00038         }
00039 
00040         function ServerInfo()
00041         {
00042                 $parent = $this->pdoDriver;
00043                 @($ver = array_pop($parent->GetCol("SELECT sqlite_version()")));
00044                 @($enc = array_pop($parent->GetCol("PRAGMA encoding")));
00045 
00046                 $arr['version']     = $ver;
00047                 $arr['description'] = 'SQLite ';
00048                 $arr['encoding']    = $enc;
00049 
00050                 return $arr;
00051         }
00052         
00053         function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
00054         {
00055                 $parent = $this->pdoDriver;
00056                 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
00057                 $limitStr  = ($nrows >= 0)  ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
00058                 if ($secs2cache)
00059                         $rs = $parent->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
00060                 else
00061                         $rs = $parent->Execute($sql."$limitStr$offsetStr",$inputarr);
00062 
00063                 return $rs;
00064         }
00065 
00066         function GenID($seq='adodbseq',$start=1)
00067         {
00068                 $parent = $this->pdoDriver;
00069                 // if you have to modify the parameter below, your database is overloaded,
00070                 // or you need to implement generation of id's yourself!
00071                 $MAXLOOPS = 100;
00072                 while (--$MAXLOOPS>=0) {
00073                         @($num = array_pop($parent->GetCol("SELECT id FROM {$seq}")));
00074                         if ($num === false || !is_numeric($num)) {
00075                                 @$parent->Execute(sprintf($this->_genSeqSQL ,$seq));
00076                                 $start -= 1;
00077                                 $num = '0';
00078                                 $cnt = $parent->GetOne(sprintf($this->_genSeqCountSQL,$seq));
00079                                 if (!$cnt) {
00080                                         $ok = $parent->Execute(sprintf($this->_genSeq2SQL,$seq,$start));
00081                                 }
00082                                 if (!$ok) return false;
00083                         }
00084                         $parent->Execute(sprintf($this->_genIDSQL,$seq,$num));
00085 
00086                         if ($parent->affected_rows() > 0) {
00087                                 $num += 1;
00088                                 $parent->genID = intval($num);
00089                                 return intval($num);
00090                         }
00091                 }
00092                 if ($fn = $parent->raiseErrorFn) {
00093                         $fn($parent->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
00094                 }
00095                 return false;
00096         }
00097 
00098         function CreateSequence($seqname='adodbseq',$start=1)
00099         {
00100                 $parent = $this->pdoDriver;
00101                 $ok = $parent->Execute(sprintf($this->_genSeqSQL,$seqname));
00102                 if (!$ok) return false;
00103                 $start -= 1;
00104                 return $parent->Execute("insert into $seqname values($start)");
00105         }
00106 
00107         function SetTransactionMode($transaction_mode)
00108         {
00109                 $parent = $this->pdoDriver;
00110                 $parent->_transmode = strtoupper($transaction_mode);
00111         }
00112 
00113         function BeginTrans()
00114         {       
00115                 $parent = $this->pdoDriver;
00116                 if ($parent->transOff) return true; 
00117                 $parent->transCnt += 1;
00118                 $parent->_autocommit = false;
00119                 return $parent->Execute("BEGIN {$parent->_transmode}");
00120         }
00121         
00122         function CommitTrans($ok=true) 
00123         { 
00124                 $parent = $this->pdoDriver;
00125                 if ($parent->transOff) return true; 
00126                 if (!$ok) return $parent->RollbackTrans();
00127                 if ($parent->transCnt) $parent->transCnt -= 1;
00128                 $parent->_autocommit = true;
00129                 
00130                 $ret = $parent->Execute('COMMIT');
00131                 return $ret;
00132         }
00133         
00134         function RollbackTrans()
00135         {
00136                 $parent = $this->pdoDriver;
00137                 if ($parent->transOff) return true; 
00138                 if ($parent->transCnt) $parent->transCnt -= 1;
00139                 $parent->_autocommit = true;
00140                 
00141                 $ret = $parent->Execute('ROLLBACK');
00142                 return $ret;
00143         }
00144 
00145 
00146     // mark newnham
00147         function MetaColumns($tab,$normalize=true)
00148         {
00149           global $ADODB_FETCH_MODE;
00150 
00151           $parent = $this->pdoDriver;
00152           $false = false;
00153           $save = $ADODB_FETCH_MODE;
00154           $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00155           if ($parent->fetchMode !== false) $savem = $parent->SetFetchMode(false);
00156           $rs = $parent->Execute("PRAGMA table_info('$tab')");
00157           if (isset($savem)) $parent->SetFetchMode($savem);
00158           if (!$rs) {
00159             $ADODB_FETCH_MODE = $save; 
00160             return $false;
00161           }
00162           $arr = array();
00163           while ($r = $rs->FetchRow()) {
00164             $type = explode('(',$r['type']);
00165             $size = '';
00166             if (sizeof($type)==2)
00167             $size = trim($type[1],')');
00168             $fn = strtoupper($r['name']);
00169             $fld = new ADOFieldObject;
00170             $fld->name = $r['name'];
00171             $fld->type = $type[0];
00172             $fld->max_length = $size;
00173             $fld->not_null = $r['notnull'];
00174             $fld->primary_key = $r['pk'];
00175             $fld->default_value = $r['dflt_value'];
00176             $fld->scale = 0;
00177             if ($save == ADODB_FETCH_NUM) $arr[] = $fld;        
00178             else $arr[strtoupper($fld->name)] = $fld;
00179           }
00180           $rs->Close();
00181           $ADODB_FETCH_MODE = $save;
00182           return $arr;
00183         }
00184 
00185         function MetaTables($ttype=false,$showSchema=false,$mask=false)
00186         {
00187                 $parent = $this->pdoDriver;
00188                 
00189                 if ($mask) {
00190                         $save = $this->metaTablesSQL;
00191                         $mask = $this->qstr(strtoupper($mask));
00192                         $this->metaTablesSQL .= " AND name LIKE $mask";
00193                 }
00194                 
00195                 $ret = $parent->GetCol($this->metaTablesSQL);
00196                 
00197                 if ($mask) {
00198                         $this->metaTablesSQL = $save;
00199                 }
00200                 return $ret;
00201    }
00202 }
00203 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations