Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  /*
   4   @version   v5.21.0  2021-02-27
   5   @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   6   @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   7    Released under both BSD license and Lesser GPL library license.
   8    Whenever there is any discrepancy between the two licenses,
   9    the BSD license will take precedence. See License.txt.
  10    Set tabs to 4 for best viewing.
  11  
  12    Latest version is available at https://adodb.org/
  13  
  14    Thanks Diogo Toscano (diogo#scriptcase.net) for the code.
  15  	 And also Sid Dunayer [sdunayer#interserv.com] for extensive fixes.
  16  */
  17  
  18  class ADODB_pdo_sqlite extends ADODB_pdo {
  19  	 var $metaTablesSQL   = "SELECT name FROM sqlite_master WHERE type='table'";
  20  	 var $sysDate         = 'current_date';
  21  	 var $sysTimeStamp    = 'current_timestamp';
  22  	 var $nameQuote       = '`';
  23  	 var $replaceQuote    = "''";
  24  	 var $hasGenID        = true;
  25  	 var $_genIDSQL       = "UPDATE %s SET id=id+1 WHERE id=%s";
  26  	 var $_genSeqSQL      = "CREATE TABLE %s (id integer)";
  27  	 var $_genSeqCountSQL = 'SELECT COUNT(*) FROM %s';
  28  	 var $_genSeq2SQL     = 'INSERT INTO %s VALUES(%s)';
  29  	 var $_dropSeqSQL     = 'DROP TABLE %s';
  30  	 var $concat_operator = '||';
  31      var $pdoDriver       = false;
  32  	 var $random='abs(random())';
  33  
  34  	function _init($parentDriver)
  35  	 {
  36  	 	 $this->pdoDriver = $parentDriver;
  37  	 	 $parentDriver->_bindInputArray = true;
  38  	 	 $parentDriver->hasTransactions = false; // // should be set to false because of PDO SQLite driver not supporting changing autocommit mode
  39  	 	 $parentDriver->hasInsertID = true;
  40  	 }
  41  
  42  	function ServerInfo()
  43  	 {
  44  	 	 $parent = $this->pdoDriver;
  45  	 	 @($ver = array_pop($parent->GetCol("SELECT sqlite_version()")));
  46  	 	 @($enc = array_pop($parent->GetCol("PRAGMA encoding")));
  47  
  48  	 	 $arr['version']     = $ver;
  49  	 	 $arr['description'] = 'SQLite ';
  50  	 	 $arr['encoding']    = $enc;
  51  
  52  	 	 return $arr;
  53  	 }
  54  
  55  	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  56  	 {
  57  	 	 $nrows = (int) $nrows;
  58  	 	 $offset = (int) $offset;
  59  	 	 $parent = $this->pdoDriver;
  60  	 	 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
  61  	 	 $limitStr  = ($nrows >= 0)  ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
  62  	 	 if ($secs2cache)
  63  	 	 	 $rs = $parent->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
  64  	 	 else
  65  	 	 	 $rs = $parent->Execute($sql."$limitStr$offsetStr",$inputarr);
  66  
  67  	 	 return $rs;
  68  	 }
  69  
  70  	function GenID($seq='adodbseq',$start=1)
  71  	 {
  72  	 	 $parent = $this->pdoDriver;
  73  	 	 // if you have to modify the parameter below, your database is overloaded,
  74  	 	 // or you need to implement generation of id's yourself!
  75  	 	 $MAXLOOPS = 100;
  76  	 	 while (--$MAXLOOPS>=0) {
  77  	 	 	 @($num = array_pop($parent->GetCol("SELECT id FROM {$seq}")));
  78  	 	 	 if ($num === false || !is_numeric($num)) {
  79  	 	 	 	 @$parent->Execute(sprintf($this->_genSeqSQL ,$seq));
  80  	 	 	 	 $start -= 1;
  81  	 	 	 	 $num = '0';
  82  	 	 	 	 $cnt = $parent->GetOne(sprintf($this->_genSeqCountSQL,$seq));
  83  	 	 	 	 if (!$cnt) {
  84  	 	 	 	 	 $ok = $parent->Execute(sprintf($this->_genSeq2SQL,$seq,$start));
  85  	 	 	 	 }
  86  	 	 	 	 if (!$ok) return false;
  87  	 	 	 }
  88  	 	 	 $parent->Execute(sprintf($this->_genIDSQL,$seq,$num));
  89  
  90  	 	 	 if ($parent->affected_rows() > 0) {
  91                  	         $num += 1;
  92                  	 	 $parent->genID = intval($num);
  93                  	 	 return intval($num);
  94  	 	 	 }
  95  	 	 }
  96  	 	 if ($fn = $parent->raiseErrorFn) {
  97  	 	 	 $fn($parent->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
  98  	 	 }
  99  	 	 return false;
 100  	 }
 101  
 102  	function CreateSequence($seqname='adodbseq',$start=1)
 103  	 {
 104  	 	 $parent = $this->pdoDriver;
 105  	 	 $ok = $parent->Execute(sprintf($this->_genSeqSQL,$seqname));
 106  	 	 if (!$ok) return false;
 107  	 	 $start -= 1;
 108  	 	 return $parent->Execute("insert into $seqname values($start)");
 109  	 }
 110  
 111  	function SetTransactionMode($transaction_mode)
 112  	 {
 113  	 	 $parent = $this->pdoDriver;
 114  	 	 $parent->_transmode = strtoupper($transaction_mode);
 115  	 }
 116  
 117  	function BeginTrans()
 118  	 {
 119  	 	 $parent = $this->pdoDriver;
 120  	 	 if ($parent->transOff) return true;
 121  	 	 $parent->transCnt += 1;
 122  	 	 $parent->_autocommit = false;
 123  	 	 return $parent->Execute("BEGIN {$parent->_transmode}");
 124  	 }
 125  
 126  	function CommitTrans($ok=true)
 127  	 {
 128  	 	 $parent = $this->pdoDriver;
 129  	 	 if ($parent->transOff) return true;
 130  	 	 if (!$ok) return $parent->RollbackTrans();
 131  	 	 if ($parent->transCnt) $parent->transCnt -= 1;
 132  	 	 $parent->_autocommit = true;
 133  
 134  	 	 $ret = $parent->Execute('COMMIT');
 135  	 	 return $ret;
 136  	 }
 137  
 138  	function RollbackTrans()
 139  	 {
 140  	 	 $parent = $this->pdoDriver;
 141  	 	 if ($parent->transOff) return true;
 142  	 	 if ($parent->transCnt) $parent->transCnt -= 1;
 143  	 	 $parent->_autocommit = true;
 144  
 145  	 	 $ret = $parent->Execute('ROLLBACK');
 146  	 	 return $ret;
 147  	 }
 148  
 149  
 150      // mark newnham
 151  	function MetaColumns($tab,$normalize=true)
 152  	 {
 153  	   global $ADODB_FETCH_MODE;
 154  
 155  	   $parent = $this->pdoDriver;
 156  	   $false = false;
 157  	   $save = $ADODB_FETCH_MODE;
 158  	   $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
 159  	   if ($parent->fetchMode !== false) $savem = $parent->SetFetchMode(false);
 160  	   $rs = $parent->Execute("PRAGMA table_info('$tab')");
 161  	   if (isset($savem)) $parent->SetFetchMode($savem);
 162  	   if (!$rs) {
 163  	     $ADODB_FETCH_MODE = $save;
 164  	     return $false;
 165  	   }
 166  	   $arr = array();
 167  	   while ($r = $rs->FetchRow()) {
 168  	     $type = explode('(',$r['type']);
 169  	     $size = '';
 170  	     if (sizeof($type)==2)
 171  	     $size = trim($type[1],')');
 172  	     $fn = strtoupper($r['name']);
 173  	     $fld = new ADOFieldObject;
 174  	     $fld->name = $r['name'];
 175  	     $fld->type = $type[0];
 176  	     $fld->max_length = $size;
 177  	     $fld->not_null = $r['notnull'];
 178  	     $fld->primary_key = $r['pk'];
 179  	     $fld->default_value = $r['dflt_value'];
 180  	     $fld->scale = 0;
 181  	     if ($save == ADODB_FETCH_NUM) $arr[] = $fld;
 182  	     else $arr[strtoupper($fld->name)] = $fld;
 183  	   }
 184  	   $rs->Close();
 185  	   $ADODB_FETCH_MODE = $save;
 186  	   return $arr;
 187  	 }
 188  
 189  	function MetaTables($ttype=false,$showSchema=false,$mask=false)
 190  	 {
 191  	 	 $parent = $this->pdoDriver;
 192  
 193  	 	 if ($mask) {
 194  	 	 	 $save = $this->metaTablesSQL;
 195  	 	 	 $mask = $this->qstr(strtoupper($mask));
 196  	 	 	 $this->metaTablesSQL .= " AND name LIKE $mask";
 197  	 	 }
 198  
 199  	 	 $ret = $parent->GetCol($this->metaTablesSQL);
 200  
 201  	 	 if ($mask) {
 202  	 	 	 $this->metaTablesSQL = $save;
 203  	 	 }
 204  	 	 return $ret;
 205     }
 206  }