Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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) {
 166  	 	 	 $savem = $parent->SetFetchMode(false);
 167  	 	 }
 168  	 	 $rs = $parent->Execute("PRAGMA table_info('$tab')");
 169  	 	 if (isset($savem)) {
 170  	 	 	 $parent->SetFetchMode($savem);
 171  	 	 }
 172  	 	 if (!$rs) {
 173  	 	 	 $ADODB_FETCH_MODE = $save;
 174  	 	 	 return $false;
 175  	 	 }
 176  	 	 $arr = array();
 177  	 	 while ($r = $rs->FetchRow()) {
 178  	 	 	 $type = explode('(', $r['type']);
 179  	 	 	 $size = '';
 180  	 	 	 if (sizeof($type) == 2) {
 181  	 	 	 	 $size = trim($type[1], ')');
 182  	 	 	 }
 183  	 	 	 $fn = strtoupper($r['name']);
 184  	 	 	 $fld = new ADOFieldObject;
 185  	 	 	 $fld->name = $r['name'];
 186  	 	 	 $fld->type = $type[0];
 187  	 	 	 $fld->max_length = $size;
 188  	 	 	 $fld->not_null = $r['notnull'];
 189  	 	 	 $fld->primary_key = $r['pk'];
 190  	 	 	 $fld->default_value = $r['dflt_value'];
 191  	 	 	 $fld->scale = 0;
 192  	 	 	 if ($save == ADODB_FETCH_NUM) {
 193  	 	 	 	 $arr[] = $fld;
 194  	 	 	 } else {
 195  	 	 	 	 $arr[strtoupper($fld->name)] = $fld;
 196  	 	 	 }
 197  	 	 }
 198  	 	 $rs->Close();
 199  	 	 $ADODB_FETCH_MODE = $save;
 200  	 	 return $arr;
 201  	 }
 202  
 203  	function MetaTables($ttype=false,$showSchema=false,$mask=false)
 204  	 {
 205  	 	 $parent = $this->pdoDriver;
 206  
 207  	 	 if ($mask) {
 208  	 	 	 $save = $this->metaTablesSQL;
 209  	 	 	 $mask = $this->qstr(strtoupper($mask));
 210  	 	 	 $this->metaTablesSQL .= " AND name LIKE $mask";
 211  	 	 }
 212  
 213  	 	 $ret = $parent->GetCol($this->metaTablesSQL);
 214  
 215  	 	 if ($mask) {
 216  	 	 	 $this->metaTablesSQL = $save;
 217  	 	 }
 218  	 	 return $ret;
 219  	 }
 220  
 221  	 /**
 222  	  * Returns a driver-specific format for a bind parameter
 223  	  *
 224  	  * @param string $name
 225  	  * @param string $type (ignored in driver)
 226  	  *
 227  	  * @return string
 228  	  */
 229  	public function param($name,$type='C')
 230  	 {
 231  	 	 return sprintf(':%s', $name);
 232  	 }
 233  }