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, $argProvider= 'MSDASQL')
00062 {
00063 try {
00064 $u = 'UID';
00065 $p = 'PWD';
00066
00067 if (!empty($this->charPage))
00068 $dbc = new COM('ADODB.Connection',null,$this->charPage);
00069 else
00070 $dbc = new COM('ADODB.Connection');
00071
00072 if (! $dbc) return false;
00073
00074
00075 if ($argProvider=='mssql') {
00076 $u = 'User Id';
00077 $p = 'Password';
00078 $argProvider = "SQLOLEDB";
00079
00080
00081
00082
00083
00084 if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
00085 } else if ($argProvider=='access')
00086 $argProvider = "Microsoft.Jet.OLEDB.4.0";
00087
00088 if ($argProvider) $dbc->Provider = $argProvider;
00089
00090 if ($argUsername) $argHostname .= ";$u=$argUsername";
00091 if ($argPassword)$argHostname .= ";$p=$argPassword";
00092
00093 if ($this->debug) ADOConnection::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
00094
00095 @$dbc->Open((string) $argHostname);
00096
00097 $this->_connectionID = $dbc;
00098
00099 $dbc->CursorLocation = $this->_cursor_location;
00100 return $dbc->State > 0;
00101 } catch (exception $e) {
00102 }
00103
00104 return false;
00105 }
00106
00107
00108 function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
00109 {
00110 return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
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 function &MetaTables()
00156 {
00157 $arr= array();
00158 $dbc = $this->_connectionID;
00159
00160 $adors=@$dbc->OpenSchema(20);
00161 if ($adors){
00162 $f = $adors->Fields(2);
00163 $t = $adors->Fields(3);
00164 while (!$adors->EOF){
00165 $tt=substr($t->value,0,6);
00166 if ($tt!='SYSTEM' && $tt !='ACCESS')
00167 $arr[]=$f->value;
00168
00169 $adors->MoveNext();
00170 }
00171 $adors->Close();
00172 }
00173
00174 return $arr;
00175 }
00176
00177 function &MetaColumns($table)
00178 {
00179 $table = strtoupper($table);
00180 $arr= array();
00181 $dbc = $this->_connectionID;
00182
00183 $adors=@$dbc->OpenSchema(4);
00184
00185 if ($adors){
00186 $t = $adors->Fields(2);
00187 while (!$adors->EOF){
00188
00189
00190 if (strtoupper($t->Value) == $table) {
00191
00192 $fld = new ADOFieldObject();
00193 $c = $adors->Fields(3);
00194 $fld->name = $c->Value;
00195 $fld->type = 'CHAR';
00196 $fld->max_length = -1;
00197 $arr[strtoupper($fld->name)]=$fld;
00198 }
00199
00200 $adors->MoveNext();
00201 }
00202 $adors->Close();
00203 }
00204
00205 return $arr;
00206 }
00207
00208
00209
00210
00211
00212 function &_query($sql,$inputarr=false)
00213 {
00214 try {
00215
00216 $dbc = $this->_connectionID;
00217
00218
00219 if ($inputarr) {
00220
00221 if (!empty($this->charPage))
00222 $oCmd = new COM('ADODB.Command',null,$this->charPage);
00223 else
00224 $oCmd = new COM('ADODB.Command');
00225 $oCmd->ActiveConnection = $dbc;
00226 $oCmd->CommandText = $sql;
00227 $oCmd->CommandType = 1;
00228
00229 foreach($inputarr as $val) {
00230
00231 $this->adoParameterType = 130;
00232 $p = $oCmd->CreateParameter('name',$this->adoParameterType,1,strlen($val),$val);
00233
00234 $oCmd->Parameters->Append($p);
00235 }
00236 $p = false;
00237 $rs = $oCmd->Execute();
00238 $e = $dbc->Errors;
00239 if ($dbc->Errors->Count > 0) return false;
00240 return $rs;
00241 }
00242
00243 $rs = @$dbc->Execute($sql,$this->_affectedRows, $this->_execute_option);
00244
00245 if ($dbc->Errors->Count > 0) return false;
00246 if (! $rs) return false;
00247
00248 if ($rs->State == 0) return true;
00249 return $rs;
00250
00251 } catch (exception $e) {
00252
00253 }
00254 return false;
00255 }
00256
00257
00258 function BeginTrans()
00259 {
00260 if ($this->transOff) return true;
00261
00262 if (isset($this->_thisTransactions))
00263 if (!$this->_thisTransactions) return false;
00264 else {
00265 $o = $this->_connectionID->Properties("Transaction DDL");
00266 $this->_thisTransactions = $o ? true : false;
00267 if (!$o) return false;
00268 }
00269 @$this->_connectionID->BeginTrans();
00270 $this->transCnt += 1;
00271 return true;
00272 }
00273 function CommitTrans($ok=true)
00274 {
00275 if (!$ok) return $this->RollbackTrans();
00276 if ($this->transOff) return true;
00277
00278 @$this->_connectionID->CommitTrans();
00279 if ($this->transCnt) @$this->transCnt -= 1;
00280 return true;
00281 }
00282 function RollbackTrans() {
00283 if ($this->transOff) return true;
00284 @$this->_connectionID->RollbackTrans();
00285 if ($this->transCnt) @$this->transCnt -= 1;
00286 return true;
00287 }
00288
00289
00290
00291 function ErrorMsg()
00292 {
00293 $errc = $this->_connectionID->Errors;
00294 if ($errc->Count == 0) return '';
00295 $err = $errc->Item($errc->Count-1);
00296 return $err->Description;
00297 }
00298
00299 function ErrorNo()
00300 {
00301 $errc = $this->_connectionID->Errors;
00302 if ($errc->Count == 0) return 0;
00303 $err = $errc->Item($errc->Count-1);
00304 return $err->NativeError;
00305 }
00306
00307
00308 function _close()
00309 {
00310 if ($this->_connectionID) $this->_connectionID->Close();
00311 $this->_connectionID = false;
00312 return true;
00313 }
00314
00315
00316 }
00317
00318
00319
00320
00321
00322 class ADORecordSet_ado extends ADORecordSet {
00323
00324 var $bind = false;
00325 var $databaseType = "ado";
00326 var $dataProvider = "ado";
00327 var $_tarr = false;
00328 var $_flds;
00329 var $canSeek = true;
00330 var $hideErrors = true;
00331
00332 function ADORecordSet_ado($id,$mode=false)
00333 {
00334 if ($mode === false) {
00335 global $ADODB_FETCH_MODE;
00336 $mode = $ADODB_FETCH_MODE;
00337 }
00338 $this->fetchMode = $mode;
00339 return $this->ADORecordSet($id,$mode);
00340 }
00341
00342
00343
00344 function &FetchField($fieldOffset = -1) {
00345 $off=$fieldOffset+1;
00346
00347 $o= new ADOFieldObject();
00348 $rs = $this->_queryID;
00349 $f = $rs->Fields($fieldOffset);
00350 $o->name = $f->Name;
00351 $t = $f->Type;
00352 $o->type = $this->MetaType($t);
00353 $o->max_length = $f->DefinedSize;
00354 $o->ado_type = $t;
00355
00356
00357
00358 return $o;
00359 }
00360
00361
00362 function Fields($colname)
00363 {
00364 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
00365 if (!$this->bind) {
00366 $this->bind = array();
00367 for ($i=0; $i < $this->_numOfFields; $i++) {
00368 $o = $this->FetchField($i);
00369 $this->bind[strtoupper($o->name)] = $i;
00370 }
00371 }
00372
00373 return $this->fields[$this->bind[strtoupper($colname)]];
00374 }
00375
00376
00377 function _initrs()
00378 {
00379 $rs = $this->_queryID;
00380 $this->_numOfRows = $rs->RecordCount;
00381
00382 $f = $rs->Fields;
00383 $this->_numOfFields = $f->Count;
00384 }
00385
00386
00387
00388 function _seek($row)
00389 {
00390 $rs = $this->_queryID;
00391
00392
00393
00394 if ($this->_currentRow > $row) return false;
00395 @$rs->Move((integer)$row - $this->_currentRow-1);
00396 return true;
00397 }
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
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 function MetaType($t,$len=-1,$fieldobj=false)
00482 {
00483 if (is_object($t)) {
00484 $fieldobj = $t;
00485 $t = $fieldobj->type;
00486 $len = $fieldobj->max_length;
00487 }
00488
00489 if (!is_numeric($t)) return $t;
00490
00491 switch ($t) {
00492 case 0:
00493 case 12:
00494 case 8:
00495 case 129:
00496 case 130:
00497 case 200:
00498 case 202:
00499 case 128:
00500 case 204:
00501 case 72:
00502 if ($len <= $this->blobSize) return 'C';
00503
00504 case 201:
00505 case 203:
00506 return 'X';
00507 case 128:
00508 case 204:
00509 case 205:
00510 return 'B';
00511 case 7:
00512 case 133: return 'D';
00513
00514 case 134:
00515 case 135: return 'T';
00516
00517 case 11: return 'L';
00518
00519 case 16:
00520 case 2:
00521 case 3:
00522 case 4:
00523 case 17:
00524 case 18:
00525 case 19:
00526 case 20:
00527 return 'I';
00528 default: return 'N';
00529 }
00530 }
00531
00532
00533 function _fetch()
00534 {
00535 $rs = $this->_queryID;
00536 if (!$rs or $rs->EOF) {
00537 $this->fields = false;
00538 return false;
00539 }
00540 $this->fields = array();
00541
00542 if (!$this->_tarr) {
00543 $tarr = array();
00544 $flds = array();
00545 for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
00546 $f = $rs->Fields($i);
00547 $flds[] = $f;
00548 $tarr[] = $f->Type;
00549 }
00550
00551 $this->_tarr = $tarr;
00552 $this->_flds = $flds;
00553 }
00554 $t = reset($this->_tarr);
00555 $f = reset($this->_flds);
00556
00557 if ($this->hideErrors) $olde = error_reporting(E_ERROR|E_CORE_ERROR);
00558 for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
00559
00560 switch($t) {
00561 case 135:
00562 if (!strlen((string)$f->value)) $this->fields[] = false;
00563 else {
00564 if (!is_numeric($f->value)) # $val = variant_date_to_timestamp($f->value);
00565
00566 $val= (float) variant_cast($f->value,VT_R8)*3600*24-2209161600;
00567 else
00568 $val = $f->value;
00569 $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
00570 }
00571 break;
00572 case 133:
00573 if ($val = $f->value) {
00574 $this->fields[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
00575 } else
00576 $this->fields[] = false;
00577 break;
00578 case 7:
00579 if (!strlen((string)$f->value)) $this->fields[] = false;
00580 else {
00581 if (!is_numeric($f->value)) $val = variant_date_to_timestamp($f->value);
00582 else $val = $f->value;
00583
00584 if (($val % 86400) == 0) $this->fields[] = adodb_date('Y-m-d',$val);
00585 else $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
00586 }
00587 break;
00588 case 1:
00589 $this->fields[] = false;
00590 break;
00591 case 6:
00592 ADOConnection::outp( '<b>'.$f->Name.': currency type not supported by PHP</b>');
00593 $this->fields[] = (float) $f->value;
00594 break;
00595 default:
00596 $this->fields[] = $f->value;
00597 break;
00598 }
00599
00600 $f = next($this->_flds);
00601 $t = next($this->_tarr);
00602 }
00603 if ($this->hideErrors) error_reporting($olde);
00604 @$rs->MoveNext();
00605
00606 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
00607 $this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
00608 }
00609 return true;
00610 }
00611
00612 function NextRecordSet()
00613 {
00614 $rs = $this->_queryID;
00615 $this->_queryID = $rs->NextRecordSet();
00616
00617 if ($this->_queryID == null) return false;
00618
00619 $this->_currentRow = -1;
00620 $this->_currentPage = -1;
00621 $this->bind = false;
00622 $this->fields = false;
00623 $this->_flds = false;
00624 $this->_tarr = false;
00625
00626 $this->_inited = false;
00627 $this->Init();
00628 return true;
00629 }
00630
00631 function _close() {
00632 $this->_flds = false;
00633 @$this->_queryID->Close();
00634 $this->_queryID = false;
00635 }
00636
00637 }
00638
00639 ?>