Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

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