00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 if (!defined('ADODB_DIR')) die();
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 function adodb_pdo_type($t)
00052 {
00053 switch($t) {
00054 case 2: return 'VARCHAR';
00055 case 3: return 'BLOB';
00056 default: return 'NUMERIC';
00057 }
00058 }
00059
00060
00061
00062
00064
00065
00066
00067 class ADODB_pdo_base extends ADODB_pdo {
00068
00069 var $sysDate = "'?'";
00070 var $sysTimeStamp = "'?'";
00071
00072
00073 function _init($parentDriver)
00074 {
00075 $parentDriver->_bindInputArray = true;
00076 #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
00077 }
00078
00079 function ServerInfo()
00080 {
00081 return ADOConnection::ServerInfo();
00082 }
00083
00084 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
00085 {
00086 $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
00087 return $ret;
00088 }
00089
00090 function MetaTables()
00091 {
00092 return false;
00093 }
00094
00095 function MetaColumns()
00096 {
00097 return false;
00098 }
00099 }
00100
00101
00102 class ADODB_pdo extends ADOConnection {
00103 var $databaseType = "pdo";
00104 var $dataProvider = "pdo";
00105 var $fmtDate = "'Y-m-d'";
00106 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
00107 var $replaceQuote = "''";
00108 var $hasAffectedRows = true;
00109 var $_bindInputArray = true;
00110 var $_genSeqSQL = "create table %s (id integer)";
00111 var $_autocommit = true;
00112 var $_haserrorfunctions = true;
00113 var $_lastAffectedRows = 0;
00114
00115 var $_errormsg = false;
00116 var $_errorno = false;
00117
00118 var $dsnType = '';
00119 var $stmt = false;
00120
00121 function ADODB_pdo()
00122 {
00123 }
00124
00125 function _UpdatePDO()
00126 {
00127 $d = &$this->_driver;
00128 $this->fmtDate = $d->fmtDate;
00129 $this->fmtTimeStamp = $d->fmtTimeStamp;
00130 $this->replaceQuote = $d->replaceQuote;
00131 $this->sysDate = $d->sysDate;
00132 $this->sysTimeStamp = $d->sysTimeStamp;
00133 $this->random = $d->random;
00134 $this->concat_operator = $d->concat_operator;
00135 $this->nameQuote = $d->nameQuote;
00136
00137 $this->hasGenID = $d->hasGenID;
00138 $this->_genIDSQL = $d->_genIDSQL;
00139 $this->_genSeqSQL = $d->_genSeqSQL;
00140 $this->_dropSeqSQL = $d->_dropSeqSQL;
00141
00142 $d->_init($this);
00143 }
00144
00145 function Time()
00146 {
00147 if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
00148 else $sql = "select $this->sysTimeStamp";
00149
00150 $rs =& $this->_Execute($sql);
00151 if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
00152
00153 return false;
00154 }
00155
00156
00157 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
00158 {
00159 $at = strpos($argDSN,':');
00160 $this->dsnType = substr($argDSN,0,$at);
00161
00162 if ($argDatabasename) {
00163 $argDSN .= ';dbname='.$argDatabasename;
00164 }
00165 try {
00166 $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
00167 } catch (Exception $e) {
00168 $this->_connectionID = false;
00169 $this->_errorno = -1;
00170
00171 $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
00172 return false;
00173 }
00174
00175 if ($this->_connectionID) {
00176 switch(ADODB_ASSOC_CASE){
00177 case 0: $m = PDO::CASE_LOWER; break;
00178 case 1: $m = PDO::CASE_UPPER; break;
00179 default:
00180 case 2: $m = PDO::CASE_NATURAL; break;
00181 }
00182
00183
00184 $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
00185
00186 $class = 'ADODB_pdo_'.$this->dsnType;
00187
00188 switch($this->dsnType) {
00189 case 'oci':
00190 case 'mysql':
00191 case 'pgsql':
00192 case 'mssql':
00193 include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
00194 break;
00195 }
00196 if (class_exists($class))
00197 $this->_driver = new $class();
00198 else
00199 $this->_driver = new ADODB_pdo_base();
00200
00201 $this->_driver->_connectionID = $this->_connectionID;
00202 $this->_UpdatePDO();
00203 return true;
00204 }
00205 $this->_driver = new ADODB_pdo_base();
00206 return false;
00207 }
00208
00209
00210 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
00211 {
00212 return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
00213 }
00214
00215
00216
00217
00218 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
00219 {
00220 $save = $this->_driver->fetchMode;
00221 $this->_driver->fetchMode = $this->fetchMode;
00222 $this->_driver->debug = $this->debug;
00223 $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
00224 $this->_driver->fetchMode = $save;
00225 return $ret;
00226 }
00227
00228
00229 function ServerInfo()
00230 {
00231 return $this->_driver->ServerInfo();
00232 }
00233
00234 function MetaTables($ttype=false,$showSchema=false,$mask=false)
00235 {
00236 return $this->_driver->MetaTables($ttype,$showSchema,$mask);
00237 }
00238
00239 function MetaColumns($table,$normalize=true)
00240 {
00241 return $this->_driver->MetaColumns($table,$normalize);
00242 }
00243
00244 function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
00245 {
00246 $obj = $stmt[1];
00247 if ($type) $obj->bindParam($name,$var,$type,$maxLen);
00248 else $obj->bindParam($name, $var);
00249 }
00250
00251
00252 function ErrorMsg()
00253 {
00254 if ($this->_errormsg !== false) return $this->_errormsg;
00255 if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo();
00256 else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo();
00257 else return 'No Connection Established';
00258
00259
00260 if ($arr) {
00261 if (sizeof($arr)<2) return '';
00262 if ((integer)$arr[1]) return $arr[2];
00263 else return '';
00264 } else return '-1';
00265 }
00266
00267
00268 function ErrorNo()
00269 {
00270 if ($this->_errorno !== false) return $this->_errorno;
00271 if (!empty($this->_stmt)) $err = $this->_stmt->errorCode();
00272 else if (!empty($this->_connectionID)) {
00273 $arr = $this->_connectionID->errorInfo();
00274 if (isset($arr[0])) $err = $arr[0];
00275 else $err = -1;
00276 } else
00277 return 0;
00278
00279 if ($err == '00000') return 0;
00280 return $err;
00281 }
00282
00283 function BeginTrans()
00284 {
00285 if (!$this->hasTransactions) return false;
00286 if ($this->transOff) return true;
00287 $this->transCnt += 1;
00288 $this->_autocommit = false;
00289 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
00290 return $this->_connectionID->beginTransaction();
00291 }
00292
00293 function CommitTrans($ok=true)
00294 {
00295 if (!$this->hasTransactions) return false;
00296 if ($this->transOff) return true;
00297 if (!$ok) return $this->RollbackTrans();
00298 if ($this->transCnt) $this->transCnt -= 1;
00299 $this->_autocommit = true;
00300
00301 $ret = $this->_connectionID->commit();
00302 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00303 return $ret;
00304 }
00305
00306 function RollbackTrans()
00307 {
00308 if (!$this->hasTransactions) return false;
00309 if ($this->transOff) return true;
00310 if ($this->transCnt) $this->transCnt -= 1;
00311 $this->_autocommit = true;
00312
00313 $ret = $this->_connectionID->rollback();
00314 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00315 return $ret;
00316 }
00317
00318 function Prepare($sql)
00319 {
00320 $this->_stmt = $this->_connectionID->prepare($sql);
00321 if ($this->_stmt) return array($sql,$this->_stmt);
00322
00323 return false;
00324 }
00325
00326 function PrepareStmt($sql)
00327 {
00328 $stmt = $this->_connectionID->prepare($sql);
00329 if (!$stmt) return false;
00330 $obj = new ADOPDOStatement($stmt,$this);
00331 return $obj;
00332 }
00333
00334
00335
00336 function _query($sql,$inputarr=false)
00337 {
00338 if (is_array($sql)) {
00339 $stmt = $sql[1];
00340 } else {
00341 $stmt = $this->_connectionID->prepare($sql);
00342 }
00343 #adodb_backtrace();
00344 #var_dump($this->_bindInputArray);
00345 if ($stmt) {
00346 $this->_driver->debug = $this->debug;
00347 if ($inputarr) $ok = $stmt->execute($inputarr);
00348 else $ok = $stmt->execute();
00349 }
00350
00351
00352 $this->_errormsg = false;
00353 $this->_errorno = false;
00354
00355 if ($ok) {
00356 $this->_stmt = $stmt;
00357 return $stmt;
00358 }
00359
00360 if ($stmt) {
00361
00362 $arr = $stmt->errorinfo();
00363 if ((integer)$arr[1]) {
00364 $this->_errormsg = $arr[2];
00365 $this->_errorno = $arr[1];
00366 }
00367
00368 } else {
00369 $this->_errormsg = false;
00370 $this->_errorno = false;
00371 }
00372 return false;
00373 }
00374
00375
00376 function _close()
00377 {
00378 $this->_stmt = false;
00379 return true;
00380 }
00381
00382 function _affectedrows()
00383 {
00384 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00385 }
00386
00387 function _insertid()
00388 {
00389 return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
00390 }
00391 }
00392
00393 class ADOPDOStatement {
00394
00395 var $databaseType = "pdo";
00396 var $dataProvider = "pdo";
00397 var $_stmt;
00398 var $_connectionID;
00399
00400 function ADOPDOStatement($stmt,$connection)
00401 {
00402 $this->_stmt = $stmt;
00403 $this->_connectionID = $connection;
00404 }
00405
00406 function Execute($inputArr=false)
00407 {
00408 $savestmt = $this->_connectionID->_stmt;
00409 $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
00410 $this->_connectionID->_stmt = $savestmt;
00411 return $rs;
00412 }
00413
00414 function InParameter(&$var,$name,$maxLen=4000,$type=false)
00415 {
00416
00417 if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
00418 else $this->_stmt->bindParam($name, $var);
00419 }
00420
00421 function Affected_Rows()
00422 {
00423 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00424 }
00425
00426 function ErrorMsg()
00427 {
00428 if ($this->_stmt) $arr = $this->_stmt->errorInfo();
00429 else $arr = $this->_connectionID->errorInfo();
00430
00431 if (is_array($arr)) {
00432 if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
00433 else return '';
00434 } else return '-1';
00435 }
00436
00437 function NumCols()
00438 {
00439 return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
00440 }
00441
00442 function ErrorNo()
00443 {
00444 if ($this->_stmt) return $this->_stmt->errorCode();
00445 else return $this->_connectionID->errorInfo();
00446 }
00447 }
00448
00449
00450
00451
00452
00453 class ADORecordSet_pdo extends ADORecordSet {
00454
00455 var $bind = false;
00456 var $databaseType = "pdo";
00457 var $dataProvider = "pdo";
00458
00459 function ADORecordSet_pdo($id,$mode=false)
00460 {
00461 if ($mode === false) {
00462 global $ADODB_FETCH_MODE;
00463 $mode = $ADODB_FETCH_MODE;
00464 }
00465 $this->adodbFetchMode = $mode;
00466 switch($mode) {
00467 case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
00468 case ADODB_FETCH_ASSOC: $mode = PDO::FETCH_ASSOC; break;
00469
00470 case ADODB_FETCH_BOTH:
00471 default: $mode = PDO::FETCH_BOTH; break;
00472 }
00473 $this->fetchMode = $mode;
00474
00475 $this->_queryID = $id;
00476 $this->ADORecordSet($id);
00477 }
00478
00479
00480 function Init()
00481 {
00482 if ($this->_inited) return;
00483 $this->_inited = true;
00484 if ($this->_queryID) @$this->_initrs();
00485 else {
00486 $this->_numOfRows = 0;
00487 $this->_numOfFields = 0;
00488 }
00489 if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
00490 $this->_currentRow = 0;
00491 if ($this->EOF = ($this->_fetch() === false)) {
00492 $this->_numOfRows = 0;
00493 }
00494 } else {
00495 $this->EOF = true;
00496 }
00497 }
00498
00499 function _initrs()
00500 {
00501 global $ADODB_COUNTRECS;
00502
00503 $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
00504 if (!$this->_numOfRows) $this->_numOfRows = -1;
00505 $this->_numOfFields = $this->_queryID->columnCount();
00506 }
00507
00508
00509 function &FetchField($fieldOffset = -1)
00510 {
00511 $off=$fieldOffset+1;
00512
00513 $o= new ADOFieldObject();
00514 $arr = @$this->_queryID->getColumnMeta($fieldOffset);
00515 if (!$arr) {
00516 $o->name = 'bad getColumnMeta()';
00517 $o->max_length = -1;
00518 $o->type = 'VARCHAR';
00519 $o->precision = 0;
00520 # $false = false;
00521 return $o;
00522 }
00523
00524 $o->name = $arr['name'];
00525 if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type'];
00526 else $o->type = adodb_pdo_type($arr['pdo_type']);
00527 $o->max_length = $arr['len'];
00528 $o->precision = $arr['precision'];
00529
00530 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
00531 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
00532 return $o;
00533 }
00534
00535 function _seek($row)
00536 {
00537 return false;
00538 }
00539
00540 function _fetch()
00541 {
00542 if (!$this->_queryID) return false;
00543
00544 $this->fields = $this->_queryID->fetch($this->fetchMode);
00545 return !empty($this->fields);
00546 }
00547
00548 function _close()
00549 {
00550 $this->_queryID = false;
00551 }
00552
00553 function Fields($colname)
00554 {
00555 if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
00556
00557 if (!$this->bind) {
00558 $this->bind = array();
00559 for ($i=0; $i < $this->_numOfFields; $i++) {
00560 $o = $this->FetchField($i);
00561 $this->bind[strtoupper($o->name)] = $i;
00562 }
00563 }
00564 return $this->fields[$this->bind[strtoupper($colname)]];
00565 }
00566
00567 }
00568
00569 ?>