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