Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  /*
   3  @version   v5.20.16  12-Jan-2020
   4  @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   5  @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   6    Released under both BSD license and Lesser GPL library license.
   7    Whenever there is any discrepancy between the two licenses,
   8    the BSD license will take precedence.
   9    Set tabs to 8.
  10  
  11  */
  12  
  13  class ADODB_pdo_mysql extends ADODB_pdo {
  14  
  15  	 var $metaTablesSQL = "SELECT
  16  	 	 	 TABLE_NAME,
  17  	 	 	 CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END
  18  	 	 FROM INFORMATION_SCHEMA.TABLES
  19  	 	 WHERE TABLE_SCHEMA=";
  20  	 var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
  21  	 var $sysDate = 'CURDATE()';
  22  	 var $sysTimeStamp = 'NOW()';
  23  	 var $hasGenID = true;
  24  	 var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
  25  	 var $_dropSeqSQL = "drop table %s";
  26  	 var $fmtTimeStamp = "'Y-m-d H:i:s'";
  27  	 var $nameQuote = '`';
  28  
  29  	function _init($parentDriver)
  30  	 {
  31  	 	 $parentDriver->hasTransactions = false;
  32  	 	 #$parentDriver->_bindInputArray = false;
  33  	 	 $parentDriver->hasInsertID = true;
  34  	 	 $parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  35  	 }
  36  
  37  	 // dayFraction is a day in floating point
  38  	function OffsetDate($dayFraction, $date=false)
  39  	 {
  40  	 	 if (!$date) {
  41  	 	 	 $date = $this->sysDate;
  42  	 	 }
  43  
  44  	 	 $fraction = $dayFraction * 24 * 3600;
  45  	 	 return $date . ' + INTERVAL ' .	 $fraction . ' SECOND';
  46  //	 	 return "from_unixtime(unix_timestamp($date)+$fraction)";
  47  	 }
  48  
  49  	function Concat()
  50  	 {
  51  	 	 $s = '';
  52  	 	 $arr = func_get_args();
  53  
  54  	 	 // suggestion by andrew005#mnogo.ru
  55  	 	 $s = implode(',', $arr);
  56  	 	 if (strlen($s) > 0) {
  57  	 	 	 return "CONCAT($s)";
  58  	 	 }
  59  	 	 return '';
  60  	 }
  61  
  62  	function ServerInfo()
  63  	 {
  64  	 	 $arr['description'] = ADOConnection::GetOne('select version()');
  65  	 	 $arr['version'] = ADOConnection::_findvers($arr['description']);
  66  	 	 return $arr;
  67  	 }
  68  
  69  	function MetaTables($ttype=false, $showSchema=false, $mask=false)
  70  	 {
  71  	 	 $save = $this->metaTablesSQL;
  72  	 	 if ($showSchema && is_string($showSchema)) {
  73  	 	 	 $this->metaTablesSQL .= $this->qstr($showSchema);
  74  	 	 } else {
  75  	 	 	 $this->metaTablesSQL .= 'schema()';
  76  	 	 }
  77  
  78  	 	 if ($mask) {
  79  	 	 	 $mask = $this->qstr($mask);
  80  	 	 	 $this->metaTablesSQL .= " like $mask";
  81  	 	 }
  82  	 	 $ret = ADOConnection::MetaTables($ttype, $showSchema);
  83  
  84  	 	 $this->metaTablesSQL = $save;
  85  	 	 return $ret;
  86  	 }
  87  
  88      /**
  89       * @param bool $auto_commit
  90       * @return void
  91       */
  92      function SetAutoCommit($auto_commit)
  93      {
  94          $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT, $auto_commit);
  95      }
  96  
  97  	function SetTransactionMode($transaction_mode)
  98  	 {
  99  	 	 $this->_transmode  = $transaction_mode;
 100  	 	 if (empty($transaction_mode)) {
 101  	 	 	 $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
 102  	 	 	 return;
 103  	 	 }
 104  	 	 if (!stristr($transaction_mode, 'isolation')) {
 105  	 	 	 $transaction_mode = 'ISOLATION LEVEL ' . $transaction_mode;
 106  	 	 }
 107  	 	 $this->Execute('SET SESSION TRANSACTION ' . $transaction_mode);
 108  	 }
 109  
 110  	function MetaColumns($table, $normalize=true)
 111  	 {
 112  	 	 $this->_findschema($table, $schema);
 113  	 	 if ($schema) {
 114  	 	 	 $dbName = $this->database;
 115  	 	 	 $this->SelectDB($schema);
 116  	 	 }
 117  	 	 global $ADODB_FETCH_MODE;
 118  	 	 $save = $ADODB_FETCH_MODE;
 119  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 120  
 121  	 	 if ($this->fetchMode !== false) {
 122  	 	 	 $savem = $this->SetFetchMode(false);
 123  	 	 }
 124  	 	 $rs = $this->Execute(sprintf($this->metaColumnsSQL, $table));
 125  
 126  	 	 if ($schema) {
 127  	 	 	 $this->SelectDB($dbName);
 128  	 	 }
 129  
 130  	 	 if (isset($savem)) {
 131  	 	 	 $this->SetFetchMode($savem);
 132  	 	 }
 133  	 	 $ADODB_FETCH_MODE = $save;
 134  	 	 if (!is_object($rs)) {
 135  	 	 	 $false = false;
 136  	 	 	 return $false;
 137  	 	 }
 138  
 139  	 	 $retarr = array();
 140  	 	 while (!$rs->EOF){
 141  	 	 	 $fld = new ADOFieldObject();
 142  	 	 	 $fld->name = $rs->fields[0];
 143  	 	 	 $type = $rs->fields[1];
 144  
 145  	 	 	 // split type into type(length):
 146  	 	 	 $fld->scale = null;
 147  	 	 	 if (preg_match('/^(.+)\((\d+),(\d+)/', $type, $query_array)) {
 148  	 	 	 	 $fld->type = $query_array[1];
 149  	 	 	 	 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
 150  	 	 	 	 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
 151  	 	 	 } elseif (preg_match('/^(.+)\((\d+)/', $type, $query_array)) {
 152  	 	 	 	 $fld->type = $query_array[1];
 153  	 	 	 	 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
 154  	 	 	 } elseif (preg_match('/^(enum)\((.*)\)$/i', $type, $query_array)) {
 155  	 	 	 	 $fld->type = $query_array[1];
 156  	 	 	 	 $arr = explode(',', $query_array[2]);
 157  	 	 	 	 $fld->enums = $arr;
 158  	 	 	 	 $zlen = max(array_map('strlen', $arr)) - 2; // PHP >= 4.0.6
 159  	 	 	 	 $fld->max_length = ($zlen > 0) ? $zlen : 1;
 160  	 	 	 } else {
 161  	 	 	 	 $fld->type = $type;
 162  	 	 	 	 $fld->max_length = -1;
 163  	 	 	 }
 164  	 	 	 $fld->not_null = ($rs->fields[2] != 'YES');
 165  	 	 	 $fld->primary_key = ($rs->fields[3] == 'PRI');
 166  	 	 	 $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
 167  	 	 	 $fld->binary = (strpos($type, 'blob') !== false);
 168  	 	 	 $fld->unsigned = (strpos($type, 'unsigned') !== false);
 169  
 170  	 	 	 if (!$fld->binary) {
 171  	 	 	 	 $d = $rs->fields[4];
 172  	 	 	 	 if ($d != '' && $d != 'NULL') {
 173  	 	 	 	 	 $fld->has_default = true;
 174  	 	 	 	 	 $fld->default_value = $d;
 175  	 	 	 	 } else {
 176  	 	 	 	 	 $fld->has_default = false;
 177  	 	 	 	 }
 178  	 	 	 }
 179  
 180  	 	 	 if ($save == ADODB_FETCH_NUM) {
 181  	 	 	 	 $retarr[] = $fld;
 182  	 	 	 } else {
 183  	 	 	 	 $retarr[strtoupper($fld->name)] = $fld;
 184  	 	 	 }
 185  	 	 	 $rs->MoveNext();
 186  	 	 }
 187  
 188  	 	 $rs->Close();
 189  	 	 return $retarr;
 190  	 }
 191  
 192  	 // returns true or false
 193  	function SelectDB($dbName)
 194  	 {
 195  	 	 $this->database = $dbName;
 196  	 	 $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
 197  	 	 $try = $this->Execute('use ' . $dbName);
 198  	 	 return ($try !== false);
 199  	 }
 200  
 201  	 // parameters use PostgreSQL convention, not MySQL
 202  	function SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs=0)
 203  	 {
 204  	 	 $nrows = (int) $nrows;
 205  	 	 $offset = (int) $offset;	 	 
 206  	 	 $offsetStr =($offset>=0) ? "$offset," : '';
 207  	 	 // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
 208  	 	 if ($nrows < 0) {
 209  	 	 	 $nrows = '18446744073709551615';
 210  	 	 }
 211  
 212  	 	 if ($secs) {
 213  	 	 	 $rs = $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows", $inputarr);
 214  	 	 } else {
 215  	 	 	 $rs = $this->Execute($sql . " LIMIT $offsetStr$nrows", $inputarr);
 216  	 	 }
 217  	 	 return $rs;
 218  	 }
 219  
 220  	function SQLDate($fmt, $col=false)
 221  	 {
 222  	 	 if (!$col) {
 223  	 	 	 $col = $this->sysTimeStamp;
 224  	 	 }
 225  	 	 $s = 'DATE_FORMAT(' . $col . ",'";
 226  	 	 $concat = false;
 227  	 	 $len = strlen($fmt);
 228  	 	 for ($i=0; $i < $len; $i++) {
 229  	 	 	 $ch = $fmt[$i];
 230  	 	 	 switch($ch) {
 231  
 232  	 	 	 	 default:
 233  	 	 	 	 	 if ($ch == '\\') {
 234  	 	 	 	 	 	 $i++;
 235  	 	 	 	 	 	 $ch = substr($fmt, $i, 1);
 236  	 	 	 	 	 }
 237  	 	 	 	 	 // FALL THROUGH
 238  	 	 	 	 case '-':
 239  	 	 	 	 case '/':
 240  	 	 	 	 	 $s .= $ch;
 241  	 	 	 	 	 break;
 242  
 243  	 	 	 	 case 'Y':
 244  	 	 	 	 case 'y':
 245  	 	 	 	 	 $s .= '%Y';
 246  	 	 	 	 	 break;
 247  
 248  	 	 	 	 case 'M':
 249  	 	 	 	 	 $s .= '%b';
 250  	 	 	 	 	 break;
 251  
 252  	 	 	 	 case 'm':
 253  	 	 	 	 	 $s .= '%m';
 254  	 	 	 	 	 break;
 255  
 256  	 	 	 	 case 'D':
 257  	 	 	 	 case 'd':
 258  	 	 	 	 	 $s .= '%d';
 259  	 	 	 	 	 break;
 260  
 261  	 	 	 	 case 'Q':
 262  	 	 	 	 case 'q':
 263  	 	 	 	 	 $s .= "'),Quarter($col)";
 264  
 265  	 	 	 	 	 if ($len > $i+1) {
 266  	 	 	 	 	 	 $s .= ",DATE_FORMAT($col,'";
 267  	 	 	 	 	 } else {
 268  	 	 	 	 	 	 $s .= ",('";
 269  	 	 	 	 	 }
 270  	 	 	 	 	 $concat = true;
 271  	 	 	 	 	 break;
 272  
 273  	 	 	 	 case 'H':
 274  	 	 	 	 	 $s .= '%H';
 275  	 	 	 	 	 break;
 276  
 277  	 	 	 	 case 'h':
 278  	 	 	 	 	 $s .= '%I';
 279  	 	 	 	 	 break;
 280  
 281  	 	 	 	 case 'i':
 282  	 	 	 	 	 $s .= '%i';
 283  	 	 	 	 	 break;
 284  
 285  	 	 	 	 case 's':
 286  	 	 	 	 	 $s .= '%s';
 287  	 	 	 	 	 break;
 288  
 289  	 	 	 	 case 'a':
 290  	 	 	 	 case 'A':
 291  	 	 	 	 	 $s .= '%p';
 292  	 	 	 	 	 break;
 293  
 294  	 	 	 	 case 'w':
 295  	 	 	 	 	 $s .= '%w';
 296  	 	 	 	 	 break;
 297  
 298  	 	 	 	 case 'W':
 299  	 	 	 	 	 $s .= '%U';
 300  	 	 	 	 	 break;
 301  
 302  	 	 	 	 case 'l':
 303  	 	 	 	 	 $s .= '%W';
 304  	 	 	 	 	 break;
 305  	 	 	 }
 306  	 	 }
 307  	 	 $s .= "')";
 308  	 	 if ($concat) {
 309  	 	 	 $s = "CONCAT($s)";
 310  	 	 }
 311  	 	 return $s;
 312  	 }
 313  }