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] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  /**
   3   * Base ODBC 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   */
  21  
  22  // security - hide paths
  23  if (!defined('ADODB_DIR')) die();
  24  
  25    define("_ADODB_ODBC_LAYER", 2 );
  26  
  27  /*
  28   * These constants are used to set define MetaColumns() method's behavior.
  29   * - METACOLUMNS_RETURNS_ACTUAL makes the driver return the actual type, 
  30   *   like all other drivers do (default)
  31   * - METACOLUMNS_RETURNS_META is provided for legacy compatibility (makes
  32   *   driver behave as it did prior to v5.21)
  33   *
  34   * @see $metaColumnsReturnType
  35   */
  36  DEFINE('METACOLUMNS_RETURNS_ACTUAL', 0);
  37  DEFINE('METACOLUMNS_RETURNS_META', 1);
  38  	 
  39  /*--------------------------------------------------------------------------------------
  40  --------------------------------------------------------------------------------------*/
  41  
  42  
  43  class ADODB_odbc extends ADOConnection {
  44  	 var $databaseType = "odbc";
  45  	 var $fmtDate = "'Y-m-d'";
  46  	 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  47  	 var $replaceQuote = "''"; // string to use to replace quotes
  48  	 var $dataProvider = "odbc";
  49  	 var $hasAffectedRows = true;
  50  	 var $binmode = ODBC_BINMODE_RETURN;
  51  	 var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive
  52  	 	 	 	 	 	 	 	 // breaking backward-compat
  53  	 //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field
  54  	 var $_bindInputArray = false;
  55  	 var $curmode = SQL_CUR_USE_DRIVER; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L
  56  	 var $_genSeqSQL = "create table %s (id integer)";
  57  	 var $_autocommit = true;
  58  	 var $_lastAffectedRows = 0;
  59  	 var $uCaseTables = true; // for meta* functions, uppercase table names
  60  	 
  61  	 /*
  62  	  * Tells the metaColumns feature whether to return actual or meta type
  63  	  */
  64  	 public $metaColumnsReturnType = METACOLUMNS_RETURNS_ACTUAL;
  65  
  66  	function __construct() {}
  67  
  68  	 	 // returns true or false
  69  	function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
  70  	 {
  71  	 	 if (!function_exists('odbc_connect')) return null;
  72  
  73  	 	 if (!empty($argDatabasename) && stristr($argDSN, 'Database=') === false) {
  74  	 	 	 $argDSN = trim($argDSN);
  75  	 	 	 $endDSN = substr($argDSN, strlen($argDSN) - 1);
  76  	 	 	 if ($endDSN != ';') $argDSN .= ';';
  77  	 	 	 $argDSN .= 'Database='.$argDatabasename;
  78  	 	 }
  79  
  80  	 	 $last_php_error = $this->resetLastError();
  81  	 	 if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
  82  	 	 else $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword,$this->curmode);
  83  	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  84  	 	 if ($this->connectStmt) {
  85  	 	 	 $this->Execute($this->connectStmt);
  86  	 	 }
  87  
  88  	 	 return $this->_connectionID != false;
  89  	 }
  90  
  91  	 // returns true or false
  92  	function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
  93  	 {
  94  	 	 if (!function_exists('odbc_connect')) return null;
  95  
  96  	 	 $last_php_error = $this->resetLastError();
  97  	 	 $this->_errorMsg = '';
  98  	 	 if ($this->debug && $argDatabasename) {
  99  	 	 	 ADOConnection::outp("For odbc PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
 100  	 	 }
 101  	 //	 print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush();
 102  	 	 if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
 103  	 	 else $this->_connectionID = odbc_pconnect($argDSN,$argUsername,$argPassword,$this->curmode);
 104  
 105  	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
 106  	 	 if ($this->_connectionID && $this->autoRollback) @odbc_rollback($this->_connectionID);
 107  	 	 if ($this->connectStmt) {
 108  	 	 	 $this->Execute($this->connectStmt);
 109  	 	 }
 110  
 111  	 	 return $this->_connectionID != false;
 112  	 }
 113  
 114  
 115  	function ServerInfo()
 116  	 {
 117  
 118  	 	 if (!empty($this->host)) {
 119  	 	 	 $dsn = strtoupper($this->host);
 120  	 	 	 $first = true;
 121  	 	 	 $found = false;
 122  
 123  	 	 	 if (!function_exists('odbc_data_source')) return false;
 124  
 125  	 	 	 while(true) {
 126  
 127  	 	 	 	 $rez = @odbc_data_source($this->_connectionID,
 128  	 	 	 	 	 $first ? SQL_FETCH_FIRST : SQL_FETCH_NEXT);
 129  	 	 	 	 $first = false;
 130  	 	 	 	 if (!is_array($rez)) break;
 131  	 	 	 	 if (strtoupper($rez['server']) == $dsn) {
 132  	 	 	 	 	 $found = true;
 133  	 	 	 	 	 break;
 134  	 	 	 	 }
 135  	 	 	 }
 136  	 	 	 if (!$found) return ADOConnection::ServerInfo();
 137  	 	 	 if (!isset($rez['version'])) $rez['version'] = '';
 138  	 	 	 return $rez;
 139  	 	 } else {
 140  	 	 	 return ADOConnection::ServerInfo();
 141  	 	 }
 142  	 }
 143  
 144  
 145  	function CreateSequence($seqname='adodbseq',$start=1)
 146  	 {
 147  	 	 if (empty($this->_genSeqSQL)) return false;
 148  	 	 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
 149  	 	 if (!$ok) return false;
 150  	 	 $start -= 1;
 151  	 	 return $this->Execute("insert into $seqname values($start)");
 152  	 }
 153  
 154  	 var $_dropSeqSQL = 'drop table %s';
 155  	function DropSequence($seqname = 'adodbseq')
 156  	 {
 157  	 	 if (empty($this->_dropSeqSQL)) return false;
 158  	 	 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
 159  	 }
 160  
 161  	 /*
 162  	 	 This algorithm is not very efficient, but works even if table locking
 163  	 	 is not available.
 164  
 165  	 	 Will return false if unable to generate an ID after $MAXLOOPS attempts.
 166  	 */
 167  	function GenID($seq='adodbseq',$start=1)
 168  	 {
 169  	 	 // if you have to modify the parameter below, your database is overloaded,
 170  	 	 // or you need to implement generation of id's yourself!
 171  	 	 $MAXLOOPS = 100;
 172  	 	 //$this->debug=1;
 173  	 	 while (--$MAXLOOPS>=0) {
 174  	 	 	 $num = $this->GetOne("select id from $seq");
 175  	 	 	 if ($num === false) {
 176  	 	 	 	 $this->Execute(sprintf($this->_genSeqSQL ,$seq));
 177  	 	 	 	 $start -= 1;
 178  	 	 	 	 $num = '0';
 179  	 	 	 	 $ok = $this->Execute("insert into $seq values($start)");
 180  	 	 	 	 if (!$ok) return false;
 181  	 	 	 }
 182  	 	 	 $this->Execute("update $seq set id=id+1 where id=$num");
 183  
 184  	 	 	 if ($this->affected_rows() > 0) {
 185  	 	 	 	 $num += 1;
 186  	 	 	 	 $this->genID = $num;
 187  	 	 	 	 return $num;
 188  	 	 	 } elseif ($this->affected_rows() == 0) {
 189  	 	 	 	 // some drivers do not return a valid value => try with another method
 190  	 	 	 	 $value = $this->GetOne("select id from $seq");
 191  	 	 	 	 if ($value == $num + 1) {
 192  	 	 	 	 	 return $value;
 193  	 	 	 	 }
 194  	 	 	 }
 195  	 	 }
 196  	 	 if ($fn = $this->raiseErrorFn) {
 197  	 	 	 $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
 198  	 	 }
 199  	 	 return false;
 200  	 }
 201  
 202  
 203  	function ErrorMsg()
 204  	 {
 205  	 	 if ($this->_errorMsg !== false) return $this->_errorMsg;
 206  	 	 if (empty($this->_connectionID)) return @odbc_errormsg();
 207  	 	 return @odbc_errormsg($this->_connectionID);
 208  	 }
 209  
 210  	function ErrorNo()
 211  	 {
 212  	 	 if ($this->_errorCode !== false) {
 213  	 	 	 // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
 214  	 	 	 return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode;
 215  	 	 }
 216  
 217  	 	 if (empty($this->_connectionID)) $e = @odbc_error();
 218  	 	 else $e = @odbc_error($this->_connectionID);
 219  
 220  	 	  // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
 221  	 	  // so we check and patch
 222  	 	 if (strlen($e)<=2) return 0;
 223  	 	 return $e;
 224  	 }
 225  
 226  
 227  
 228  	function BeginTrans()
 229  	 {
 230  	 	 if (!$this->hasTransactions) return false;
 231  	 	 if ($this->transOff) return true;
 232  	 	 $this->transCnt += 1;
 233  	 	 $this->_autocommit = false;
 234  	 	 return odbc_autocommit($this->_connectionID,false);
 235  	 }
 236  
 237  	function CommitTrans($ok=true)
 238  	 {
 239  	 	 if ($this->transOff) return true;
 240  	 	 if (!$ok) return $this->RollbackTrans();
 241  	 	 if ($this->transCnt) $this->transCnt -= 1;
 242  	 	 $this->_autocommit = true;
 243  	 	 $ret = odbc_commit($this->_connectionID);
 244  	 	 odbc_autocommit($this->_connectionID,true);
 245  	 	 return $ret;
 246  	 }
 247  
 248  	function RollbackTrans()
 249  	 {
 250  	 	 if ($this->transOff) return true;
 251  	 	 if ($this->transCnt) $this->transCnt -= 1;
 252  	 	 $this->_autocommit = true;
 253  	 	 $ret = odbc_rollback($this->_connectionID);
 254  	 	 odbc_autocommit($this->_connectionID,true);
 255  	 	 return $ret;
 256  	 }
 257  
 258  	function MetaPrimaryKeys($table,$owner=false)
 259  	 {
 260  	 global $ADODB_FETCH_MODE;
 261  
 262  	 	 if ($this->uCaseTables) $table = strtoupper($table);
 263  	 	 $schema = '';
 264  	 	 $this->_findschema($table,$schema);
 265  
 266  	 	 $savem = $ADODB_FETCH_MODE;
 267  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 268  	 	 $qid = @odbc_primarykeys($this->_connectionID,'',$schema,$table);
 269  
 270  	 	 if (!$qid) {
 271  	 	 	 $ADODB_FETCH_MODE = $savem;
 272  	 	 	 return false;
 273  	 	 }
 274  	 	 $rs = new ADORecordSet_odbc($qid);
 275  	 	 $ADODB_FETCH_MODE = $savem;
 276  
 277  	 	 if (!$rs) return false;
 278  
 279  	 	 $arr = $rs->GetArray();
 280  	 	 $rs->Close();
 281  	 	 //print_r($arr);
 282  	 	 $arr2 = array();
 283  	 	 for ($i=0; $i < sizeof($arr); $i++) {
 284  	 	 	 if ($arr[$i][3]) $arr2[] = $arr[$i][3];
 285  	 	 }
 286  	 	 return $arr2;
 287  	 }
 288  
 289  
 290  
 291  	function MetaTables($ttype=false,$showSchema=false,$mask=false)
 292  	 {
 293  	 global $ADODB_FETCH_MODE;
 294  
 295  	 	 $savem = $ADODB_FETCH_MODE;
 296  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 297  	 	 $qid = odbc_tables($this->_connectionID);
 298  
 299  	 	 $rs = new ADORecordSet_odbc($qid);
 300  
 301  	 	 $ADODB_FETCH_MODE = $savem;
 302  	 	 if (!$rs) {
 303  	 	 	 $false = false;
 304  	 	 	 return $false;
 305  	 	 }
 306  
 307  	 	 $arr = $rs->GetArray();
 308  	 	 //print_r($arr);
 309  
 310  	 	 $rs->Close();
 311  	 	 $arr2 = array();
 312  
 313  	 	 if ($ttype) {
 314  	 	 	 $isview = strncmp($ttype,'V',1) === 0;
 315  	 	 }
 316  	 	 for ($i=0; $i < sizeof($arr); $i++) {
 317  	 	 	 if (!$arr[$i][2]) continue;
 318  	 	 	 $type = $arr[$i][3];
 319  	 	 	 if ($ttype) {
 320  	 	 	 	 if ($isview) {
 321  	 	 	 	 	 if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2];
 322  	 	 	 	 } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2];
 323  	 	 	 } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2];
 324  	 	 }
 325  	 	 return $arr2;
 326  	 }
 327  
 328  /*
 329  See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp
 330  / SQL data type codes /
 331  #define	 SQL_UNKNOWN_TYPE	 0
 332  #define SQL_CHAR	 	 	 1
 333  #define SQL_NUMERIC	 	  2
 334  #define SQL_DECIMAL	 	  3
 335  #define SQL_INTEGER	 	  4
 336  #define SQL_SMALLINT	 	 5
 337  #define SQL_FLOAT	 	    6
 338  #define SQL_REAL	 	 	 7
 339  #define SQL_DOUBLE	 	   8
 340  #if (ODBCVER >= 0x0300)
 341  #define SQL_DATETIME	 	 9
 342  #endif
 343  #define SQL_VARCHAR	 	 12
 344  
 345  
 346  / One-parameter shortcuts for date/time data types /
 347  #if (ODBCVER >= 0x0300)
 348  #define SQL_TYPE_DATE	   91
 349  #define SQL_TYPE_TIME	   92
 350  #define SQL_TYPE_TIMESTAMP 93
 351  
 352  #define SQL_UNICODE                             (-95)
 353  #define SQL_UNICODE_VARCHAR                     (-96)
 354  #define SQL_UNICODE_LONGVARCHAR                 (-97)
 355  */
 356  	function ODBCTypes($t)
 357  	 {
 358  	 	 switch ((integer)$t) {
 359  	 	 case 1:
 360  	 	 case 12:
 361  	 	 case 0:
 362  	 	 case -95:
 363  	 	 case -96:
 364  	 	 	 return 'C';
 365  	 	 case -97:
 366  	 	 case -1: //text
 367  	 	 	 return 'X';
 368  	 	 case -4: //image
 369  	 	 	 return 'B';
 370  
 371  	 	 case 9:
 372  	 	 case 91:
 373  	 	 	 return 'D';
 374  
 375  	 	 case 10:
 376  	 	 case 11:
 377  	 	 case 92:
 378  	 	 case 93:
 379  	 	 	 return 'T';
 380  
 381  	 	 case 4:
 382  	 	 case 5:
 383  	 	 case -6:
 384  	 	 	 return 'I';
 385  
 386  	 	 case -11: // uniqidentifier
 387  	 	 	 return 'R';
 388  	 	 case -7: //bit
 389  	 	 	 return 'L';
 390  
 391  	 	 default:
 392  	 	 	 return 'N';
 393  	 	 }
 394  	 }
 395  
 396  	function MetaColumns($table, $normalize=true)
 397  	 {
 398  	 global $ADODB_FETCH_MODE;
 399  
 400  	 	 $false = false;
 401  	 	 if ($this->uCaseTables) $table = strtoupper($table);
 402  	 	 $schema = '';
 403  	 	 $this->_findschema($table,$schema);
 404  
 405  	 	 $savem = $ADODB_FETCH_MODE;
 406  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 407  
 408  	 	 /*if (false) { // after testing, confirmed that the following does not work because of a bug
 409  	 	 	 $qid2 = odbc_tables($this->_connectionID);
 410  	 	 	 $rs = new ADORecordSet_odbc($qid2);
 411  	 	 	 $ADODB_FETCH_MODE = $savem;
 412  	 	 	 if (!$rs) return false;
 413  	 	 	 $rs->_fetch();
 414  
 415  	 	 	 while (!$rs->EOF) {
 416  	 	 	 	 if ($table == strtoupper($rs->fields[2])) {
 417  	 	 	 	 	 $q = $rs->fields[0];
 418  	 	 	 	 	 $o = $rs->fields[1];
 419  	 	 	 	 	 break;
 420  	 	 	 	 }
 421  	 	 	 	 $rs->MoveNext();
 422  	 	 	 }
 423  	 	 	 $rs->Close();
 424  
 425  	 	 	 $qid = odbc_columns($this->_connectionID,$q,$o,strtoupper($table),'%');
 426  	 	 } */
 427  
 428  	 	 switch ($this->databaseType) {
 429  	 	 case 'access':
 430  	 	 case 'vfp':
 431  	 	 	 $qid = odbc_columns($this->_connectionID);#,'%','',strtoupper($table),'%');
 432  	 	 	 break;
 433  
 434  
 435  	 	 case 'db2':
 436              $colname = "%";
 437              $qid = odbc_columns($this->_connectionID, "", $schema, $table, $colname);
 438              break;
 439  
 440  	 	 default:
 441  	 	 	 $qid = @odbc_columns($this->_connectionID,'%','%',strtoupper($table),'%');
 442  	 	 	 if (empty($qid)) $qid = odbc_columns($this->_connectionID);
 443  	 	 	 break;
 444  	 	 }
 445  	 	 if (empty($qid)) return $false;
 446  
 447  	 	 $rs = new ADORecordSet_odbc($qid);
 448  	 	 $ADODB_FETCH_MODE = $savem;
 449  
 450  	 	 if (!$rs) return $false;
 451  	 	 $rs->_fetch();
 452  
 453  	 	 $retarr = array();
 454  
 455  	 	 /*
 456  	 	 $rs->fields indices
 457  	 	 0 TABLE_QUALIFIER
 458  	 	 1 TABLE_SCHEM
 459  	 	 2 TABLE_NAME
 460  	 	 3 COLUMN_NAME
 461  	 	 4 DATA_TYPE
 462  	 	 5 TYPE_NAME
 463  	 	 6 PRECISION
 464  	 	 7 LENGTH
 465  	 	 8 SCALE
 466  	 	 9 RADIX
 467  	 	 10 NULLABLE
 468  	 	 11 REMARKS
 469  	 	 */
 470  	 	 while (!$rs->EOF) {
 471  	 	 //	 adodb_pr($rs->fields);
 472  	 	 	 if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
 473  	 	 	 	 $fld = new ADOFieldObject();
 474  	 	 	 	 $fld->name = $rs->fields[3];
 475  	 	 	 	 if ($this->metaColumnsReturnType == METACOLUMNS_RETURNS_META)
 476  	 	 	 	 	 /* 
 477  	 	 	 	     * This is the broken, original value
 478  	 	 	 	 	 */
 479  	 	 	 	 	 $fld->type = $this->ODBCTypes($rs->fields[4]);
 480  	 	 	 	 else
 481  	 	 	 	 	 /*
 482  	 	 	 	     * This is the correct new value
 483  	 	 	 	 	 */
 484  	 	 	 	     $fld->type = $rs->fields[4];
 485  
 486  	 	 	 	 // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp
 487  	 	 	 	 // access uses precision to store length for char/varchar
 488  	 	 	 	 if ($fld->type == 'C' or $fld->type == 'X') {
 489  	 	 	 	 	 if ($this->databaseType == 'access')
 490  	 	 	 	 	 	 $fld->max_length = $rs->fields[6];
 491  	 	 	 	 	 else if ($rs->fields[4] <= -95) // UNICODE
 492  	 	 	 	 	 	 $fld->max_length = $rs->fields[7]/2;
 493  	 	 	 	 	 else
 494  	 	 	 	 	 	 $fld->max_length = $rs->fields[7];
 495  	 	 	 	 } else
 496  	 	 	 	 	 $fld->max_length = $rs->fields[7];
 497  	 	 	 	 $fld->not_null = !empty($rs->fields[10]);
 498  	 	 	 	 $fld->scale = $rs->fields[8];
 499  	 	 	 	 $retarr[strtoupper($fld->name)] = $fld;
 500  	 	 	 } else if (sizeof($retarr)>0)
 501  	 	 	 	 break;
 502  	 	 	 $rs->MoveNext();
 503  	 	 }
 504  	 	 $rs->Close(); //-- crashes 4.03pl1 -- why?
 505  
 506  	 	 if (empty($retarr)) $retarr = false;
 507  	 	 return $retarr;
 508  	 }
 509  
 510  	function Prepare($sql)
 511  	 {
 512  	 	 if (! $this->_bindInputArray) return $sql; // no binding
 513  	 	 $stmt = odbc_prepare($this->_connectionID,$sql);
 514  	 	 if (!$stmt) {
 515  	 	 	 // we don't know whether odbc driver is parsing prepared stmts, so just return sql
 516  	 	 	 return $sql;
 517  	 	 }
 518  	 	 return array($sql,$stmt,false);
 519  	 }
 520  
 521  	 /* returns queryID or false */
 522  	function _query($sql,$inputarr=false)
 523  	 {
 524  	 	 $last_php_error = $this->resetLastError();
 525  	 	 $this->_errorMsg = '';
 526  
 527  	 	 if ($inputarr) {
 528  	 	 	 if (is_array($sql)) {
 529  	 	 	 	 $stmtid = $sql[1];
 530  	 	 	 } else {
 531  	 	 	 	 $stmtid = odbc_prepare($this->_connectionID,$sql);
 532  
 533  	 	 	 	 if ($stmtid == false) {
 534  	 	 	 	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
 535  	 	 	 	 	 return false;
 536  	 	 	 	 }
 537  	 	 	 }
 538  
 539  	 	 	 if (! odbc_execute($stmtid,$inputarr)) {
 540  	 	 	 	 //@odbc_free_result($stmtid);
 541  	 	 	 	 $this->_errorMsg = odbc_errormsg();
 542  	 	 	 	 $this->_errorCode = odbc_error();
 543  	 	 	 	 return false;
 544  	 	 	 }
 545  
 546  	 	 } else if (is_array($sql)) {
 547  	 	 	 $stmtid = $sql[1];
 548  	 	 	 if (!odbc_execute($stmtid)) {
 549  	 	 	 	 //@odbc_free_result($stmtid);
 550  	 	 	 	 $this->_errorMsg = odbc_errormsg();
 551  	 	 	 	 $this->_errorCode = odbc_error();
 552  	 	 	 	 return false;
 553  	 	 	 }
 554  	 	 } else
 555  	 	 	 $stmtid = odbc_exec($this->_connectionID,$sql);
 556  
 557  	 	 $this->_lastAffectedRows = 0;
 558  	 	 if ($stmtid) {
 559  	 	 	 if (@odbc_num_fields($stmtid) == 0) {
 560  	 	 	 	 $this->_lastAffectedRows = odbc_num_rows($stmtid);
 561  	 	 	 	 $stmtid = true;
 562  	 	 	 } else {
 563  	 	 	 	 $this->_lastAffectedRows = 0;
 564  	 	 	 	 odbc_binmode($stmtid,$this->binmode);
 565  	 	 	 	 odbc_longreadlen($stmtid,$this->maxblobsize);
 566  	 	 	 }
 567  
 568  	 	 	 $this->_errorMsg = '';
 569  	 	 	 $this->_errorCode = 0;
 570  	 	 } else {
 571  	 	 	 $this->_errorMsg = odbc_errormsg();
 572  	 	 	 $this->_errorCode = odbc_error();
 573  	 	 }
 574  	 	 return $stmtid;
 575  	 }
 576  
 577  	 /*
 578  	 	 Insert a null into the blob field of the table first.
 579  	 	 Then use UpdateBlob to store the blob.
 580  
 581  	 	 Usage:
 582  
 583  	 	 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 584  	 	 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 585  	 */
 586  	function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 587  	 {
 588  	 	 return $this->Execute("UPDATE $table SET $column=? WHERE $where",array($val)) != false;
 589  	 }
 590  
 591  	 // returns true or false
 592  	function _close()
 593  	 {
 594  	 	 $ret = @odbc_close($this->_connectionID);
 595  	 	 $this->_connectionID = false;
 596  	 	 return $ret;
 597  	 }
 598  
 599  	function _affectedrows()
 600  	 {
 601  	 	 return $this->_lastAffectedRows;
 602  	 }
 603  
 604  }
 605  
 606  /*--------------------------------------------------------------------------------------
 607  	  Class Name: Recordset
 608  --------------------------------------------------------------------------------------*/
 609  
 610  class ADORecordSet_odbc extends ADORecordSet {
 611  
 612  	 var $bind = false;
 613  	 var $databaseType = "odbc";
 614  	 var $dataProvider = "odbc";
 615  	 var $useFetchArray;
 616  
 617  	function __construct($id,$mode=false)
 618  	 {
 619  	 	 if ($mode === false) {
 620  	 	 	 global $ADODB_FETCH_MODE;
 621  	 	 	 $mode = $ADODB_FETCH_MODE;
 622  	 	 }
 623  	 	 $this->fetchMode = $mode;
 624  
 625  	 	 $this->_queryID = $id;
 626  
 627  	 	 // the following is required for mysql odbc driver in 4.3.1 -- why?
 628  	 	 $this->EOF = false;
 629  	 	 $this->_currentRow = -1;
 630  	 	 //parent::__construct($id);
 631  	 }
 632  
 633  
 634  	 // returns the field object
 635  	function FetchField($fieldOffset = -1)
 636  	 {
 637  
 638  	 	 $off=$fieldOffset+1; // offsets begin at 1
 639  
 640  	 	 $o= new ADOFieldObject();
 641  	 	 $o->name = @odbc_field_name($this->_queryID,$off);
 642  	 	 $o->type = @odbc_field_type($this->_queryID,$off);
 643  	 	 $o->max_length = @odbc_field_len($this->_queryID,$off);
 644  	 	 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
 645  	 	 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
 646  	 	 return $o;
 647  	 }
 648  
 649  	 /* Use associative array to get fields array */
 650  	function Fields($colname)
 651  	 {
 652  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
 653  	 	 if (!$this->bind) {
 654  	 	 	 $this->bind = array();
 655  	 	 	 for ($i=0; $i < $this->_numOfFields; $i++) {
 656  	 	 	 	 $o = $this->FetchField($i);
 657  	 	 	 	 $this->bind[strtoupper($o->name)] = $i;
 658  	 	 	 }
 659  	 	 }
 660  
 661  	 	  return $this->fields[$this->bind[strtoupper($colname)]];
 662  	 }
 663  
 664  
 665  	function _initrs()
 666  	 {
 667  	 global $ADODB_COUNTRECS;
 668  	 	 $this->_numOfRows = ($ADODB_COUNTRECS) ? @odbc_num_rows($this->_queryID) : -1;
 669  	 	 $this->_numOfFields = @odbc_num_fields($this->_queryID);
 670  	 	 // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0
 671  	 	 if ($this->_numOfRows == 0) $this->_numOfRows = -1;
 672  	 	 //$this->useFetchArray = $this->connection->useFetchArray;
 673  	 }
 674  
 675  	function _seek($row)
 676  	 {
 677  	 	 return false;
 678  	 }
 679  
 680  	 // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated
 681  	function GetArrayLimit($nrows,$offset=-1)
 682  	 {
 683  	 	 if ($offset <= 0) {
 684  	 	 	 $rs = $this->GetArray($nrows);
 685  	 	 	 return $rs;
 686  	 	 }
 687  	 	 $savem = $this->fetchMode;
 688  	 	 $this->fetchMode = ADODB_FETCH_NUM;
 689  	 	 $this->Move($offset);
 690  	 	 $this->fetchMode = $savem;
 691  
 692  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
 693  	 	 	 $this->fields = $this->GetRowAssoc();
 694  	 	 }
 695  
 696  	 	 $results = array();
 697  	 	 $cnt = 0;
 698  	 	 while (!$this->EOF && $nrows != $cnt) {
 699  	 	 	 $results[$cnt++] = $this->fields;
 700  	 	 	 $this->MoveNext();
 701  	 	 }
 702  
 703  	 	 return $results;
 704  	 }
 705  
 706  
 707  	function MoveNext()
 708  	 {
 709  	 	 if ($this->_numOfRows != 0 && !$this->EOF) {
 710  	 	 	 $this->_currentRow++;
 711  	 	 	 if( $this->_fetch() ) {
 712  	 	 	 	 return true;
 713  	 	 	 }
 714  	 	 }
 715  	 	 $this->fields = false;
 716  	 	 $this->EOF = true;
 717  	 	 return false;
 718  	 }
 719  
 720  	function _fetch()
 721  	 {
 722  	 	 $this->fields = false;
 723  	 	 $rez = @odbc_fetch_into($this->_queryID,$this->fields);
 724  	 	 if ($rez) {
 725  	 	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
 726  	 	 	 	 $this->fields = $this->GetRowAssoc();
 727  	 	 	 }
 728  	 	 	 return true;
 729  	 	 }
 730  	 	 return false;
 731  	 }
 732  
 733  	function _close()
 734  	 {
 735  	 	 return @odbc_free_result($this->_queryID);
 736  	 }
 737  
 738  }