Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403]

   1  <?php
   2  /**
   3   * ADOdb PDO dblib 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 2019 Damien Regad, Mark Newnham and the ADOdb community
  20   */
  21  
  22  class ADODB_pdo_dblib extends ADODB_pdo
  23  {
  24  	 var $hasTop = 'top';
  25  	 var $sysDate = 'convert(datetime,convert(char,GetDate(),102),102)';
  26  	 var $sysTimeStamp = 'GetDate()';
  27  	 var $metaDatabasesSQL = "select name from sysdatabases where name <> 'master'";
  28  	 var $metaTablesSQL="select name,case when type='U' then 'T' else 'V' end from sysobjects where (type='U' or type='V') and (name not in ('sysallocations','syscolumns','syscomments','sysdepends','sysfilegroups','sysfiles','sysfiles1','sysforeignkeys','sysfulltextcatalogs','sysindexes','sysindexkeys','sysmembers','sysobjects','syspermissions','sysprotects','sysreferences','systypes','sysusers','sysalternates','sysconstraints','syssegments','REFERENTIAL_CONSTRAINTS','CHECK_CONSTRAINTS','CONSTRAINT_TABLE_USAGE','CONSTRAINT_COLUMN_USAGE','VIEWS','VIEW_TABLE_USAGE','VIEW_COLUMN_USAGE','SCHEMATA','TABLES','TABLE_CONSTRAINTS','TABLE_PRIVILEGES','COLUMNS','COLUMN_DOMAIN_USAGE','COLUMN_PRIVILEGES','DOMAINS','DOMAIN_CONSTRAINTS','KEY_COLUMN_USAGE','dtproperties'))";
  29  
  30  	 var $metaColumnsSQL = "SELECT c.NAME, OBJECT_NAME(c.id) as tbl_name, c.length, c.isnullable, c.status, ( CASE WHEN c.xusertype=61 THEN 0 ELSE c.xprec END), ( CASE WHEN c.xusertype=61 THEN 0 ELSE c.xscale END), ISNULL(i.is_primary_key, 0) as primary_key FROM   syscolumns c INNER JOIN systypes t ON t.xusertype=c.xusertype INNER JOIN sysobjects o ON o.id=c.id LEFT JOIN sys.index_columns ic ON ic.object_id = c.id AND c.colid = ic.column_id LEFT JOIN sys.indexes i ON i.object_id = ic.object_id AND i.index_id = ic.index_id WHERE c.id = OBJECT_ID('%s') ORDER by c.colid";
  31  
  32  	function _init(ADODB_pdo $parentDriver)
  33  	 {
  34  	 	 $parentDriver->hasTransactions = true;
  35  	 	 $parentDriver->_bindInputArray = true;
  36  	 	 $parentDriver->hasInsertID = true;
  37  	 	 $parentDriver->fmtTimeStamp = "'Y-m-d H:i:s'";
  38  	 	 $parentDriver->fmtDate = "'Y-m-d'";
  39  	 }
  40  
  41  	function BeginTrans()
  42  	 {
  43  	 	 $returnval = parent::BeginTrans();
  44  	 	 return $returnval;
  45  	 }
  46  
  47  	function MetaColumns($table, $normalize=true)
  48  	 {
  49  	 	 $this->_findschema($table,$schema);
  50  	 	 if ($schema) {
  51  	 	 	 $dbName = $this->database;
  52  	 	 	 $this->SelectDB($schema);
  53  	 	 }
  54  	 	 global $ADODB_FETCH_MODE;
  55  	 	 $save = $ADODB_FETCH_MODE;
  56  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  57  
  58  	 	 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  59  	 	 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  60  
  61  	 	 if ($schema) {
  62  	 	 	 $this->SelectDB($dbName);
  63  	 	 }
  64  
  65  	 	 if (isset($savem)) $this->SetFetchMode($savem);
  66  	 	 $ADODB_FETCH_MODE = $save;
  67  	 	 if (!is_object($rs)) {
  68  	 	 	 $false = false;
  69  	 	 	 return $false;
  70  	 	 }
  71  
  72  	 	 $retarr = array();
  73  	 	 while (!$rs->EOF) {
  74  	 	 	 $fld = new ADOFieldObject();
  75  	 	 	 $fld->name = $rs->fields[0];
  76  	 	 	 $fld->type = $rs->fields[1];
  77  	 	 	 $fld->primary_key = $rs->fields[7];
  78  
  79  	 	 	 $fld->not_null = (!$rs->fields[3]);
  80  	 	 	 $fld->auto_increment = ($rs->fields[4] == 128);	 	 // sys.syscolumns status field. 0x80 = 128 ref: http://msdn.microsoft.com/en-us/library/ms186816.aspx
  81  
  82  	 	 	 if (isset($rs->fields[5]) && $rs->fields[5]) {
  83  	 	 	 	 if ($rs->fields[5]>0) $fld->max_length = $rs->fields[5];
  84  	 	 	 	 $fld->scale = $rs->fields[6];
  85  	 	 	 	 if ($fld->scale>0) $fld->max_length += 1;
  86  	 	 	 } else
  87  	 	 	 	 $fld->max_length = $rs->fields[2];
  88  
  89  	 	 	 if ($save == ADODB_FETCH_NUM) {
  90  	 	 	 	 $retarr[] = $fld;
  91  	 	 	 } else {
  92  	 	 	 	 $retarr[strtoupper($fld->name)] = $fld;
  93  	 	 	 }
  94  	 	 	 $rs->MoveNext();
  95  	 	 }
  96  
  97  	 	 $rs->Close();
  98  	 	 return $retarr;
  99  	 }
 100  
 101  	function MetaTables($ttype=false,$showSchema=false,$mask=false)
 102  	 {
 103  	 	 if ($mask) {
 104  	 	 	 $save = $this->metaTablesSQL;
 105  	 	 	 $mask = $this->qstr(($mask));
 106  	 	 	 $this->metaTablesSQL .= " AND name like $mask";
 107  	 	 }
 108  	 	 $ret = ADOConnection::MetaTables($ttype,$showSchema);
 109  
 110  	 	 if ($mask) {
 111  	 	 	 $this->metaTablesSQL = $save;
 112  	 	 }
 113  	 	 return $ret;
 114  	 }
 115  
 116  	function SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
 117  	 {
 118  	 	 if ($nrows > 0 && $offset <= 0) {
 119  	 	 	 $sql = preg_replace(
 120  	 	 	 	 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql);
 121  
 122  	 	 	 if ($secs2cache)
 123  	 	 	 	 $rs = $this->CacheExecute($secs2cache, $sql, $inputarr);
 124  	 	 	 else
 125  	 	 	 	 $rs = $this->Execute($sql,$inputarr);
 126  	 	 } else
 127  	 	 	 $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
 128  
 129  	 	 return $rs;
 130  	 }
 131  
 132  	function _query($sql,$inputarr=false)
 133  	 {
 134  	 	 $this->_connectionID->setAttribute(\PDO::ATTR_EMULATE_PREPARES , true);
 135  	 	 if (is_array($sql)) {
 136  	 	 	 $stmt = $sql[1];
 137  	 	 } else {
 138  	 	 	 $stmt = $this->_connectionID->prepare($sql);
 139  	 	 }
 140  
 141  	 	 if ($stmt) {
 142  	 	 	 $this->_driver->debug = $this->debug;
 143  	 	 	 if ($inputarr) {
 144  	 	 	 	 foreach ($inputarr as $key => $value) {
 145  	 	 	 	 	 if(gettype($key) == 'integer') {
 146  	 	 	 	 	 	 $key += 1;
 147  	 	 	 	 	 }
 148  	 	 	 	 	 $stmt->bindValue($key, $value, $this->GetPDODataType($value));
 149  	 	 	 	 }
 150  	 	 	 }
 151  	 	 }
 152  
 153  	 	 $ok = $stmt->execute();
 154  
 155  	 	 $this->_errormsg = false;
 156  	 	 $this->_errorno = false;
 157  
 158  	 	 if ($ok) {
 159  	 	 	 $this->_stmt = $stmt;
 160  	 	 	 return $stmt;
 161  	 	 }
 162  
 163  	 	 if ($stmt) {
 164  
 165  	 	 	 $arr = $stmt->errorinfo();
 166  	 	 	 if ((integer)$arr[1]) {
 167  	 	 	 	 $this->_errormsg = $arr[2];
 168  	 	 	 	 $this->_errorno = $arr[1];
 169  	 	 	 }
 170  
 171  	 	 } else {
 172  	 	 	 $this->_errormsg = false;
 173  	 	 	 $this->_errorno = false;
 174  	 	 }
 175  	 	 return false;
 176  	 }
 177  
 178  	private function GetPDODataType($var)
 179  	 {
 180  	 	 if(gettype($var) == 'integer') {
 181  	 	 	 return PDO::PARAM_INT ;
 182  	 	 }
 183  	 	 return PDO::PARAM_STR;
 184  	 }
 185  
 186  	function ServerInfo()
 187  	 {
 188  	 	 return ADOConnection::ServerInfo();
 189  	 }
 190  }