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]

   1  <?php
   2  /**
   3   * FileDescription
   4   *
   5   * Currently unsupported: MetaDatabases, MetaTables and MetaColumns,
   6   * and also inputarr in Execute.
   7   * Native types have been converted to MetaTypes.
   8   * Transactions not supported yet.
   9   *
  10   * Limitation of url length. For IIS, see MaxClientRequestBuffer registry value.
  11   *
  12   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
  13   *
  14   * @package ADOdb
  15   * @link https://adodb.org Project's web site and documentation
  16   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  17   *
  18   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  19   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  20   * any later version. This means you can use it in proprietary products.
  21   * See the LICENSE.md file distributed with this source code for details.
  22   * @license BSD-3-Clause
  23   * @license LGPL-2.1-or-later
  24   *
  25   * @copyright 2000-2013 John Lim
  26   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  27   */
  28  
  29  // security - hide paths
  30  if (!defined('ADODB_DIR')) die();
  31  
  32  if (! defined("_ADODB_CSV_LAYER")) {
  33   define("_ADODB_CSV_LAYER", 1 );
  34  
  35  include_once (ADODB_DIR.'/adodb-csvlib.inc.php');
  36  
  37  class ADODB_csv extends ADOConnection {
  38  	 var $databaseType = 'csv';
  39  	 var $databaseProvider = 'csv';
  40  	 var $hasInsertID = true;
  41  	 var $hasAffectedRows = true;
  42  	 var $fmtTimeStamp = "'Y-m-d H:i:s'";
  43  	 var $_affectedrows=0;
  44  	 var $_insertid=0;
  45  	 var $_url;
  46  	 var $replaceQuote = "''"; // string to use to replace quotes
  47  	 var $hasTransactions = false;
  48  	 var $_errorNo = false;
  49  
  50  	protected function _insertID($table = '', $column = '')
  51  	 {
  52  	 	 return $this->_insertid;
  53  	 }
  54  
  55  	function _affectedrows()
  56  	 {
  57  	 	 return $this->_affectedrows;
  58  	 }
  59  
  60    	function MetaDatabases()
  61  	 {
  62  	 	 return false;
  63  	 }
  64  
  65  
  66  	 // returns true or false
  67  	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  68  	 {
  69  	 	 if (strtolower(substr($argHostname,0,7)) !== 'http://') return false;
  70  	 	 $this->_url = $argHostname;
  71  	 	 return true;
  72  	 }
  73  
  74  	 // returns true or false
  75  	function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  76  	 {
  77  	 	 if (strtolower(substr($argHostname,0,7)) !== 'http://') return false;
  78  	 	 $this->_url = $argHostname;
  79  	 	 return true;
  80  	 }
  81  
  82   	function MetaColumns($table, $normalize=true)
  83  	 {
  84  	 	 return false;
  85  	 }
  86  
  87  
  88  	 // parameters use PostgreSQL convention, not MySQL
  89  	function SelectLimit($sql, $nrows = -1, $offset = -1, $inputarr = false, $secs2cache = 0)
  90  	 {
  91  	 	 global $ADODB_FETCH_MODE;
  92  
  93  	 	 $nrows = (int) $nrows;
  94  	 	 $offset = (int) $offset;
  95  	 	 $url = $this->_url.'?sql='.urlencode($sql)."&nrows=$nrows&fetch=".
  96  	 	 	 (($this->fetchMode !== false)?$this->fetchMode : $ADODB_FETCH_MODE).
  97  	 	 	 "&offset=$offset";
  98  	 	 $err = false;
  99  	 	 $rs = csv2rs($url,$err,false);
 100  
 101  	 	 if ($this->debug) print "$url<br><i>$err</i><br>";
 102  
 103  	 	 $at = strpos($err,'::::');
 104  	 	 if ($at === false) {
 105  	 	 	 $this->_errorMsg = $err;
 106  	 	 	 $this->_errorNo = (integer)$err;
 107  	 	 } else {
 108  	 	 	 $this->_errorMsg = substr($err,$at+4,1024);
 109  	 	 	 $this->_errorNo = -9999;
 110  	 	 }
 111  	 	 if ($this->_errorNo)
 112  	 	 	 if ($fn = $this->raiseErrorFn) {
 113  	 	 	 	 $fn($this->databaseType,'EXECUTE',$this->ErrorNo(),$this->ErrorMsg(),$sql,'');
 114  	 	 	 }
 115  
 116  	 	 if (is_object($rs)) {
 117  
 118  	 	 	 $rs->databaseType='csv';
 119  	 	 	 $rs->fetchMode = ($this->fetchMode !== false) ?  $this->fetchMode : $ADODB_FETCH_MODE;
 120  	 	 	 $rs->connection = $this;
 121  	 	 }
 122  	 	 return $rs;
 123  	 }
 124  
 125  	 // returns queryID or false
 126  	function _Execute($sql,$inputarr=false)
 127  	 {
 128  	 global $ADODB_FETCH_MODE;
 129  
 130  	 	 if (!$this->_bindInputArray && $inputarr) {
 131  	 	 	 $sqlarr = explode('?',$sql);
 132  	 	 	 $sql = '';
 133  	 	 	 $i = 0;
 134  	 	 	 foreach($inputarr as $v) {
 135  
 136  	 	 	 	 $sql .= $sqlarr[$i];
 137  	 	 	 	 if (gettype($v) == 'string')
 138  	 	 	 	 	 $sql .= $this->qstr($v);
 139  	 	 	 	 else if ($v === null)
 140  	 	 	 	 	 $sql .= 'NULL';
 141  	 	 	 	 else
 142  	 	 	 	 	 $sql .= $v;
 143  	 	 	 	 $i += 1;
 144  
 145  	 	 	 }
 146  	 	 	 $sql .= $sqlarr[$i];
 147  	 	 	 if ($i+1 != sizeof($sqlarr))
 148  	 	 	 	 print "Input Array does not match ?: ".htmlspecialchars($sql);
 149  	 	 	 $inputarr = false;
 150  	 	 }
 151  
 152  	 	 $url =  $this->_url.'?sql='.urlencode($sql)."&fetch=".
 153  	 	 	 (($this->fetchMode !== false)?$this->fetchMode : $ADODB_FETCH_MODE);
 154  	 	 $err = false;
 155  
 156  
 157  	 	 $rs = csv2rs($url,$err,false);
 158  	 	 if ($this->debug) print urldecode($url)."<br><i>$err</i><br>";
 159  	 	 $at = strpos($err,'::::');
 160  	 	 if ($at === false) {
 161  	 	 	 $this->_errorMsg = $err;
 162  	 	 	 $this->_errorNo = (integer)$err;
 163  	 	 } else {
 164  	 	 	 $this->_errorMsg = substr($err,$at+4,1024);
 165  	 	 	 $this->_errorNo = -9999;
 166  	 	 }
 167  
 168  	 	 if ($this->_errorNo)
 169  	 	 	 if ($fn = $this->raiseErrorFn) {
 170  	 	 	 	 $fn($this->databaseType,'EXECUTE',$this->ErrorNo(),$this->ErrorMsg(),$sql,$inputarr);
 171  	 	 	 }
 172  	 	 if (is_object($rs)) {
 173  	 	 	 $rs->fetchMode = ($this->fetchMode !== false) ?  $this->fetchMode : $ADODB_FETCH_MODE;
 174  
 175  	 	 	 $this->_affectedrows = $rs->affectedrows;
 176  	 	 	 $this->_insertid = $rs->insertid;
 177  	 	 	 $rs->databaseType='csv';
 178  	 	 	 $rs->connection = $this;
 179  	 	 }
 180  	 	 return $rs;
 181  	 }
 182  
 183  	 /*	 Returns: the last error message from previous database operation	 */
 184  	function ErrorMsg()
 185  	 {
 186  	 	 return $this->_errorMsg;
 187  	 }
 188  
 189  	 /*	 Returns: the last error number from previous database operation	 */
 190  	function ErrorNo()
 191  	 {
 192  	 	 return $this->_errorNo;
 193  	 }
 194  
 195  	 // returns true or false
 196  	function _close()
 197  	 {
 198  	 	 return true;
 199  	 }
 200  } // class
 201  
 202  class ADORecordset_csv extends ADORecordset {
 203  
 204  	function _close()
 205  	 {
 206  	 	 return true;
 207  	 }
 208  }
 209  
 210  } // define