00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 if (!defined('ADODB_DIR')) die();
00016
00017 define("_ADODB_ADO_LAYER", 1 );
00018
00019
00020
00021
00022 class ADODB_ado extends ADOConnection {
00023 var $databaseType = "ado";
00024 var $_bindInputArray = false;
00025 var $fmtDate = "'Y-m-d'";
00026 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
00027 var $replaceQuote = "''";
00028 var $dataProvider = "ado";
00029 var $hasAffectedRows = true;
00030 var $adoParameterType = 201;
00031 var $_affectedRows = false;
00032 var $_thisTransactions;
00033 var $_cursor_type = 3;
00034 var $_cursor_location = 3;
00035 var $_lock_type = -1;
00036 var $_execute_option = -1;
00037 var $poorAffectedRows = true;
00038 var $charPage;
00039
00040 function ADODB_ado()
00041 {
00042 $this->_affectedRows = new VARIANT;
00043 }
00044
00045 function ServerInfo()
00046 {
00047 if (!empty($this->_connectionID)) $desc = $this->_connectionID->provider;
00048 return array('description' => $desc, 'version' => '');
00049 }
00050
00051 function _affectedrows()
00052 {
00053 if (PHP_VERSION >= 5) return $this->_affectedRows;
00054
00055 return $this->_affectedRows->value;
00056 }
00057
00058
00059
00060
00061 function _connect($argHostname, $argUsername, $argPassword,$argDBorProvider, $argProvider= '')
00062 {
00063
00064
00065
00066
00067
00068 if ($argProvider) {
00069 $argDatabasename = $argDBorProvider;
00070 } else {
00071 $argDatabasename = '';
00072 if ($argDBorProvider) $argProvider = $argDBorProvider;
00073 else $argProvider = 'MSDASQL';
00074 }
00075
00076
00077 try {
00078 $u = 'UID';
00079 $p = 'PWD';
00080
00081 if (!empty($this->charPage))
00082 $dbc = new COM('ADODB.Connection',null,$this->charPage);
00083 else
00084 $dbc = new COM('ADODB.Connection');
00085
00086 if (! $dbc) return false;
00087
00088
00089 if ($argProvider=='mssql') {
00090 $u = 'User Id';
00091 $p = 'Password';
00092 $argProvider = "SQLOLEDB";
00093
00094
00095
00096
00097
00098 if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
00099 } else if ($argProvider=='access')
00100 $argProvider = "Microsoft.Jet.OLEDB.4.0";
00101
00102 if ($argProvider) $dbc->Provider = $argProvider;
00103
00104 if ($argProvider) $argHostname = "PROVIDER=$argProvider;DRIVER={SQL Server};SERVER=$argHostname";
00105
00106
00107 if ($argDatabasename) $argHostname .= ";DATABASE=$argDatabasename";
00108 if ($argUsername) $argHostname .= ";$u=$argUsername";
00109 if ($argPassword)$argHostname .= ";$p=$argPassword";
00110
00111 if ($this->debug) ADOConnection::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
00112
00113 @$dbc->Open((string) $argHostname);
00114
00115 $this->_connectionID = $dbc;
00116
00117 $dbc->CursorLocation = $this->_cursor_location;
00118 return $dbc->State > 0;
00119 } catch (exception $e) {
00120 }
00121
00122 return false;
00123 }
00124
00125
00126 function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
00127 {
00128 return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 function &MetaTables()
00174 {
00175 $arr= array();
00176 $dbc = $this->_connectionID;
00177
00178 $adors=@$dbc->OpenSchema(20);
00179 if ($adors){
00180 $f = $adors->Fields(2);
00181 $t = $adors->Fields(3);
00182 while (!$adors->EOF){
00183 $tt=substr($t->value,0,6);
00184 if ($tt!='SYSTEM' && $tt !='ACCESS')
00185 $arr[]=$f->value;
00186
00187 $adors->MoveNext();
00188 }
00189 $adors->Close();
00190 }
00191
00192 return $arr;
00193 }
00194
00195 function &MetaColumns($table)
00196 {
00197 $table = strtoupper($table);
00198 $arr= array();
00199 $dbc = $this->_connectionID;
00200
00201 $adors=@$dbc->OpenSchema(4);
00202
00203 if ($adors){
00204 $t = $adors->Fields(2);
00205 while (!$adors->EOF){
00206
00207
00208 if (strtoupper($t->Value) == $table) {
00209
00210 $fld = new ADOFieldObject();
00211 $c = $adors->Fields(3);
00212 $fld->name = $c->Value;
00213 $fld->type = 'CHAR';
00214 $fld->max_length = -1;
00215 $arr[strtoupper($fld->name)]=$fld;
00216 }
00217
00218 $adors->MoveNext();
00219 }
00220 $adors->Close();
00221 }
00222
00223 return $arr;
00224 }
00225
00226
00227 function &_query($sql,$inputarr=false)
00228 {
00229 try {
00230
00231 $dbc = $this->_connectionID;
00232
00233
00234
00235 $false = false;
00236
00237 if ($inputarr) {
00238
00239 if (!empty($this->charPage))
00240 $oCmd = new COM('ADODB.Command',null,$this->charPage);
00241 else
00242 $oCmd = new COM('ADODB.Command');
00243 $oCmd->ActiveConnection = $dbc;
00244 $oCmd->CommandText = $sql;
00245 $oCmd->CommandType = 1;
00246
00247 foreach($inputarr as $val) {
00248
00249 $this->adoParameterType = 130;
00250 $p = $oCmd->CreateParameter('name',$this->adoParameterType,1,strlen($val),$val);
00251
00252 $oCmd->Parameters->Append($p);
00253 }
00254 $p = false;
00255 $rs = $oCmd->Execute();
00256 $e = $dbc->Errors;
00257 if ($dbc->Errors->Count > 0) return $false;
00258 return $rs;
00259 }
00260
00261 $rs = @$dbc->Execute($sql,$this->_affectedRows, $this->_execute_option);
00262
00263 if ($dbc->Errors->Count > 0) return $false;
00264 if (! $rs) return $false;
00265
00266 if ($rs->State == 0) {
00267 $true = true;
00268 return $true;
00269 }
00270 return $rs;
00271
00272 } catch (exception $e) {
00273
00274 }
00275 return $false;
00276 }
00277
00278
00279 function BeginTrans()
00280 {
00281 if ($this->transOff) return true;
00282
00283 if (isset($this->_thisTransactions))
00284 if (!$this->_thisTransactions) return false;
00285 else {
00286 $o = $this->_connectionID->Properties("Transaction DDL");
00287 $this->_thisTransactions = $o ? true : false;
00288 if (!$o) return false;
00289 }
00290 @$this->_connectionID->BeginTrans();
00291 $this->transCnt += 1;
00292 return true;
00293 }
00294 function CommitTrans($ok=true)
00295 {
00296 if (!$ok) return $this->RollbackTrans();
00297 if ($this->transOff) return true;
00298
00299 @$this->_connectionID->CommitTrans();
00300 if ($this->transCnt) @$this->transCnt -= 1;
00301 return true;
00302 }
00303 function RollbackTrans() {
00304 if ($this->transOff) return true;
00305 @$this->_connectionID->RollbackTrans();
00306 if ($this->transCnt) @$this->transCnt -= 1;
00307 return true;
00308 }
00309
00310
00311
00312 function ErrorMsg()
00313 {
00314 if (!$this->_connectionID) return "No connection established";
00315 $errmsg = '';
00316
00317 try {
00318 $errc = $this->_connectionID->Errors;
00319 if (!$errc) return "No Errors object found";
00320 if ($errc->Count == 0) return '';
00321 $err = $errc->Item($errc->Count-1);
00322 $errmsg = $err->Description;
00323 }catch(exception $e) {
00324 }
00325 return $errmsg;
00326 }
00327
00328 function ErrorNo()
00329 {
00330 $errc = $this->_connectionID->Errors;
00331 if ($errc->Count == 0) return 0;
00332 $err = $errc->Item($errc->Count-1);
00333 return $err->NativeError;
00334 }
00335
00336
00337 function _close()
00338 {
00339 if ($this->_connectionID) $this->_connectionID->Close();
00340 $this->_connectionID = false;
00341 return true;
00342 }
00343
00344
00345 }
00346
00347
00348
00349
00350
00351 class ADORecordSet_ado extends ADORecordSet {
00352
00353 var $bind = false;
00354 var $databaseType = "ado";
00355 var $dataProvider = "ado";
00356 var $_tarr = false;
00357 var $_flds;
00358 var $canSeek = true;
00359 var $hideErrors = true;
00360
00361 function ADORecordSet_ado($id,$mode=false)
00362 {
00363 if ($mode === false) {
00364 global $ADODB_FETCH_MODE;
00365 $mode = $ADODB_FETCH_MODE;
00366 }
00367 $this->fetchMode = $mode;
00368 return $this->ADORecordSet($id,$mode);
00369 }
00370
00371
00372
00373 function &FetchField($fieldOffset = -1) {
00374 $off=$fieldOffset+1;
00375
00376 $o= new ADOFieldObject();
00377 $rs = $this->_queryID;
00378 $f = $rs->Fields($fieldOffset);
00379 $o->name = $f->Name;
00380 $t = $f->Type;
00381 $o->type = $this->MetaType($t);
00382 $o->max_length = $f->DefinedSize;
00383 $o->ado_type = $t;
00384
00385
00386
00387 return $o;
00388 }
00389
00390
00391 function Fields($colname)
00392 {
00393 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
00394 if (!$this->bind) {
00395 $this->bind = array();
00396 for ($i=0; $i < $this->_numOfFields; $i++) {
00397 $o = $this->FetchField($i);
00398 $this->bind[strtoupper($o->name)] = $i;
00399 }
00400 }
00401
00402 return $this->fields[$this->bind[strtoupper($colname)]];
00403 }
00404
00405
00406 function _initrs()
00407 {
00408 $rs = $this->_queryID;
00409 $this->_numOfRows = $rs->RecordCount;
00410
00411 $f = $rs->Fields;
00412 $this->_numOfFields = $f->Count;
00413 }
00414
00415
00416
00417 function _seek($row)
00418 {
00419 $rs = $this->_queryID;
00420
00421
00422
00423 if ($this->_currentRow > $row) return false;
00424 @$rs->Move((integer)$row - $this->_currentRow-1);
00425 return true;
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 function MetaType($t,$len=-1,$fieldobj=false)
00511 {
00512 if (is_object($t)) {
00513 $fieldobj = $t;
00514 $t = $fieldobj->type;
00515 $len = $fieldobj->max_length;
00516 }
00517
00518 if (!is_numeric($t)) return $t;
00519
00520 switch ($t) {
00521 case 0:
00522 case 12:
00523 case 8:
00524 case 129:
00525 case 130:
00526 case 200:
00527 case 202:
00528 case 128:
00529 case 204:
00530 case 72:
00531 if ($len <= $this->blobSize) return 'C';
00532
00533 case 201:
00534 case 203:
00535 return 'X';
00536 case 128:
00537 case 204:
00538 case 205:
00539 return 'B';
00540 case 7:
00541 case 133: return 'D';
00542
00543 case 134:
00544 case 135: return 'T';
00545
00546 case 11: return 'L';
00547
00548 case 16:
00549 case 2:
00550 case 3:
00551 case 4:
00552 case 17:
00553 case 18:
00554 case 19:
00555 case 20:
00556 return 'I';
00557 default: return 'N';
00558 }
00559 }
00560
00561
00562 function _fetch()
00563 {
00564 $rs = $this->_queryID;
00565 if (!$rs or $rs->EOF) {
00566 $this->fields = false;
00567 return false;
00568 }
00569 $this->fields = array();
00570
00571 if (!$this->_tarr) {
00572 $tarr = array();
00573 $flds = array();
00574 for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
00575 $f = $rs->Fields($i);
00576 $flds[] = $f;
00577 $tarr[] = $f->Type;
00578 }
00579
00580 $this->_tarr = $tarr;
00581 $this->_flds = $flds;
00582 }
00583 $t = reset($this->_tarr);
00584 $f = reset($this->_flds);
00585
00586 if ($this->hideErrors) $olde = error_reporting(E_ERROR|E_CORE_ERROR);
00587 for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
00588
00589 switch($t) {
00590 case 135:
00591 if (!strlen((string)$f->value)) $this->fields[] = false;
00592 else {
00593 if (!is_numeric($f->value)) # $val = variant_date_to_timestamp($f->value);
00594
00595 $val= (float) variant_cast($f->value,VT_R8)*3600*24-2209161600;
00596 else
00597 $val = $f->value;
00598 $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
00599 }
00600 break;
00601 case 133:
00602 if ($val = $f->value) {
00603 $this->fields[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
00604 } else
00605 $this->fields[] = false;
00606 break;
00607 case 7:
00608 if (!strlen((string)$f->value)) $this->fields[] = false;
00609 else {
00610 if (!is_numeric($f->value)) $val = variant_date_to_timestamp($f->value);
00611 else $val = $f->value;
00612
00613 if (($val % 86400) == 0) $this->fields[] = adodb_date('Y-m-d',$val);
00614 else $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
00615 }
00616 break;
00617 case 1:
00618 $this->fields[] = false;
00619 break;
00620 case 6:
00621 ADOConnection::outp( '<b>'.$f->Name.': currency type not supported by PHP</b>');
00622 $this->fields[] = (float) $f->value;
00623 break;
00624 default:
00625 $this->fields[] = $f->value;
00626 break;
00627 }
00628
00629 $f = next($this->_flds);
00630 $t = next($this->_tarr);
00631 }
00632 if ($this->hideErrors) error_reporting($olde);
00633 @$rs->MoveNext();
00634
00635 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
00636 $this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
00637 }
00638 return true;
00639 }
00640
00641 function NextRecordSet()
00642 {
00643 $rs = $this->_queryID;
00644 $this->_queryID = $rs->NextRecordSet();
00645
00646 if ($this->_queryID == null) return false;
00647
00648 $this->_currentRow = -1;
00649 $this->_currentPage = -1;
00650 $this->bind = false;
00651 $this->fields = false;
00652 $this->_flds = false;
00653 $this->_tarr = false;
00654
00655 $this->_inited = false;
00656 $this->Init();
00657 return true;
00658 }
00659
00660 function _close() {
00661 $this->_flds = false;
00662 @$this->_queryID->Close();
00663 $this->_queryID = false;
00664 }
00665
00666 }
00667
00668 ?>