PHPOpenChat PHP Cross Reference Customer Relationship Management

Source: /include/adodb/drivers/adodb-ado.inc.php - 629 lines - 15966 bytes - Summary - Text - Print

   1  <?php
   2  /* 

   3  V4.61 24 Feb 2005  (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.

   4    Released under both BSD license and Lesser GPL library license. 

   5    Whenever there is any discrepancy between the two licenses, 

   6    the BSD license will take precedence. 

   7  Set tabs to 4 for best viewing.

   8    

   9    Latest version is available at http://adodb.sourceforge.net

  10    

  11      Microsoft ADO data driver. Requires ADO. Works only on MS Windows.

  12  */
  13  
  14  // security - hide paths

  15  if (!defined('ADODB_DIR')) die();
  16      
  17  define("_ADODB_ADO_LAYER", 1 );
  18  /*--------------------------------------------------------------------------------------

  19  --------------------------------------------------------------------------------------*/
  20  
  21      
  22  class ADODB_ado extends ADOConnection {
  23      var $databaseType = "ado";    
  24      var $_bindInputArray = false;
  25      var $fmtDate = "'Y-m-d'";
  26      var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  27      var $replaceQuote = "''"; // string to use to replace quotes

  28      var $dataProvider = "ado";    
  29      var $hasAffectedRows = true;
  30      var $adoParameterType = 201; // 201 = long varchar, 203=long wide varchar, 205 = long varbinary

  31      var $_affectedRows = false;
  32      var $_thisTransactions;
  33      var $_cursor_type = 3; // 3=adOpenStatic,0=adOpenForwardOnly,1=adOpenKeyset,2=adOpenDynamic

  34      var $_cursor_location = 3; // 2=adUseServer, 3 = adUseClient;

  35      var $_lock_type = -1;
  36      var $_execute_option = -1;
  37      var $poorAffectedRows = true; 
  38      var $charPage;
  39          
  40  	function ADODB_ado() 
  41      {     
  42          $this->_affectedRows = new VARIANT;
  43      }
  44  
  45  	function ServerInfo()
  46      {
  47          if (!empty($this->_connectionID)) $desc = $this->_connectionID->provider;
  48          return array('description' => $desc, 'version' => '');
  49      }
  50      
  51  	function _affectedrows()
  52      {
  53          if (PHP_VERSION >= 5) return $this->_affectedRows;
  54          
  55          return $this->_affectedRows->value;
  56      }
  57      
  58      // you can also pass a connection string like this:

  59      //

  60      // $DB->Connect('USER ID=sa;PASSWORD=pwd;SERVER=mangrove;DATABASE=ai',false,false,'SQLOLEDB');

  61  	function _connect($argHostname, $argUsername, $argPassword, $argProvider= 'MSDASQL')
  62      {
  63          $u = 'UID';
  64          $p = 'PWD';
  65      
  66          if (!empty($this->charPage))
  67              $dbc = new COM('ADODB.Connection',null,$this->charPage);
  68          else
  69              $dbc = new COM('ADODB.Connection');
  70              
  71          if (! $dbc) return false;
  72  
  73          /* special support if provider is mssql or access */

  74          if ($argProvider=='mssql') {
  75              $u = 'User Id';  //User parameter name for OLEDB

  76              $p = 'Password'; 
  77              $argProvider = "SQLOLEDB"; // SQL Server Provider

  78              
  79              // not yet

  80              //if ($argDatabasename) $argHostname .= ";Initial Catalog=$argDatabasename";

  81              
  82              //use trusted conection for SQL if username not specified

  83              if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
  84          } else if ($argProvider=='access')
  85              $argProvider = "Microsoft.Jet.OLEDB.4.0"; // Microsoft Jet Provider

  86          
  87          if ($argProvider) $dbc->Provider = $argProvider;    
  88          
  89          if ($argUsername) $argHostname .= ";$u=$argUsername";
  90          if ($argPassword)$argHostname .= ";$p=$argPassword";
  91          
  92          if ($this->debug) ADOConnection::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
  93          // @ added below for php 4.0.1 and earlier

  94          @$dbc->Open((string) $argHostname);
  95          
  96          $this->_connectionID = $dbc;
  97          
  98          $dbc->CursorLocation = $this->_cursor_location;
  99          return  $dbc->State > 0;
 100      }
 101      
 102      // returns true or false

 103  	function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
 104      {
 105          return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
 106      }    
 107      
 108  /*

 109      adSchemaCatalogs    = 1,

 110      adSchemaCharacterSets    = 2,

 111      adSchemaCollations    = 3,

 112      adSchemaColumns    = 4,

 113      adSchemaCheckConstraints    = 5,

 114      adSchemaConstraintColumnUsage    = 6,

 115      adSchemaConstraintTableUsage    = 7,

 116      adSchemaKeyColumnUsage    = 8,

 117      adSchemaReferentialContraints    = 9,

 118      adSchemaTableConstraints    = 10,

 119      adSchemaColumnsDomainUsage    = 11,

 120      adSchemaIndexes    = 12,

 121      adSchemaColumnPrivileges    = 13,

 122      adSchemaTablePrivileges    = 14,

 123      adSchemaUsagePrivileges    = 15,

 124      adSchemaProcedures    = 16,

 125      adSchemaSchemata    = 17,

 126      adSchemaSQLLanguages    = 18,

 127      adSchemaStatistics    = 19,

 128      adSchemaTables    = 20,

 129      adSchemaTranslations    = 21,

 130      adSchemaProviderTypes    = 22,

 131      adSchemaViews    = 23,

 132      adSchemaViewColumnUsage    = 24,

 133      adSchemaViewTableUsage    = 25,

 134      adSchemaProcedureParameters    = 26,

 135      adSchemaForeignKeys    = 27,

 136      adSchemaPrimaryKeys    = 28,

 137      adSchemaProcedureColumns    = 29,

 138      adSchemaDBInfoKeywords    = 30,

 139      adSchemaDBInfoLiterals    = 31,

 140      adSchemaCubes    = 32,

 141      adSchemaDimensions    = 33,

 142      adSchemaHierarchies    = 34,

 143      adSchemaLevels    = 35,

 144      adSchemaMeasures    = 36,

 145      adSchemaProperties    = 37,

 146      adSchemaMembers    = 38

 147  

 148  */
 149      
 150      function &MetaTables()
 151      {
 152          $arr= array();
 153          $dbc = $this->_connectionID;
 154          
 155          $adors=@$dbc->OpenSchema(20);//tables

 156          if ($adors){
 157              $f = $adors->Fields(2);//table/view name

 158              $t = $adors->Fields(3);//table type

 159              while (!$adors->EOF){
 160                  $tt=substr($t->value,0,6);
 161                  if ($tt!='SYSTEM' && $tt !='ACCESS')
 162                      $arr[]=$f->value;
 163                  //print $f->value . ' ' . $t->value.'<br>';

 164                  $adors->MoveNext();
 165              }
 166              $adors->Close();
 167          }
 168          
 169          return $arr;
 170      }
 171      
 172      function &MetaColumns($table)
 173      {
 174          $table = strtoupper($table);
 175          $arr = array();
 176          $dbc = $this->_connectionID;
 177          
 178          $adors=@$dbc->OpenSchema(4);//tables

 179      
 180          if ($adors){
 181              $t = $adors->Fields(2);//table/view name

 182              while (!$adors->EOF){
 183                  
 184                  
 185                  if (strtoupper($t->Value) == $table) {
 186                  
 187                      $fld = new ADOFieldObject();
 188                      $c = $adors->Fields(3);
 189                      $fld->name = $c->Value;
 190                      $fld->type = 'CHAR'; // cannot discover type in ADO!

 191                      $fld->max_length = -1;
 192                      $arr[strtoupper($fld->name)]=$fld;
 193                  }
 194          
 195                  $adors->MoveNext();
 196              }
 197              $adors->Close();
 198          }
 199          $false = false;
 200          return empty($arr) ? $false : $arr;
 201      }
 202      
 203  
 204  
 205      
 206      /* returns queryID or false */

 207      function &_query($sql,$inputarr=false) 
 208      {
 209          
 210          $dbc = $this->_connectionID;
 211          $false = false;
 212          
 213      //    return rs    

 214          if ($inputarr) {
 215              
 216              if (!empty($this->charPage))
 217                  $oCmd = new COM('ADODB.Command',null,$this->charPage);
 218              else
 219                  $oCmd = new COM('ADODB.Command');
 220              $oCmd->ActiveConnection = $dbc;
 221              $oCmd->CommandText = $sql;
 222              $oCmd->CommandType = 1;
 223  
 224              foreach($inputarr as $val) {
 225                  // name, type, direction 1 = input, len,

 226                  $this->adoParameterType = 130;
 227                  $p = $oCmd->CreateParameter('name',$this->adoParameterType,1,strlen($val),$val);
 228                  //print $p->Type.' '.$p->value;

 229                  $oCmd->Parameters->Append($p);
 230              }
 231              $p = false;
 232              $rs = $oCmd->Execute();
 233              $e = $dbc->Errors;
 234              if ($dbc->Errors->Count > 0) return $false;
 235              return $rs;
 236          }
 237          
 238          $rs = @$dbc->Execute($sql,$this->_affectedRows, $this->_execute_option);
 239  
 240          if ($dbc->Errors->Count > 0) return $false;
 241          if (! $rs) return $false;
 242          
 243          if ($rs->State == 0) {
 244              $true = true;
 245              return $true; // 0 = adStateClosed means no records returned

 246          }
 247          return $rs;
 248      }
 249  
 250      
 251  	function BeginTrans() 
 252      { 
 253          if ($this->transOff) return true;
 254          
 255          if (isset($this->_thisTransactions))
 256              if (!$this->_thisTransactions) return false;
 257          else {
 258              $o = $this->_connectionID->Properties("Transaction DDL");
 259              $this->_thisTransactions = $o ? true : false;
 260              if (!$o) return false;
 261          }
 262          @$this->_connectionID->BeginTrans();
 263          $this->transCnt += 1;
 264          return true;
 265      }
 266  	function CommitTrans($ok=true) 
 267      { 
 268          if (!$ok) return $this->RollbackTrans();
 269          if ($this->transOff) return true;
 270          
 271          @$this->_connectionID->CommitTrans();
 272          if ($this->transCnt) @$this->transCnt -= 1;
 273          return true;
 274      }
 275  	function RollbackTrans() {
 276          if ($this->transOff) return true;
 277          @$this->_connectionID->RollbackTrans();
 278          if ($this->transCnt) @$this->transCnt -= 1;
 279          return true;
 280      }
 281      
 282      /*    Returns: the last error message from previous database operation    */    

 283  
 284  	function ErrorMsg() 
 285      {
 286          $errc = $this->_connectionID->Errors;
 287          if ($errc->Count == 0) return '';
 288          $err = $errc->Item($errc->Count-1);
 289          return $err->Description;
 290      }
 291      
 292  	function ErrorNo() 
 293      {
 294          $errc = $this->_connectionID->Errors;
 295          if ($errc->Count == 0) return 0;
 296          $err = $errc->Item($errc->Count-1);
 297          return $err->NativeError;
 298      }
 299  
 300      // returns true or false

 301  	function _close()
 302      {
 303          if ($this->_connectionID) $this->_connectionID->Close();
 304          $this->_connectionID = false;
 305          return true;
 306      }
 307      
 308      
 309  }
 310      
 311  /*--------------------------------------------------------------------------------------

 312       Class Name: Recordset

 313  --------------------------------------------------------------------------------------*/
 314  
 315  class ADORecordSet_ado extends ADORecordSet {    
 316      
 317      var $bind = false;
 318      var $databaseType = "ado";    
 319      var $dataProvider = "ado";    
 320      var $_tarr = false; // caches the types

 321      var $_flds; // and field objects

 322      var $canSeek = true;
 323        var $hideErrors = true;
 324            
 325  	function ADORecordSet_ado($id,$mode=false)
 326      {
 327          if ($mode === false) { 
 328              global $ADODB_FETCH_MODE;
 329              $mode = $ADODB_FETCH_MODE;
 330          }
 331          $this->fetchMode = $mode;
 332          return $this->ADORecordSet($id,$mode);
 333      }
 334  
 335  
 336      // returns the field object

 337  	function FetchField($fieldOffset = -1) {
 338          $off=$fieldOffset+1; // offsets begin at 1

 339          
 340          $o= new ADOFieldObject();
 341          $rs = $this->_queryID;
 342          $f = $rs->Fields($fieldOffset);
 343          $o->name = $f->Name;
 344          $t = $f->Type;
 345          $o->type = $this->MetaType($t);
 346          $o->max_length = $f->DefinedSize;
 347          $o->ado_type = $t;
 348          
 349  
 350          //print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";

 351          return $o;
 352      }
 353      
 354      /* Use associative array to get fields array */

 355  	function Fields($colname)
 356      {
 357          if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
 358          if (!$this->bind) {
 359              $this->bind = array();
 360              for ($i=0; $i < $this->_numOfFields; $i++) {
 361                  $o = $this->FetchField($i);
 362                  $this->bind[strtoupper($o->name)] = $i;
 363              }
 364          }
 365          
 366           return $this->fields[$this->bind[strtoupper($colname)]];
 367      }
 368  
 369          
 370  	function _initrs()
 371      {
 372          $rs = $this->_queryID;
 373          $this->_numOfRows = $rs->RecordCount;
 374          
 375          $f = $rs->Fields;
 376          $this->_numOfFields = $f->Count;
 377      }
 378      
 379      
 380       // should only be used to move forward as we normally use forward-only cursors

 381  	function _seek($row)
 382      {
 383         $rs = $this->_queryID; 
 384          // absoluteposition doesn't work -- my maths is wrong ?

 385          //    $rs->AbsolutePosition->$row-2;

 386          //    return true;

 387          if ($this->_currentRow > $row) return false;
 388          @$rs->Move((integer)$row - $this->_currentRow-1); //adBookmarkFirst

 389          return true;
 390      }
 391      
 392  /*

 393      OLEDB types

 394      

 395       enum DBTYPEENUM

 396      {    DBTYPE_EMPTY    = 0,

 397      DBTYPE_NULL    = 1,

 398      DBTYPE_I2    = 2,

 399      DBTYPE_I4    = 3,

 400      DBTYPE_R4    = 4,

 401      DBTYPE_R8    = 5,

 402      DBTYPE_CY    = 6,

 403      DBTYPE_DATE    = 7,

 404      DBTYPE_BSTR    = 8,

 405      DBTYPE_IDISPATCH    = 9,

 406      DBTYPE_ERROR    = 10,

 407      DBTYPE_BOOL    = 11,

 408      DBTYPE_VARIANT    = 12,

 409      DBTYPE_IUNKNOWN    = 13,

 410      DBTYPE_DECIMAL    = 14,

 411      DBTYPE_UI1    = 17,

 412      DBTYPE_ARRAY    = 0x2000,

 413      DBTYPE_BYREF    = 0x4000,

 414      DBTYPE_I1    = 16,

 415      DBTYPE_UI2    = 18,

 416      DBTYPE_UI4    = 19,

 417      DBTYPE_I8    = 20,

 418      DBTYPE_UI8    = 21,

 419      DBTYPE_GUID    = 72,

 420      DBTYPE_VECTOR    = 0x1000,

 421      DBTYPE_RESERVED    = 0x8000,

 422      DBTYPE_BYTES    = 128,

 423      DBTYPE_STR    = 129,

 424      DBTYPE_WSTR    = 130,

 425      DBTYPE_NUMERIC    = 131,

 426      DBTYPE_UDT    = 132,

 427      DBTYPE_DBDATE    = 133,

 428      DBTYPE_DBTIME    = 134,

 429      DBTYPE_DBTIMESTAMP    = 135

 430      

 431      ADO Types

 432      

 433         adEmpty    = 0,

 434      adTinyInt    = 16,

 435      adSmallInt    = 2,

 436      adInteger    = 3,

 437      adBigInt    = 20,

 438      adUnsignedTinyInt    = 17,

 439      adUnsignedSmallInt    = 18,

 440      adUnsignedInt    = 19,

 441      adUnsignedBigInt    = 21,

 442      adSingle    = 4,

 443      adDouble    = 5,

 444      adCurrency    = 6,

 445      adDecimal    = 14,

 446      adNumeric    = 131,

 447      adBoolean    = 11,

 448      adError    = 10,

 449      adUserDefined    = 132,

 450      adVariant    = 12,

 451      adIDispatch    = 9,

 452      adIUnknown    = 13,    

 453      adGUID    = 72,

 454      adDate    = 7,

 455      adDBDate    = 133,

 456      adDBTime    = 134,

 457      adDBTimeStamp    = 135,

 458      adBSTR    = 8,

 459      adChar    = 129,

 460      adVarChar    = 200,

 461      adLongVarChar    = 201,

 462      adWChar    = 130,

 463      adVarWChar    = 202,

 464      adLongVarWChar    = 203,

 465      adBinary    = 128,

 466      adVarBinary    = 204,

 467      adLongVarBinary    = 205,

 468      adChapter    = 136,

 469      adFileTime    = 64,

 470      adDBFileTime    = 137,

 471      adPropVariant    = 138,

 472      adVarNumeric    = 139

 473  */
 474  	function MetaType($t,$len=-1,$fieldobj=false)
 475      {
 476          if (is_object($t)) {
 477              $fieldobj = $t;
 478              $t = $fieldobj->type;
 479              $len = $fieldobj->max_length;
 480          }
 481          
 482          if (!is_numeric($t)) return $t;
 483          
 484          switch ($t) {
 485          case 0:
 486          case 12: // variant
 487          case 8: // bstr
 488          case 129: //char
 489          case 130: //wc
 490          case 200: // varc
 491          case 202:// varWC
 492          case 128: // bin
 493          case 204: // varBin
 494          case 72: // guid
 495              if ($len <= $this->blobSize) return 'C';
 496          
 497          case 201:
 498          case 203:
 499              return 'X';
 500          case 128:
 501          case 204:
 502          case 205:
 503               return 'B';
 504          case 7:
 505          case 133: return 'D';
 506          
 507          case 134:
 508          case 135: return 'T';
 509          
 510          case 11: return 'L';
 511          
 512          case 16://    adTinyInt    = 16,
 513          case 2://adSmallInt    = 2,
 514          case 3://adInteger    = 3,
 515          case 4://adBigInt    = 20,
 516          case 17://adUnsignedTinyInt    = 17,
 517          case 18://adUnsignedSmallInt    = 18,
 518          case 19://adUnsignedInt    = 19,
 519          case 20://adUnsignedBigInt    = 21,
 520              return 'I';
 521          default: return 'N';
 522          }
 523      }
 524      
 525      // time stamp not supported yet

 526  	function _fetch()
 527      {    
 528          $rs = $this->_queryID;
 529          if (!$rs or $rs->EOF) {
 530              $this->fields = false;
 531              return false;
 532          }
 533          $this->fields = array();
 534      
 535          if (!$this->_tarr) {
 536              $tarr = array();
 537              $flds = array();
 538              for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
 539                  $f = $rs->Fields($i);
 540                  $flds[] = $f;
 541                  $tarr[] = $f->Type;
 542              }
 543              // bind types and flds only once

 544              $this->_tarr = $tarr; 
 545              $this->_flds = $flds;
 546          }
 547          $t = reset($this->_tarr);
 548          $f = reset($this->_flds);
 549          
 550          if ($this->hideErrors)  $olde = error_reporting(E_ERROR|E_CORE_ERROR);// sometimes $f->value be null

 551          for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
 552              //echo "<p>",$t,' ';var_dump($f->value); echo '</p>';

 553              switch($t) {
 554              case 135: // timestamp
 555                  if (!strlen((string)$f->value)) $this->fields[] = false;
 556                  else {
 557                      if (!is_numeric($f->value)) $val = variant_date_to_timestamp($f->value);
 558                      else $val = $f->value;
 559                      $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
 560                  }
 561                  break;            
 562              case 133:// A date value (yyyymmdd) 
 563                  if ($val = $f->value) {
 564                      $this->fields[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
 565                  } else
 566                      $this->fields[] = false;
 567                  break;
 568              case 7: // adDate
 569                  if (!strlen((string)$f->value)) $this->fields[] = false;
 570                  else {
 571                      if (!is_numeric($f->value)) $val = variant_date_to_timestamp($f->value);
 572                      else $val = $f->value;
 573                      
 574                      if (($val % 86400) == 0) $this->fields[] = adodb_date('Y-m-d',$val);
 575                      else $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
 576                  }
 577                  break;
 578              case 1: // null
 579                  $this->fields[] = false;
 580                  break;
 581              case 6: // currency is not supported properly;
 582                  ADOConnection::outp( '<b>'.$f->Name.': currency type not supported by PHP</b>');
 583                  $this->fields[] = (float) $f->value;
 584                  break;
 585              default:
 586                  $this->fields[] = $f->value; 
 587                  break;
 588              }
 589              //print " $f->value $t, ";

 590              $f = next($this->_flds);
 591              $t = next($this->_tarr);
 592          } // for

 593          if ($this->hideErrors) error_reporting($olde);
 594          @$rs->MoveNext(); // @ needed for some versions of PHP!

 595          
 596          if ($this->fetchMode & ADODB_FETCH_ASSOC) {
 597              $this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
 598          }
 599          return true;
 600      }
 601      
 602  		function NextRecordSet()
 603          {
 604              $rs = $this->_queryID;
 605              $this->_queryID = $rs->NextRecordSet();
 606              //$this->_queryID = $this->_QueryId->NextRecordSet();

 607              if ($this->_queryID == null) return false;
 608              
 609              $this->_currentRow = -1;
 610              $this->_currentPage = -1;
 611              $this->bind = false;
 612              $this->fields = false;
 613              $this->_flds = false;
 614              $this->_tarr = false;
 615              
 616              $this->_inited = false;
 617              $this->Init();
 618              return true;
 619          }
 620  
 621  	function _close() {
 622          $this->_flds = false;
 623          @$this->_queryID->Close();// by Pete Dishman (peterd@telephonetics.co.uk)

 624          $this->_queryID = false;    
 625      }
 626  
 627  }
 628  
 629  ?>

title

Description

title

Description

title

Description

title

title

Body