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 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   1  <?php
   2  /**
   3   * DB2 driver via ODBC
   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   */
  21  
  22  // security - hide paths
  23  if (!defined('ADODB_DIR')) die();
  24  
  25  if (!defined('_ADODB_ODBC_LAYER')) {
  26  	 include_once(ADODB_DIR."/drivers/adodb-odbc.inc.php");
  27  }
  28  if (!defined('ADODB_ODBC_DB2')){
  29  define('ADODB_ODBC_DB2',1);
  30  
  31  class ADODB_ODBC_DB2 extends ADODB_odbc {
  32  	 var $databaseType = "db2";
  33  	 var $concat_operator = '||';
  34  	 var $sysTime = 'CURRENT TIME';
  35  	 var $sysDate = 'CURRENT DATE';
  36  	 var $sysTimeStamp = 'CURRENT TIMESTAMP';
  37  	 // The complete string representation of a timestamp has the form
  38  	 // yyyy-mm-dd-hh.mm.ss.nnnnnn.
  39  	 var $fmtTimeStamp = "'Y-m-d-H.i.s'";
  40  	 var $ansiOuter = true;
  41  	 var $identitySQL = 'values IDENTITY_VAL_LOCAL()';
  42  	 var $_bindInputArray = true;
  43  	  var $hasInsertID = true;
  44  	 var $rsPrefix = 'ADORecordset_odbc_';
  45  
  46  	function __construct()
  47  	 {
  48  	 	 if (strncmp(PHP_OS,'WIN',3) === 0) $this->curmode = SQL_CUR_USE_ODBC;
  49  	 	 parent::__construct();
  50  	 }
  51  
  52  	function IfNull( $field, $ifNull )
  53  	 {
  54  	 	 return " COALESCE($field, $ifNull) "; // if DB2 UDB
  55  	 }
  56  
  57  	function ServerInfo()
  58  	 {
  59  	 	 //odbc_setoption($this->_connectionID,1,101 /*SQL_ATTR_ACCESS_MODE*/, 1 /*SQL_MODE_READ_ONLY*/);
  60  	 	 $vers = $this->GetOne('select versionnumber from sysibm.sysversions');
  61  	 	 //odbc_setoption($this->_connectionID,1,101, 0 /*SQL_MODE_READ_WRITE*/);
  62  	 	 return array('description'=>'DB2 ODBC driver', 'version'=>$vers);
  63  	 }
  64  
  65  	protected function _insertID($table = '', $column = '')
  66  	 {
  67  	 	 return $this->GetOne($this->identitySQL);
  68  	 }
  69  
  70  	function RowLock($tables,$where,$col='1 as adodbignore')
  71  	 {
  72  	 	 if ($this->_autocommit) $this->BeginTrans();
  73  	 	 return $this->GetOne("select $col from $tables where $where for update");
  74  	 }
  75  
  76  	function MetaTables($ttype=false,$showSchema=false, $qtable="%", $qschema="%")
  77  	 {
  78  	 global $ADODB_FETCH_MODE;
  79  
  80  	 	 $savem = $ADODB_FETCH_MODE;
  81  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  82  	 	 $qid = odbc_tables($this->_connectionID, "", $qschema, $qtable, "");
  83  
  84  	 	 $rs = new ADORecordSet_odbc($qid);
  85  
  86  	 	 $ADODB_FETCH_MODE = $savem;
  87  	 	 if (!$rs) {
  88  	 	 	 $false = false;
  89  	 	 	 return $false;
  90  	 	 }
  91  
  92  	 	 $arr = $rs->GetArray();
  93  	 	 //print_r($arr);
  94  
  95  	 	 $rs->Close();
  96  	 	 $arr2 = array();
  97  
  98  	 	 if ($ttype) {
  99  	 	 	 $isview = strncmp($ttype,'V',1) === 0;
 100  	 	 }
 101  	 	 for ($i=0; $i < sizeof($arr); $i++) {
 102  
 103  	 	 	 if (!$arr[$i][2]) continue;
 104  	 	 	 if (strncmp($arr[$i][1],'SYS',3) === 0) continue;
 105  
 106  	 	 	 $type = $arr[$i][3];
 107  
 108  	 	 	 if ($showSchema) $arr[$i][2] = $arr[$i][1].'.'.$arr[$i][2];
 109  
 110  	 	 	 if ($ttype) {
 111  	 	 	 	 if ($isview) {
 112  	 	 	 	 	 if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2];
 113  	 	 	 	 } else if (strncmp($type,'T',1) === 0) $arr2[] = $arr[$i][2];
 114  	 	 	 } else if (strncmp($type,'S',1) !== 0) $arr2[] = $arr[$i][2];
 115  	 	 }
 116  	 	 return $arr2;
 117  	 }
 118  
 119  	function MetaIndexes ($table, $primary = FALSE, $owner=false)
 120  	 {
 121          // save old fetch mode
 122          global $ADODB_FETCH_MODE;
 123          $save = $ADODB_FETCH_MODE;
 124          $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 125          if ($this->fetchMode !== FALSE) {
 126                 $savem = $this->SetFetchMode(FALSE);
 127          }
 128  	 	 $false = false;
 129  	 	 // get index details
 130  	 	 $table = strtoupper($table);
 131  	 	 $SQL="SELECT NAME, UNIQUERULE, COLNAMES FROM SYSIBM.SYSINDEXES WHERE TBNAME='$table'";
 132          if ($primary)
 133  	 	 	 $SQL.= " AND UNIQUERULE='P'";
 134  	 	 $rs = $this->Execute($SQL);
 135          if (!is_object($rs)) {
 136  	 	 	 if (isset($savem))
 137  	 	 	 	 $this->SetFetchMode($savem);
 138  	 	 	 $ADODB_FETCH_MODE = $save;
 139              return $false;
 140          }
 141  	 	 $indexes = array ();
 142          // parse index data into array
 143          while ($row = $rs->FetchRow()) {
 144  	 	 	 $indexes[$row[0]] = array(
 145  	 	 	    'unique' => ($row[1] == 'U' || $row[1] == 'P'),
 146  	 	 	    'columns' => array()
 147  	 	 	 );
 148  	 	 	 $cols = ltrim($row[2],'+');
 149  	 	 	 $indexes[$row[0]]['columns'] = explode('+', $cols);
 150          }
 151  	 	 if (isset($savem)) {
 152              $this->SetFetchMode($savem);
 153  	 	 	 $ADODB_FETCH_MODE = $save;
 154  	 	 }
 155          return $indexes;
 156  	 }
 157  
 158  	 // Format date column in sql string given an input format that understands Y M D
 159  	function SQLDate($fmt, $col=false)
 160  	 {
 161  	 // use right() and replace() ?
 162  	 	 if (!$col) $col = $this->sysDate;
 163  	 	 $s = '';
 164  
 165  	 	 $len = strlen($fmt);
 166  	 	 for ($i=0; $i < $len; $i++) {
 167  	 	 	 if ($s) $s .= '||';
 168  	 	 	 $ch = $fmt[$i];
 169  	 	 	 switch($ch) {
 170  	 	 	 case 'Y':
 171  	 	 	 case 'y':
 172  	 	 	 	 $s .= "char(year($col))";
 173  	 	 	 	 break;
 174  	 	 	 case 'M':
 175  	 	 	 	 $s .= "substr(monthname($col),1,3)";
 176  	 	 	 	 break;
 177  	 	 	 case 'm':
 178  	 	 	 	 $s .= "right(digits(month($col)),2)";
 179  	 	 	 	 break;
 180  	 	 	 case 'D':
 181  	 	 	 case 'd':
 182  	 	 	 	 $s .= "right(digits(day($col)),2)";
 183  	 	 	 	 break;
 184  	 	 	 case 'H':
 185  	 	 	 case 'h':
 186  	 	 	 	 if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)";
 187  	 	 	 	 else $s .= "''";
 188  	 	 	 	 break;
 189  	 	 	 case 'i':
 190  	 	 	 case 'I':
 191  	 	 	 	 if ($col != $this->sysDate)
 192  	 	 	 	 	 $s .= "right(digits(minute($col)),2)";
 193  	 	 	 	 	 else $s .= "''";
 194  	 	 	 	 break;
 195  	 	 	 case 'S':
 196  	 	 	 case 's':
 197  	 	 	 	 if ($col != $this->sysDate)
 198  	 	 	 	 	 $s .= "right(digits(second($col)),2)";
 199  	 	 	 	 else $s .= "''";
 200  	 	 	 	 break;
 201  	 	 	 default:
 202  	 	 	 	 if ($ch == '\\') {
 203  	 	 	 	 	 $i++;
 204  	 	 	 	 	 $ch = substr($fmt,$i,1);
 205  	 	 	 	 }
 206  	 	 	 	 $s .= $this->qstr($ch);
 207  	 	 	 }
 208  	 	 }
 209  	 	 return $s;
 210  	 }
 211  
 212  
 213  	function SelectLimit($sql, $nrows = -1, $offset = -1, $inputArr = false, $secs2cache = 0)
 214  	 {
 215  	 	 $nrows = (integer) $nrows;
 216  	 	 if ($offset <= 0) {
 217  	 	 // could also use " OPTIMIZE FOR $nrows ROWS "
 218  	 	 	 if ($nrows >= 0) $sql .=  " FETCH FIRST $nrows ROWS ONLY ";
 219  	 	 	 $rs = $this->Execute($sql,$inputArr);
 220  	 	 } else {
 221  	 	 	 if ($offset > 0 && $nrows < 0);
 222  	 	 	 else {
 223  	 	 	 	 $nrows += $offset;
 224  	 	 	 	 $sql .=  " FETCH FIRST $nrows ROWS ONLY ";
 225  	 	 	 }
 226  	 	 	 $rs = ADOConnection::SelectLimit($sql,-1,$offset,$inputArr);
 227  	 	 }
 228  
 229  	 	 return $rs;
 230  	 }
 231  
 232  };
 233  
 234  
 235  class  ADORecordSet_odbc_db2 extends ADORecordSet_odbc {
 236  
 237  	 var $databaseType = "db2";
 238  
 239  	function MetaType($t,$len=-1,$fieldobj=false)
 240  	 {
 241  	 	 if (is_object($t)) {
 242  	 	 	 $fieldobj = $t;
 243  	 	 	 $t = $fieldobj->type;
 244  	 	 	 $len = $fieldobj->max_length;
 245  	 	 }
 246  
 247  	 	 switch (strtoupper($t)) {
 248  	 	 case 'VARCHAR':
 249  	 	 case 'CHAR':
 250  	 	 case 'CHARACTER':
 251  	 	 case 'C':
 252  	 	 	 if ($len <= $this->blobSize) return 'C';
 253  
 254  	 	 case 'LONGCHAR':
 255  	 	 case 'TEXT':
 256  	 	 case 'CLOB':
 257  	 	 case 'DBCLOB': // double-byte
 258  	 	 case 'X':
 259  	 	 	 return 'X';
 260  
 261  	 	 case 'BLOB':
 262  	 	 case 'GRAPHIC':
 263  	 	 case 'VARGRAPHIC':
 264  	 	 	 return 'B';
 265  
 266  	 	 case 'DATE':
 267  	 	 case 'D':
 268  	 	 	 return 'D';
 269  
 270  	 	 case 'TIME':
 271  	 	 case 'TIMESTAMP':
 272  	 	 case 'T':
 273  	 	 	 return 'T';
 274  
 275  	 	 //case 'BOOLEAN':
 276  	 	 //case 'BIT':
 277  	 	 //	 return 'L';
 278  
 279  	 	 //case 'COUNTER':
 280  	 	 //	 return 'R';
 281  
 282  	 	 case 'INT':
 283  	 	 case 'INTEGER':
 284  	 	 case 'BIGINT':
 285  	 	 case 'SMALLINT':
 286  	 	 case 'I':
 287  	 	 	 return 'I';
 288  
 289  	 	 default: return ADODB_DEFAULT_METATYPE;
 290  	 	 }
 291  	 }
 292  }
 293  
 294  } //define