Documentation TYPO3 par Ameos

adodb-pdo.inc.php

00001 <?php
00002 /* 
00003 V4.80 8 Mar 2006  (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved.
00004   Released under both BSD license and Lesser GPL library license. 
00005   Whenever there is any discrepancy between the two licenses, 
00006   the BSD license will take precedence. 
00007 Set tabs to 4 for best viewing.
00008   
00009   Latest version is available at http://adodb.sourceforge.net
00010   
00011   Requires ODBC. Works on Windows and Unix.
00012 
00013         Problems: 
00014                 Where is float/decimal type in pdo_param_type
00015                 LOB handling for CLOB/BLOB differs significantly
00016 */
00017 // security - hide paths
00018 if (!defined('ADODB_DIR')) die();
00019 
00020 
00021 /*
00022 enum pdo_param_type {
00023 PDO::PARAM_NULL, 0
00024 
00025 /* int as in long (the php native int type).
00026  * If you mark a column as an int, PDO expects get_col to return
00027  * a pointer to a long 
00028 PDO::PARAM_INT, 1
00029 
00030 /* get_col ptr should point to start of the string buffer 
00031 PDO::PARAM_STR, 2
00032 
00033 /* get_col: when len is 0 ptr should point to a php_stream *,
00034  * otherwise it should behave like a string. Indicate a NULL field
00035  * value by setting the ptr to NULL 
00036 PDO::PARAM_LOB, 3
00037 
00038 /* get_col: will expect the ptr to point to a new PDOStatement object handle,
00039  * but this isn't wired up yet 
00040 PDO::PARAM_STMT, 4 /* hierarchical result set 
00041 
00042 /* get_col ptr should point to a zend_bool 
00043 PDO::PARAM_BOOL, 5
00044 
00045 
00046 /* magic flag to denote a parameter as being input/output 
00047 PDO::PARAM_INPUT_OUTPUT = 0x80000000
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 = "''"; // string to use to replace quotes
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                 $d->_init($this);
00138         }
00139         
00140         function Time()
00141         {
00142                 if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
00143                 else $sql = "select $this->sysTimeStamp";
00144                 
00145                 $rs =& $this->_Execute($sql);
00146                 if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
00147                 
00148                 return false;
00149         }
00150         
00151         // returns true or false
00152         function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
00153         {
00154                 $at = strpos($argDSN,':');
00155                 $this->dsnType = substr($argDSN,0,$at);
00156 
00157                 if ($argDatabasename) {
00158                         $argDSN .= ';dbname='.$argDatabasename;
00159                 }
00160                 try {
00161                         $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
00162                 } catch (Exception $e) {
00163                         $this->_connectionID = false;
00164                         $this->_errorno = -1;
00165                         //var_dump($e);
00166                         $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
00167                         return false;
00168                 }
00169                 
00170                 if ($this->_connectionID) {
00171                         switch(ADODB_ASSOC_CASE){
00172                         case 0: $m = PDO::CASE_LOWER; break;
00173                         case 1: $m = PDO::CASE_UPPER; break;
00174                         default:
00175                         case 2: $m = PDO::CASE_NATURAL; break;
00176                         }
00177                         
00178                         //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
00179                         $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
00180                         
00181                         $class = 'ADODB_pdo_'.$this->dsnType;
00182                         //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00183                         switch($this->dsnType) {
00184                         case 'oci':
00185                         case 'mysql':
00186                         case 'pgsql':
00187                         case 'mssql':
00188                                 include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
00189                                 break;
00190                         }
00191                         if (class_exists($class))
00192                                 $this->_driver = new $class();
00193                         else
00194                                 $this->_driver = new ADODB_pdo_base();
00195                         
00196                         $this->_driver->_connectionID = $this->_connectionID;
00197                         $this->_UpdatePDO();
00198                         return true;
00199                 }
00200                 $this->_driver = new ADODB_pdo_base();
00201                 return false;
00202         }
00203         
00204         // returns true or false
00205         function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
00206         {
00207                 return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
00208         }
00209         
00210         /*------------------------------------------------------------------------------*/
00211         
00212         
00213         function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
00214         {       
00215                 $save = $this->_driver->fetchMode;
00216                 $this->_driver->fetchMode = $this->fetchMode;
00217                 $this->_driver->debug = $this->debug;
00218                 $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
00219                 $this->_driver->fetchMode = $save;
00220                 return $ret;
00221         }
00222         
00223         
00224         function ServerInfo()
00225         {
00226                 return $this->_driver->ServerInfo();
00227         }
00228         
00229         function MetaTables($ttype=false,$showSchema=false,$mask=false)
00230         {
00231                 return $this->_driver->MetaTables($ttype,$showSchema,$mask);
00232         }
00233         
00234         function MetaColumns($table,$normalize=true)
00235         {
00236                 return $this->_driver->MetaColumns($table,$normalize);
00237         }
00238         
00239         function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
00240         {
00241                 $obj = $stmt[1];
00242                 if ($type) $obj->bindParam($name,$var,$type,$maxLen);
00243                 else $obj->bindParam($name, $var);
00244         }
00245         
00246         
00247         function ErrorMsg()
00248         {
00249                 if ($this->_errormsg !== false) return $this->_errormsg;
00250                 if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo();
00251                 else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo();
00252                 else return 'No Connection Established';
00253                 
00254                 
00255                 if ($arr) {
00256                         if (sizeof($arr)<2) return '';
00257                         if ((integer)$arr[1]) return $arr[2];
00258                         else return '';
00259                 } else return '-1';
00260         }
00261         
00262 
00263         function ErrorNo()
00264         {
00265                 if ($this->_errorno !== false) return $this->_errorno;
00266                 if (!empty($this->_stmt)) $err = $this->_stmt->errorCode();
00267                 else if (!empty($this->_connectionID)) {
00268                         $arr = $this->_connectionID->errorInfo();
00269                         if (isset($arr[0])) $err = $arr[0];
00270                         else $err = -1;
00271                 } else
00272                         return 0;
00273                         
00274                 if ($err == '00000') return 0; // allows empty check
00275                 return $err;
00276         }
00277 
00278         function BeginTrans()
00279         {       
00280                 if (!$this->hasTransactions) return false;
00281                 if ($this->transOff) return true; 
00282                 $this->transCnt += 1;
00283                 $this->_autocommit = false;
00284                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
00285                 return $this->_connectionID->beginTransaction();
00286         }
00287         
00288         function CommitTrans($ok=true) 
00289         { 
00290                 if (!$this->hasTransactions) return false;
00291                 if ($this->transOff) return true; 
00292                 if (!$ok) return $this->RollbackTrans();
00293                 if ($this->transCnt) $this->transCnt -= 1;
00294                 $this->_autocommit = true;
00295                 
00296                 $ret = $this->_connectionID->commit();
00297                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00298                 return $ret;
00299         }
00300         
00301         function RollbackTrans()
00302         {
00303                 if (!$this->hasTransactions) return false;
00304                 if ($this->transOff) return true; 
00305                 if ($this->transCnt) $this->transCnt -= 1;
00306                 $this->_autocommit = true;
00307                 
00308                 $ret = $this->_connectionID->rollback();
00309                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00310                 return $ret;
00311         }
00312         
00313         function Prepare($sql)
00314         {
00315                 $this->_stmt = $this->_connectionID->prepare($sql);
00316                 if ($this->_stmt) return array($sql,$this->_stmt);
00317                 
00318                 return false;
00319         }
00320         
00321         function PrepareStmt($sql)
00322         {
00323                 $stmt = $this->_connectionID->prepare($sql);
00324                 if (!$stmt) return false;
00325                 $obj = new ADOPDOStatement($stmt,$this);
00326                 return $obj;
00327         }
00328 
00329         /* returns queryID or false */
00330         function _query($sql,$inputarr=false) 
00331         {
00332                 if (is_array($sql)) {
00333                         $stmt = $sql[1];
00334                 } else {
00335                         $stmt = $this->_connectionID->prepare($sql);
00336                 }
00337                 
00338                 if ($stmt) {
00339                         $this->_driver->debug = $this->debug;
00340                         if ($inputarr) $ok = $stmt->execute($inputarr);
00341                         else $ok = $stmt->execute();
00342                 } 
00343                 
00344                 
00345                 $this->_errormsg = false;
00346                 $this->_errorno = false;
00347                         
00348                 if ($ok) {
00349                         $this->_stmt = $stmt;
00350                         return $stmt;
00351                 }
00352                 
00353                 if ($stmt) {
00354                         
00355                         $arr = $stmt->errorinfo();
00356                         if ((integer)$arr[1]) {
00357                                 $this->_errormsg = $arr[2];
00358                                 $this->_errorno = $arr[1];
00359                         }
00360 
00361                 } else {
00362                         $this->_errormsg = false;
00363                         $this->_errorno = false;
00364                 }
00365                 return false;
00366         }
00367 
00368         // returns true or false
00369         function _close()
00370         {
00371                 $this->_stmt = false;
00372                 return true;
00373         }
00374 
00375         function _affectedrows()
00376         {
00377                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00378         }
00379         
00380         function _insertid()
00381         {
00382                 return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
00383         }
00384 }
00385 
00386 class ADOPDOStatement {
00387 
00388         var $databaseType = "pdo";              
00389         var $dataProvider = "pdo";
00390         var $_stmt;
00391         var $_connectionID;
00392         
00393         function ADOPDOStatement($stmt,$connection)
00394         {
00395                 $this->_stmt = $stmt;
00396                 $this->_connectionID = $connection;
00397         }
00398         
00399         function Execute($inputArr=false)
00400         {
00401                 $savestmt = $this->_connectionID->_stmt;
00402                 $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
00403                 $this->_connectionID->_stmt = $savestmt;
00404                 return $rs;
00405         }
00406         
00407         function InParameter(&$var,$name,$maxLen=4000,$type=false)
00408         {
00409 
00410                 if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
00411                 else $this->_stmt->bindParam($name, $var);
00412         }
00413         
00414         function Affected_Rows()
00415         {
00416                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00417         }
00418         
00419         function ErrorMsg()
00420         {
00421                 if ($this->_stmt) $arr = $this->_stmt->errorInfo();
00422                 else $arr = $this->_connectionID->errorInfo();
00423 
00424                 if (is_array($arr)) {
00425                         if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
00426                         else return '';
00427                 } else return '-1';
00428         }
00429         
00430         function NumCols()
00431         {
00432                 return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
00433         }
00434         
00435         function ErrorNo()
00436         {
00437                 if ($this->_stmt) return $this->_stmt->errorCode();
00438                 else return $this->_connectionID->errorInfo();
00439         }
00440 }
00441 
00442 /*--------------------------------------------------------------------------------------
00443          Class Name: Recordset
00444 --------------------------------------------------------------------------------------*/
00445 
00446 class ADORecordSet_pdo extends ADORecordSet {   
00447         
00448         var $bind = false;
00449         var $databaseType = "pdo";              
00450         var $dataProvider = "pdo";
00451         
00452         function ADORecordSet_pdo($id,$mode=false)
00453         {
00454                 if ($mode === false) {  
00455                         global $ADODB_FETCH_MODE;
00456                         $mode = $ADODB_FETCH_MODE;
00457                 }
00458                 $this->adodbFetchMode = $mode;
00459                 switch($mode) {
00460                 case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
00461                 case ADODB_FETCH_ASSOC:  $mode = PDO::FETCH_ASSOC; break;
00462                 
00463                 case ADODB_FETCH_BOTH: 
00464                 default: $mode = PDO::FETCH_BOTH; break;
00465                 }
00466                 $this->fetchMode = $mode;
00467                 
00468                 $this->_queryID = $id;
00469                 $this->ADORecordSet($id);
00470         }
00471 
00472         
00473         function Init()
00474         {
00475                 if ($this->_inited) return;
00476                 $this->_inited = true;
00477                 if ($this->_queryID) @$this->_initrs();
00478                 else {
00479                         $this->_numOfRows = 0;
00480                         $this->_numOfFields = 0;
00481                 }
00482                 if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
00483                         $this->_currentRow = 0;
00484                         if ($this->EOF = ($this->_fetch() === false)) {
00485                                 $this->_numOfRows = 0; // _numOfRows could be -1
00486                         }
00487                 } else {
00488                         $this->EOF = true;
00489                 }
00490         }
00491         
00492         function _initrs()
00493         {
00494         global $ADODB_COUNTRECS;
00495         
00496                 $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
00497                 if (!$this->_numOfRows) $this->_numOfRows = -1;
00498                 $this->_numOfFields = $this->_queryID->columnCount();
00499         }
00500 
00501         // returns the field object
00502         function &FetchField($fieldOffset = -1) 
00503         {
00504                 $off=$fieldOffset+1; // offsets begin at 1
00505                 
00506                 $o= new ADOFieldObject();
00507                 $arr = @$this->_queryID->getColumnMeta($fieldOffset);
00508                 if (!$arr) {
00509                         $o->name = 'bad getColumnMeta()';
00510                         $o->max_length = -1;
00511                         $o->type = 'VARCHAR';
00512                         $o->precision = 0;
00513         #               $false = false;
00514                         return $o;
00515                 }
00516                 //adodb_pr($arr);
00517                 $o->name = $arr['name'];
00518                 if (isset($arr['native_type'])) $o->type = $arr['native_type'];
00519                 else $o->type = adodb_pdo_type($arr['pdo_type']);
00520                 $o->max_length = $arr['len'];
00521                 $o->precision = $arr['precision'];
00522                 
00523                 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
00524                 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
00525                 return $o;
00526         }
00527         
00528         function _seek($row)
00529         {
00530                 return false;
00531         }
00532         
00533         function _fetch()
00534         {
00535                 if (!$this->_queryID) return false;
00536                 
00537                 $this->fields = $this->_queryID->fetch($this->fetchMode);
00538                 return !empty($this->fields);
00539         }
00540         
00541         function _close() 
00542         {
00543                 $this->_queryID = false;
00544         }
00545         
00546         function Fields($colname)
00547         {
00548                 if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
00549                 
00550                 if (!$this->bind) {
00551                         $this->bind = array();
00552                         for ($i=0; $i < $this->_numOfFields; $i++) {
00553                                 $o = $this->FetchField($i);
00554                                 $this->bind[strtoupper($o->name)] = $i;
00555                         }
00556                 }
00557                  return $this->fields[$this->bind[strtoupper($colname)]];
00558         }
00559 
00560 }
00561 
00562 ?>


Généré par Les experts TYPO3 avec  doxygen 1.4.6