Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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