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

   1  <?php
   2  /**
   3   * ODBTP driver
   4   *
   5   * @deprecated will be removed in ADOdb version 6
   6   *
   7   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
   8   *
   9   * @package ADOdb
  10   * @link https://adodb.org Project's web site and documentation
  11   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  12   *
  13   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  14   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  15   * any later version. This means you can use it in proprietary products.
  16   * See the LICENSE.md file distributed with this source code for details.
  17   * @license BSD-3-Clause
  18   * @license LGPL-2.1-or-later
  19   *
  20   * @copyright 2000-2013 John Lim
  21   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  22   * @author stefan bogdan <sbogdan@rsb.ro>
  23   */
  24  
  25  // security - hide paths
  26  if (!defined('ADODB_DIR')) die();
  27  
  28  define("_ADODB_ODBTP_LAYER", 2 );
  29  
  30  class ADODB_odbtp extends ADOConnection{
  31  	 var $databaseType = "odbtp";
  32  	 var $dataProvider = "odbtp";
  33  	 var $fmtDate = "'Y-m-d'";
  34  	 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  35  	 var $replaceQuote = "''"; // string to use to replace quotes
  36  	 var $odbc_driver = 0;
  37  	 var $hasAffectedRows = true;
  38  	 var $hasInsertID = false;
  39  	 var $hasGenID = true;
  40  	 var $hasMoveFirst = true;
  41  
  42  	 var $_genSeqSQL = "create table %s (seq_name char(30) not null unique , seq_value integer not null)";
  43  	 var $_dropSeqSQL = "delete from adodb_seq where seq_name = '%s'";
  44  	 var $_bindInputArray = false;
  45  	 var $_useUnicodeSQL = false;
  46  	 var $_canPrepareSP = false;
  47  	 var $_dontPoolDBC = true;
  48  
  49  	function ServerInfo()
  50  	 {
  51  	 	 return array('description' => @odbtp_get_attr( ODB_ATTR_DBMSNAME, $this->_connectionID),
  52  	 	              'version' => @odbtp_get_attr( ODB_ATTR_DBMSVER, $this->_connectionID));
  53  	 }
  54  
  55  	function ErrorMsg()
  56  	 {
  57  	 	 if ($this->_errorMsg !== false) return $this->_errorMsg;
  58  	 	 if (empty($this->_connectionID)) return @odbtp_last_error();
  59  	 	 return @odbtp_last_error($this->_connectionID);
  60  	 }
  61  
  62  	function ErrorNo()
  63  	 {
  64  	 	 if ($this->_errorCode !== false) return $this->_errorCode;
  65  	 	 if (empty($this->_connectionID)) return @odbtp_last_error_state();
  66  	 	 	 return @odbtp_last_error_state($this->_connectionID);
  67  	 }
  68  /*
  69  	 function DBDate($d,$isfld=false)
  70  	 {
  71  	 	 if (empty($d) && $d !== 0) return 'null';
  72  	 	 if ($isfld) return "convert(date, $d, 120)";
  73  
  74  	 	 if (is_string($d)) $d = ADORecordSet::UnixDate($d);
  75  	 	 $d = adodb_date($this->fmtDate,$d);
  76  	 	 return "convert(date, $d, 120)";
  77  	 }
  78  
  79  	 function DBTimeStamp($d,$isfld=false)
  80  	 {
  81  	 	 if (empty($d) && $d !== 0) return 'null';
  82  	 	 if ($isfld) return "convert(datetime, $d, 120)";
  83  
  84  	 	 if (is_string($d)) $d = ADORecordSet::UnixDate($d);
  85  	 	 $d = adodb_date($this->fmtDate,$d);
  86  	 	 return "convert(datetime, $d, 120)";
  87  	 }
  88  */
  89  
  90  	protected function _insertID($table = '', $column = '')
  91  	 {
  92  	 // SCOPE_IDENTITY()
  93  	 // Returns the last IDENTITY value inserted into an IDENTITY column in
  94  	 // the same scope. A scope is a module -- a stored procedure, trigger,
  95  	 // function, or batch. Thus, two statements are in the same scope if
  96  	 // they are in the same stored procedure, function, or batch.
  97  	 	 	 return $this->GetOne($this->identitySQL);
  98  	 }
  99  
 100  	function _affectedrows()
 101  	 {
 102  	 	 if ($this->_queryID) {
 103  	 	 	 return @odbtp_affected_rows ($this->_queryID);
 104  	    } else
 105  	 	 return 0;
 106  	 }
 107  
 108  	function CreateSequence($seqname='adodbseq',$start=1)
 109  	 {
 110  	 	 //verify existence
 111  	 	 $num = $this->GetOne("select seq_value from adodb_seq");
 112  	 	 $seqtab='adodb_seq';
 113  	 	 if( $this->odbc_driver == ODB_DRIVER_FOXPRO ) {
 114  	 	 	 $path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
 115  	 	 	 //if using vfp dbc file
 116  	 	 	 if( !strcasecmp(strrchr($path, '.'), '.dbc') )
 117                  $path = substr($path,0,strrpos($path,'\/'));
 118             	 $seqtab = $path . '/' . $seqtab;
 119          }
 120  	 	 if($num == false) {
 121  	 	 	 if (empty($this->_genSeqSQL)) return false;
 122  	 	 	 $ok = $this->Execute(sprintf($this->_genSeqSQL ,$seqtab));
 123  	 	 }
 124  	 	 $num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seqname'");
 125  	 	 if ($num) {
 126  	 	 	 return false;
 127  	 	 }
 128  	 	 $start -= 1;
 129  	 	 return $this->Execute("insert into adodb_seq values('$seqname',$start)");
 130  	 }
 131  
 132  	function DropSequence($seqname = 'adodbseq')
 133  	 {
 134  	 	 if (empty($this->_dropSeqSQL)) return false;
 135  	 	 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
 136  	 }
 137  
 138  	function GenID($seq='adodbseq',$start=1)
 139  	 {
 140  	 	 $seqtab='adodb_seq';
 141  	 	 if( $this->odbc_driver == ODB_DRIVER_FOXPRO) {
 142  	 	 	 $path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
 143  	 	 	 //if using vfp dbc file
 144  	 	 	 if( !strcasecmp(strrchr($path, '.'), '.dbc') )
 145                  $path = substr($path,0,strrpos($path,'\/'));
 146             	 $seqtab = $path . '/' . $seqtab;
 147          }
 148  	 	 $MAXLOOPS = 100;
 149  	 	 while (--$MAXLOOPS>=0) {
 150  	 	 	 $num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seq'");
 151  	 	 	 if ($num === false) {
 152  	 	 	 	 //verify if abodb_seq table exist
 153  	 	 	 	 $ok = $this->GetOne("select seq_value from adodb_seq ");
 154  	 	 	 	 if(!$ok) {
 155  	 	 	 	 	 //creating the sequence table adodb_seq
 156  	 	 	 	 	 $this->Execute(sprintf($this->_genSeqSQL ,$seqtab));
 157  	 	 	 	 }
 158  	 	 	 	 $start -= 1;
 159  	 	 	 	 $num = '0';
 160  	 	 	 	 $ok = $this->Execute("insert into adodb_seq values('$seq',$start)");
 161  	 	 	 	 if (!$ok) return false;
 162  	 	 	 }
 163  	 	 	 $ok = $this->Execute("update adodb_seq set seq_value=seq_value+1 where seq_name='$seq'");
 164  	 	 	 if($ok) {
 165  	 	 	 	 $num += 1;
 166  	 	 	 	 $this->genID = $num;
 167  	 	 	 	 return $num;
 168  	 	 	 }
 169  	 	 }
 170  	 if ($fn = $this->raiseErrorFn) {
 171  	 	 $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
 172  	 }
 173  	 	 return false;
 174  	 }
 175  
 176  	 //example for $UserOrDSN
 177  	 //for visual fox : DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBF;SOURCEDB=c:\YourDbfFileDir;EXCLUSIVE=NO;
 178  	 //for visual fox dbc: DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBC;SOURCEDB=c:\YourDbcFileDir\mydb.dbc;EXCLUSIVE=NO;
 179  	 //for access : DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\path_to_access_db\base_test.mdb;UID=root;PWD=;
 180  	 //for mssql : DRIVER={SQL Server};SERVER=myserver;UID=myuid;PWD=mypwd;DATABASE=OdbtpTest;
 181  	 //if uid & pwd can be separate
 182      function _connect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
 183  	 {
 184  	 	 if ($argPassword && stripos($UserOrDSN,'DRIVER=') !== false) {
 185  	 	 	 $this->_connectionID = odbtp_connect($HostOrInterface,$UserOrDSN.';PWD='.$argPassword);
 186  	 	 } else
 187  	 	 	 $this->_connectionID = odbtp_connect($HostOrInterface,$UserOrDSN,$argPassword,$argDatabase);
 188  	 	 if ($this->_connectionID === false) {
 189  	 	 	 $this->_errorMsg = $this->ErrorMsg() ;
 190  	 	 	 return false;
 191  	 	 }
 192  
 193  	 	 odbtp_convert_datetime($this->_connectionID,true);
 194  
 195  	 	 if ($this->_dontPoolDBC) {
 196  	 	 	 if (function_exists('odbtp_dont_pool_dbc'))
 197  	 	 	 	 @odbtp_dont_pool_dbc($this->_connectionID);
 198  	 	 }
 199  	 	 else {
 200  	 	 	 $this->_dontPoolDBC = true;
 201  	 	 }
 202  	 	 $this->odbc_driver = @odbtp_get_attr(ODB_ATTR_DRIVER, $this->_connectionID);
 203  	 	 $dbms = strtolower(@odbtp_get_attr(ODB_ATTR_DBMSNAME, $this->_connectionID));
 204  	 	 $this->odbc_name = $dbms;
 205  
 206  	 	 // Account for inconsistent DBMS names
 207  	 	 if( $this->odbc_driver == ODB_DRIVER_ORACLE )
 208  	 	 	 $dbms = 'oracle';
 209  	 	 else if( $this->odbc_driver == ODB_DRIVER_SYBASE )
 210  	 	 	 $dbms = 'sybase';
 211  
 212  	 	 // Set DBMS specific attributes
 213  	 	 switch( $dbms ) {
 214  	 	 	 case 'microsoft sql server':
 215  	 	 	 	 $this->databaseType = 'odbtp_mssql';
 216  	 	 	 	 $this->fmtDate = "'Y-m-d'";
 217  	 	 	 	 $this->fmtTimeStamp = "'Y-m-d h:i:sA'";
 218  	 	 	 	 $this->sysDate = 'convert(datetime,convert(char,GetDate(),102),102)';
 219  	 	 	 	 $this->sysTimeStamp = 'GetDate()';
 220  	 	 	 	 $this->ansiOuter = true;
 221  	 	 	 	 $this->leftOuter = '*=';
 222  	 	 	 	 $this->rightOuter = '=*';
 223                  $this->hasTop = 'top';
 224  	 	 	 	 $this->hasInsertID = true;
 225  	 	 	 	 $this->hasTransactions = true;
 226  	 	 	 	 $this->_bindInputArray = true;
 227  	 	 	 	 $this->_canSelectDb = true;
 228  	 	 	 	 $this->substr = "substring";
 229  	 	 	 	 $this->length = 'len';
 230  	 	 	 	 $this->identitySQL = 'select SCOPE_IDENTITY()';
 231  	 	 	 	 $this->metaDatabasesSQL = "select name from master..sysdatabases where name <> 'master'";
 232  	 	 	 	 $this->_canPrepareSP = true;
 233  	 	 	 	 break;
 234  	 	 	 case 'access':
 235  	 	 	 	 $this->databaseType = 'odbtp_access';
 236  	 	 	 	 $this->fmtDate = "#Y-m-d#";
 237  	 	 	 	 $this->fmtTimeStamp = "#Y-m-d h:i:sA#";
 238  	 	 	 	 $this->sysDate = "FORMAT(NOW,'yyyy-mm-dd')";
 239  	 	 	 	 $this->sysTimeStamp = 'NOW';
 240                  $this->hasTop = 'top';
 241  	 	 	 	 $this->hasTransactions = false;
 242  	 	 	 	 $this->_canPrepareSP = true;  // For MS Access only.
 243  	 	 	 	 break;
 244  	 	 	 case 'visual foxpro':
 245  	 	 	 	 $this->databaseType = 'odbtp_vfp';
 246  	 	 	 	 $this->fmtDate = "{^Y-m-d}";
 247  	 	 	 	 $this->fmtTimeStamp = "{^Y-m-d, h:i:sA}";
 248  	 	 	 	 $this->sysDate = 'date()';
 249  	 	 	 	 $this->sysTimeStamp = 'datetime()';
 250  	 	 	 	 $this->ansiOuter = true;
 251                  $this->hasTop = 'top';
 252  	 	 	 	 $this->hasTransactions = false;
 253  	 	 	 	 $this->replaceQuote = "'+chr(39)+'";
 254  	 	 	 	 $this->true = '.T.';
 255  	 	 	 	 $this->false = '.F.';
 256  
 257  	 	 	 	 break;
 258  	 	 	 case 'oracle':
 259  	 	 	 	 $this->databaseType = 'odbtp_oci8';
 260  	 	 	 	 $this->fmtDate = "'Y-m-d 00:00:00'";
 261  	 	 	 	 $this->fmtTimeStamp = "'Y-m-d h:i:sA'";
 262  	 	 	 	 $this->sysDate = 'TRUNC(SYSDATE)';
 263  	 	 	 	 $this->sysTimeStamp = 'SYSDATE';
 264  	 	 	 	 $this->hasTransactions = true;
 265  	 	 	 	 $this->_bindInputArray = true;
 266  	 	 	 	 $this->concat_operator = '||';
 267  	 	 	 	 break;
 268  	 	 	 case 'sybase':
 269  	 	 	 	 $this->databaseType = 'odbtp_sybase';
 270  	 	 	 	 $this->fmtDate = "'Y-m-d'";
 271  	 	 	 	 $this->fmtTimeStamp = "'Y-m-d H:i:s'";
 272  	 	 	 	 $this->sysDate = 'GetDate()';
 273  	 	 	 	 $this->sysTimeStamp = 'GetDate()';
 274  	 	 	 	 $this->leftOuter = '*=';
 275  	 	 	 	 $this->rightOuter = '=*';
 276  	 	 	 	 $this->hasInsertID = true;
 277  	 	 	 	 $this->hasTransactions = true;
 278  	 	 	 	 $this->identitySQL = 'select SCOPE_IDENTITY()';
 279  	 	 	 	 break;
 280  	 	 	 default:
 281  	 	 	 	 $this->databaseType = 'odbtp';
 282  	 	 	 	 if( @odbtp_get_attr(ODB_ATTR_TXNCAPABLE, $this->_connectionID) )
 283  	 	 	 	 	 $this->hasTransactions = true;
 284  	 	 	 	 else
 285  	 	 	 	 	 $this->hasTransactions = false;
 286  	 	 }
 287          @odbtp_set_attr(ODB_ATTR_FULLCOLINFO, TRUE, $this->_connectionID );
 288  
 289  	 	 if ($this->_useUnicodeSQL )
 290  	 	 	 @odbtp_set_attr(ODB_ATTR_UNICODESQL, TRUE, $this->_connectionID);
 291  
 292          return true;
 293  	 }
 294  
 295  	function _pconnect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
 296  	 {
 297  	 	 $this->_dontPoolDBC = false;
 298    	 	 return $this->_connect($HostOrInterface, $UserOrDSN, $argPassword, $argDatabase);
 299  	 }
 300  
 301  	function SelectDB($dbName)
 302  	 {
 303  	 	 if (!@odbtp_select_db($dbName, $this->_connectionID)) {
 304  	 	 	 return false;
 305  	 	 }
 306  	 	 $this->database = $dbName;
 307  	 	 $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
 308  	 	 return true;
 309  	 }
 310  
 311  	function MetaTables($ttype='',$showSchema=false,$mask=false)
 312  	 {
 313  	 global $ADODB_FETCH_MODE;
 314  
 315  	 	 $savem = $ADODB_FETCH_MODE;
 316  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 317  	 	 if ($this->fetchMode !== false) $savefm = $this->SetFetchMode(false);
 318  
 319  	 	 $arr = $this->GetArray("||SQLTables||||$ttype");
 320  
 321  	 	 if (isset($savefm)) $this->SetFetchMode($savefm);
 322  	 	 $ADODB_FETCH_MODE = $savem;
 323  
 324  	 	 $arr2 = array();
 325  	 	 for ($i=0; $i < sizeof($arr); $i++) {
 326  	 	 	 if ($arr[$i][3] == 'SYSTEM TABLE' )	 continue;
 327  	 	 	 if ($arr[$i][2])
 328  	 	 	 	 $arr2[] = $showSchema && $arr[$i][1]? $arr[$i][1].'.'.$arr[$i][2] : $arr[$i][2];
 329  	 	 }
 330  	 	 return $arr2;
 331  	 }
 332  
 333  	function MetaColumns($table,$upper=true)
 334  	 {
 335  	 global $ADODB_FETCH_MODE;
 336  
 337  	 	 $schema = false;
 338  	 	 $this->_findschema($table,$schema);
 339  	 	 if ($upper) $table = strtoupper($table);
 340  
 341  	 	 $savem = $ADODB_FETCH_MODE;
 342  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 343  	 	 if ($this->fetchMode !== false) $savefm = $this->SetFetchMode(false);
 344  
 345  	 	 $rs = $this->Execute( "||SQLColumns||$schema|$table" );
 346  
 347  	 	 if (isset($savefm)) $this->SetFetchMode($savefm);
 348  	 	 $ADODB_FETCH_MODE = $savem;
 349  
 350  	 	 if (!$rs || $rs->EOF) {
 351  	 	 	 $false = false;
 352  	 	 	 return $false;
 353  	 	 }
 354  	 	 $retarr = array();
 355  	 	 while (!$rs->EOF) {
 356  	 	 	 //print_r($rs->fields);
 357  	 	 	 if (strtoupper($rs->fields[2]) == $table) {
 358  	 	 	 	 $fld = new ADOFieldObject();
 359  	 	 	 	 $fld->name = $rs->fields[3];
 360  	 	 	 	 $fld->type = $rs->fields[5];
 361  	 	 	 	 $fld->max_length = $rs->fields[6];
 362      	 	 	 $fld->not_null = !empty($rs->fields[9]);
 363   	 	 	 	 $fld->scale = $rs->fields[7];
 364  	 	 	 	 if (isset($rs->fields[12])) // vfp does not have field 12
 365  	  	 	 	 	 if (!is_null($rs->fields[12])) {
 366  	  	 	 	 	 	 $fld->has_default = true;
 367  	  	 	 	 	 	 $fld->default_value = $rs->fields[12];
 368  	 	 	 	 	 }
 369  	 	 	 	 $retarr[strtoupper($fld->name)] = $fld;
 370  	 	 	 } else if (!empty($retarr))
 371  	 	 	 	 break;
 372  	 	 	 $rs->MoveNext();
 373  	 	 }
 374  	 	 $rs->Close();
 375  
 376  	 	 return $retarr;
 377  	 }
 378  
 379  	function MetaPrimaryKeys($table, $owner='')
 380  	 {
 381  	 global $ADODB_FETCH_MODE;
 382  
 383  	 	 $savem = $ADODB_FETCH_MODE;
 384  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 385  	 	 $arr = $this->GetArray("||SQLPrimaryKeys||$owner|$table");
 386  	 	 $ADODB_FETCH_MODE = $savem;
 387  
 388  	 	 //print_r($arr);
 389  	 	 $arr2 = array();
 390  	 	 for ($i=0; $i < sizeof($arr); $i++) {
 391  	 	 	 if ($arr[$i][3]) $arr2[] = $arr[$i][3];
 392  	 	 }
 393  	 	 return $arr2;
 394  	 }
 395  
 396  	public function metaForeignKeys($table, $owner = '', $upper = false, $associative = false)
 397  	 {
 398  	 global $ADODB_FETCH_MODE;
 399  
 400  	 	 $savem = $ADODB_FETCH_MODE;
 401  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 402  	 	 $constraints = $this->GetArray("||SQLForeignKeys|||||$owner|$table");
 403  	 	 $ADODB_FETCH_MODE = $savem;
 404  
 405  	 	 $arr = false;
 406  	 	 foreach($constraints as $constr) {
 407  	 	 	 //print_r($constr);
 408  	 	 	 $arr[$constr[11]][$constr[2]][] = $constr[7].'='.$constr[3];
 409  	 	 }
 410  	 	 if (!$arr) {
 411  	 	 	 $false = false;
 412  	 	 	 return $false;
 413  	 	 }
 414  
 415  	 	 $arr2 = array();
 416  
 417  	 	 foreach($arr as $k => $v) {
 418  	 	 	 foreach($v as $a => $b) {
 419  	 	 	 	 if ($upper) $a = strtoupper($a);
 420  	 	 	 	 $arr2[$a] = $b;
 421  	 	 	 }
 422  	 	 }
 423  	 	 return $arr2;
 424  	 }
 425  
 426  	function BeginTrans()
 427  	 {
 428  	 	 if (!$this->hasTransactions) return false;
 429  	 	 if ($this->transOff) return true;
 430  	 	 $this->transCnt += 1;
 431  	 	 $this->autoCommit = false;
 432  	 	 if (defined('ODB_TXN_DEFAULT'))
 433  	 	 	 $txn = ODB_TXN_DEFAULT;
 434  	 	 else
 435  	 	 	 $txn = ODB_TXN_READUNCOMMITTED;
 436  	 	 $rs = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS,$txn,$this->_connectionID);
 437  	 	 if(!$rs) return false;
 438  	 	 return true;
 439  	 }
 440  
 441  	function CommitTrans($ok=true)
 442  	 {
 443  	 	 if ($this->transOff) return true;
 444  	 	 if (!$ok) return $this->RollbackTrans();
 445  	 	 if ($this->transCnt) $this->transCnt -= 1;
 446  	 	 $this->autoCommit = true;
 447  	 	 if( ($ret = @odbtp_commit($this->_connectionID)) )
 448  	 	 	 $ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);//set transaction off
 449  	 	 return $ret;
 450  	 }
 451  
 452  	function RollbackTrans()
 453  	 {
 454  	 	 if ($this->transOff) return true;
 455  	 	 if ($this->transCnt) $this->transCnt -= 1;
 456  	 	 $this->autoCommit = true;
 457  	 	 if( ($ret = @odbtp_rollback($this->_connectionID)) )
 458  	 	 	 $ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);//set transaction off
 459  	 	 return $ret;
 460  	 }
 461  
 462  	function SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
 463  	 {
 464  	 	 // TOP requires ORDER BY for Visual FoxPro
 465  	 	 if( $this->odbc_driver == ODB_DRIVER_FOXPRO ) {
 466  	 	 	 if (!preg_match('/ORDER[ \t\r\n]+BY/is',$sql)) $sql .= ' ORDER BY 1';
 467  	 	 }
 468  	 	 $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
 469  	 	 return $ret;
 470  	 }
 471  
 472  	function Prepare($sql)
 473  	 {
 474  	 	 if (! $this->_bindInputArray) return $sql; // no binding
 475  
 476          $this->_errorMsg = false;
 477  	 	 $this->_errorCode = false;
 478  
 479  	 	 $stmt = @odbtp_prepare($sql,$this->_connectionID);
 480  	 	 if (!$stmt) {
 481  	 	 //	 print "Prepare Error for ($sql) ".$this->ErrorMsg()."<br>";
 482  	 	 	 return $sql;
 483  	 	 }
 484  	 	 return array($sql,$stmt,false);
 485  	 }
 486  
 487  	function PrepareSP($sql, $param = true)
 488  	 {
 489  	 	 if (!$this->_canPrepareSP) return $sql; // Can't prepare procedures
 490  
 491          $this->_errorMsg = false;
 492  	 	 $this->_errorCode = false;
 493  
 494  	 	 $stmt = @odbtp_prepare_proc($sql,$this->_connectionID);
 495  	 	 if (!$stmt) return false;
 496  	 	 return array($sql,$stmt);
 497  	 }
 498  
 499  	 /*
 500  	 Usage:
 501  	 	 $stmt = $db->PrepareSP('SP_RUNSOMETHING'); -- takes 2 params, @myid and @group
 502  
 503  	 	 # note that the parameter does not have @ in front!
 504  	 	 $db->Parameter($stmt,$id,'myid');
 505  	 	 $db->Parameter($stmt,$group,'group',false,64);
 506  	 	 $db->Parameter($stmt,$group,'photo',false,100000,ODB_BINARY);
 507  	 	 $db->Execute($stmt);
 508  
 509  	 	 @param $stmt Statement returned by Prepare() or PrepareSP().
 510  	 	 @param $var PHP variable to bind to. Can set to null (for isNull support).
 511  	 	 @param $name Name of stored procedure variable name to bind to.
 512  	 	 @param [$isOutput] Indicates direction of parameter 0/false=IN  1=OUT  2= IN/OUT. This is ignored in odbtp.
 513  	 	 @param [$maxLen] Holds an maximum length of the variable.
 514  	 	 @param [$type] The data type of $var. Legal values depend on driver.
 515  
 516  	 	 See odbtp_attach_param documentation at http://odbtp.sourceforge.net.
 517  	 */
 518  	function Parameter(&$stmt, &$var, $name, $isOutput=false, $maxLen=0, $type=0)
 519  	 {
 520  	 	 if ( $this->odbc_driver == ODB_DRIVER_JET ) {
 521  	 	 	 $name = '['.$name.']';
 522  	 	 	 if( !$type && $this->_useUnicodeSQL
 523  	 	 	 	 && @odbtp_param_bindtype($stmt[1], $name) == ODB_CHAR )
 524  	 	 	 {
 525  	 	 	 	 $type = ODB_WCHAR;
 526  	 	 	 }
 527  	 	 }
 528  	 	 else {
 529  	 	 	 $name = '@'.$name;
 530  	 	 }
 531  	 	 return @odbtp_attach_param($stmt[1], $name, $var, $type, $maxLen);
 532  	 }
 533  
 534  	 /*
 535  	 	 Insert a null into the blob field of the table first.
 536  	 	 Then use UpdateBlob to store the blob.
 537  
 538  	 	 Usage:
 539  
 540  	 	 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 541  	 	 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 542  	 */
 543  
 544  	function UpdateBlob($table,$column,$val,$where,$blobtype='image')
 545  	 {
 546  	 	 $sql = "UPDATE $table SET $column = ? WHERE $where";
 547  	 	 if( !($stmt = @odbtp_prepare($sql, $this->_connectionID)) )
 548  	 	 	 return false;
 549  	 	 if( !@odbtp_input( $stmt, 1, ODB_BINARY, 1000000, $blobtype ) )
 550  	 	 	 return false;
 551  	 	 if( !@odbtp_set( $stmt, 1, $val ) )
 552  	 	 	 return false;
 553  	 	 return @odbtp_execute( $stmt ) != false;
 554  	 }
 555  
 556  	function MetaIndexes($table,$primary=false, $owner=false)
 557  	 {
 558  	 	 switch ( $this->odbc_driver) {
 559  	 	 	 case ODB_DRIVER_MSSQL:
 560  	 	 	 	 return $this->MetaIndexes_mssql($table, $primary);
 561  	 	 	 default:
 562  	 	 	 	 return array();
 563  	 	 }
 564  	 }
 565  
 566  	function MetaIndexes_mssql($table,$primary=false, $owner = false)
 567  	 {
 568  	 	 $table = strtolower($this->qstr($table));
 569  
 570  	 	 $sql = "SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno,
 571  	 	 	 CASE WHEN I.indid BETWEEN 1 AND 254 AND (I.status & 2048 = 2048 OR I.Status = 16402 AND O.XType = 'V') THEN 1 ELSE 0 END AS IsPK,
 572  	 	 	 CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique
 573  	 	 	 FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id
 574  	 	 	 INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid
 575  	 	 	 INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid
 576  	 	 	 WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND lower(O.Name) = $table
 577  	 	 	 ORDER BY O.name, I.Name, K.keyno";
 578  
 579  	 	 global $ADODB_FETCH_MODE;
 580  	 	 $save = $ADODB_FETCH_MODE;
 581          $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 582          if ($this->fetchMode !== FALSE) {
 583          	 $savem = $this->SetFetchMode(FALSE);
 584          }
 585  
 586          $rs = $this->Execute($sql);
 587          if (isset($savem)) {
 588          	 $this->SetFetchMode($savem);
 589          }
 590          $ADODB_FETCH_MODE = $save;
 591  
 592          if (!is_object($rs)) {
 593          	 return FALSE;
 594          }
 595  
 596  	 	 $indexes = array();
 597  	 	 while ($row = $rs->FetchRow()) {
 598  	 	 	 if ($primary && !$row[5]) continue;
 599  
 600              $indexes[$row[0]]['unique'] = $row[6];
 601              $indexes[$row[0]]['columns'][] = $row[1];
 602      	 }
 603          return $indexes;
 604  	 }
 605  
 606  	function IfNull( $field, $ifNull )
 607  	 {
 608  	 	 switch( $this->odbc_driver ) {
 609  	 	 	 case ODB_DRIVER_MSSQL:
 610  	 	 	 	 return " ISNULL($field, $ifNull) ";
 611  	 	 	 case ODB_DRIVER_JET:
 612  	 	 	 	 return " IIF(IsNull($field), $ifNull, $field) ";
 613  	 	 }
 614  	 	 return " CASE WHEN $field is null THEN $ifNull ELSE $field END ";
 615  	 }
 616  
 617  	function _query($sql,$inputarr=false)
 618  	 {
 619  	 	 $last_php_error = $this->resetLastError();
 620  	 	 $this->_errorMsg = false;
 621  	 	 $this->_errorCode = false;
 622  
 623   	 	 if ($inputarr) {
 624  	 	 	 if (is_array($sql)) {
 625  	 	 	 	 $stmtid = $sql[1];
 626  	 	 	 } else {
 627  	 	 	 	 $stmtid = @odbtp_prepare($sql,$this->_connectionID);
 628  	 	 	 	 if ($stmtid == false) {
 629  	 	 	 	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
 630  	 	 	 	 	 return false;
 631  	 	 	 	 }
 632  	 	 	 }
 633  	 	 	 $num_params = @odbtp_num_params( $stmtid );
 634  	 	 	 /*
 635  	 	 	 for( $param = 1; $param <= $num_params; $param++ ) {
 636  	 	 	 	 @odbtp_input( $stmtid, $param );
 637  	 	 	 	 @odbtp_set( $stmtid, $param, $inputarr[$param-1] );
 638  	 	 	 }*/
 639  
 640  	 	 	 $param = 1;
 641  	 	 	 foreach($inputarr as $v) {
 642  	 	 	 	 @odbtp_input( $stmtid, $param );
 643  	 	 	 	 @odbtp_set( $stmtid, $param, $v );
 644  	 	 	 	 $param += 1;
 645  	 	 	 	 if ($param > $num_params) break;
 646  	 	 	 }
 647  
 648  	 	 	 if (!@odbtp_execute($stmtid) ) {
 649  	 	 	 	 return false;
 650  	 	 	 }
 651  	 	 } else if (is_array($sql)) {
 652  	 	 	 $stmtid = $sql[1];
 653  	 	 	 if (!@odbtp_execute($stmtid)) {
 654  	 	 	 	 return false;
 655  	 	 	 }
 656  	 	 } else {
 657  	 	 	 $stmtid = odbtp_query($sql,$this->_connectionID);
 658     	 	 }
 659  	 	 $this->_lastAffectedRows = 0;
 660  	 	 if ($stmtid) {
 661  	 	 	 	 $this->_lastAffectedRows = @odbtp_affected_rows($stmtid);
 662  	 	 }
 663          return $stmtid;
 664  	 }
 665  
 666  	function _close()
 667  	 {
 668  	 	 $ret = @odbtp_close($this->_connectionID);
 669  	 	 $this->_connectionID = false;
 670  	 	 return $ret;
 671  	 }
 672  }
 673  
 674  class ADORecordSet_odbtp extends ADORecordSet {
 675  
 676  	 var $databaseType = 'odbtp';
 677  	 var $canSeek = true;
 678  
 679  	function __construct($queryID,$mode=false)
 680  	 {
 681  	 	 if ($mode === false) {
 682  	 	 	 global $ADODB_FETCH_MODE;
 683  	 	 	 $mode = $ADODB_FETCH_MODE;
 684  	 	 }
 685  	 	 $this->fetchMode = $mode;
 686  	 	 parent::__construct($queryID);
 687  	 }
 688  
 689  	function _initrs()
 690  	 {
 691  	 	 $this->_numOfFields = @odbtp_num_fields($this->_queryID);
 692  	 	 if (!($this->_numOfRows = @odbtp_num_rows($this->_queryID)))
 693  	 	 	 $this->_numOfRows = -1;
 694  
 695  	 	 if (!$this->connection->_useUnicodeSQL) return;
 696  
 697  	 	 if ($this->connection->odbc_driver == ODB_DRIVER_JET) {
 698  	 	 	 if (!@odbtp_get_attr(ODB_ATTR_MAPCHARTOWCHAR,
 699  	 	 	                      $this->connection->_connectionID))
 700  	 	 	 {
 701  	 	 	 	 for ($f = 0; $f < $this->_numOfFields; $f++) {
 702  	 	 	 	 	 if (@odbtp_field_bindtype($this->_queryID, $f) == ODB_CHAR)
 703  	 	 	 	 	 	 @odbtp_bind_field($this->_queryID, $f, ODB_WCHAR);
 704  	 	 	 	 }
 705  	 	 	 }
 706  	 	 }
 707  	 }
 708  
 709  	function FetchField($fieldOffset = 0)
 710  	 {
 711  	 	 $off=$fieldOffset; // offsets begin at 0
 712  	 	 $o= new ADOFieldObject();
 713  	 	 $o->name = @odbtp_field_name($this->_queryID,$off);
 714  	 	 $o->type = @odbtp_field_type($this->_queryID,$off);
 715          $o->max_length = @odbtp_field_length($this->_queryID,$off);
 716  	 	 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
 717  	 	 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
 718  	 	 return $o;
 719  	 }
 720  
 721  	function _seek($row)
 722  	 {
 723  	 	 return @odbtp_data_seek($this->_queryID, $row);
 724  	 }
 725  
 726  	function fields($colname)
 727  	 {
 728  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
 729  
 730  	 	 if (!$this->bind) {
 731  	 	 	 $this->bind = array();
 732  	 	 	 for ($i=0; $i < $this->_numOfFields; $i++) {
 733  	 	 	 	 $name = @odbtp_field_name( $this->_queryID, $i );
 734  	 	 	 	 $this->bind[strtoupper($name)] = $i;
 735  	 	 	 }
 736  	 	 }
 737  	 	 return $this->fields[$this->bind[strtoupper($colname)]];
 738  	 }
 739  
 740  	function _fetch_odbtp($type=0)
 741  	 {
 742  	 	 switch ($this->fetchMode) {
 743  	 	 	 case ADODB_FETCH_NUM:
 744  	 	 	 	 $this->fields = @odbtp_fetch_row($this->_queryID, $type);
 745  	 	 	 	 break;
 746  	 	 	 case ADODB_FETCH_ASSOC:
 747  	 	 	 	 $this->fields = @odbtp_fetch_assoc($this->_queryID, $type);
 748  	 	 	 	 break;
 749              default:
 750  	 	 	 	 $this->fields = @odbtp_fetch_array($this->_queryID, $type);
 751  	 	 }
 752  	 	 if ($this->databaseType = 'odbtp_vfp') {
 753  	 	 	 if ($this->fields)
 754  	 	 	 foreach($this->fields as $k => $v) {
 755  	 	 	 	 if (strncmp($v,'1899-12-30',10) == 0) $this->fields[$k] = '';
 756  	 	 	 }
 757  	 	 }
 758  	 	 return is_array($this->fields);
 759  	 }
 760  
 761  	function _fetch()
 762  	 {
 763  	 	 return $this->_fetch_odbtp();
 764  	 }
 765  
 766  	function MoveFirst()
 767  	 {
 768  	 	 if (!$this->_fetch_odbtp(ODB_FETCH_FIRST)) return false;
 769  	 	 $this->EOF = false;
 770  	 	 $this->_currentRow = 0;
 771  	 	 return true;
 772      }
 773  
 774  	function MoveLast()
 775  	 {
 776  	 	 if (!$this->_fetch_odbtp(ODB_FETCH_LAST)) return false;
 777  	 	 $this->EOF = false;
 778  	 	 $this->_currentRow = $this->_numOfRows - 1;
 779  	 	 return true;
 780  	 }
 781  
 782  	function NextRecordSet()
 783  	 {
 784  	 	 if (!@odbtp_next_result($this->_queryID)) return false;
 785  	 	 $this->_inited = false;
 786  	 	 $this->bind = false;
 787  	 	 $this->_currentRow = -1;
 788  	 	 $this->Init();
 789  	 	 return true;
 790  	 }
 791  
 792  	function _close()
 793  	 {
 794  	 	 return @odbtp_free_query($this->_queryID);
 795  	 }
 796  }
 797  
 798  class ADORecordSet_odbtp_mssql extends ADORecordSet_odbtp {
 799  
 800  	 var $databaseType = 'odbtp_mssql';
 801  
 802  }
 803  
 804  class ADORecordSet_odbtp_access extends ADORecordSet_odbtp {
 805  
 806  	 var $databaseType = 'odbtp_access';
 807  
 808  }
 809  
 810  class ADORecordSet_odbtp_vfp extends ADORecordSet_odbtp {
 811  
 812  	 var $databaseType = 'odbtp_vfp';
 813  
 814  }
 815  
 816  class ADORecordSet_odbtp_oci8 extends ADORecordSet_odbtp {
 817  
 818  	 var $databaseType = 'odbtp_oci8';
 819  
 820  }
 821  
 822  class ADORecordSet_odbtp_sybase extends ADORecordSet_odbtp {
 823  
 824  	 var $databaseType = 'odbtp_sybase';
 825  
 826  }