Documentation TYPO3 par Ameos

adodb-pdo.inc.php

00001 <?php
00002 /* 
00003 V4.93 10 Oct 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         
00330         /* returns queryID or false */
00331         function _query($sql,$inputarr=false) 
00332         {
00333                 if (is_array($sql)) {
00334                         $stmt = $sql[1];
00335                 } else {
00336                         $stmt = $this->_connectionID->prepare($sql);
00337                 }
00338                 #adodb_backtrace();
00339                 #var_dump($this->_bindInputArray);
00340                 if ($stmt) {
00341                         $this->_driver->debug = $this->debug;
00342                         if ($inputarr) $ok = $stmt->execute($inputarr);
00343                         else $ok = $stmt->execute();
00344                 } 
00345                 
00346                 
00347                 $this->_errormsg = false;
00348                 $this->_errorno = false;
00349                         
00350                 if ($ok) {
00351                         $this->_stmt = $stmt;
00352                         return $stmt;
00353                 }
00354                 
00355                 if ($stmt) {
00356                         
00357                         $arr = $stmt->errorinfo();
00358                         if ((integer)$arr[1]) {
00359                                 $this->_errormsg = $arr[2];
00360                                 $this->_errorno = $arr[1];
00361                         }
00362 
00363                 } else {
00364                         $this->_errormsg = false;
00365                         $this->_errorno = false;
00366                 }
00367                 return false;
00368         }
00369 
00370         // returns true or false
00371         function _close()
00372         {
00373                 $this->_stmt = false;
00374                 return true;
00375         }
00376 
00377         function _affectedrows()
00378         {
00379                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00380         }
00381         
00382         function _insertid()
00383         {
00384                 return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
00385         }
00386 }
00387 
00388 class ADOPDOStatement {
00389 
00390         var $databaseType = "pdo";              
00391         var $dataProvider = "pdo";
00392         var $_stmt;
00393         var $_connectionID;
00394         
00395         function ADOPDOStatement($stmt,$connection)
00396         {
00397                 $this->_stmt = $stmt;
00398                 $this->_connectionID = $connection;
00399         }
00400         
00401         function Execute($inputArr=false)
00402         {
00403                 $savestmt = $this->_connectionID->_stmt;
00404                 $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
00405                 $this->_connectionID->_stmt = $savestmt;
00406                 return $rs;
00407         }
00408         
00409         function InParameter(&$var,$name,$maxLen=4000,$type=false)
00410         {
00411 
00412                 if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
00413                 else $this->_stmt->bindParam($name, $var);
00414         }
00415         
00416         function Affected_Rows()
00417         {
00418                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00419         }
00420         
00421         function ErrorMsg()
00422         {
00423                 if ($this->_stmt) $arr = $this->_stmt->errorInfo();
00424                 else $arr = $this->_connectionID->errorInfo();
00425 
00426                 if (is_array($arr)) {
00427                         if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
00428                         else return '';
00429                 } else return '-1';
00430         }
00431         
00432         function NumCols()
00433         {
00434                 return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
00435         }
00436         
00437         function ErrorNo()
00438         {
00439                 if ($this->_stmt) return $this->_stmt->errorCode();
00440                 else return $this->_connectionID->errorInfo();
00441         }
00442 }
00443 
00444 /*--------------------------------------------------------------------------------------
00445          Class Name: Recordset
00446 --------------------------------------------------------------------------------------*/
00447 
00448 class ADORecordSet_pdo extends ADORecordSet {   
00449         
00450         var $bind = false;
00451         var $databaseType = "pdo";              
00452         var $dataProvider = "pdo";
00453         
00454         function ADORecordSet_pdo($id,$mode=false)
00455         {
00456                 if ($mode === false) {  
00457                         global $ADODB_FETCH_MODE;
00458                         $mode = $ADODB_FETCH_MODE;
00459                 }
00460                 $this->adodbFetchMode = $mode;
00461                 switch($mode) {
00462                 case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
00463                 case ADODB_FETCH_ASSOC:  $mode = PDO::FETCH_ASSOC; break;
00464                 
00465                 case ADODB_FETCH_BOTH: 
00466                 default: $mode = PDO::FETCH_BOTH; break;
00467                 }
00468                 $this->fetchMode = $mode;
00469                 
00470                 $this->_queryID = $id;
00471                 $this->ADORecordSet($id);
00472         }
00473 
00474         
00475         function Init()
00476         {
00477                 if ($this->_inited) return;
00478                 $this->_inited = true;
00479                 if ($this->_queryID) @$this->_initrs();
00480                 else {
00481                         $this->_numOfRows = 0;
00482                         $this->_numOfFields = 0;
00483                 }
00484                 if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
00485                         $this->_currentRow = 0;
00486                         if ($this->EOF = ($this->_fetch() === false)) {
00487                                 $this->_numOfRows = 0; // _numOfRows could be -1
00488                         }
00489                 } else {
00490                         $this->EOF = true;
00491                 }
00492         }
00493         
00494         function _initrs()
00495         {
00496         global $ADODB_COUNTRECS;
00497         
00498                 $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
00499                 if (!$this->_numOfRows) $this->_numOfRows = -1;
00500                 $this->_numOfFields = $this->_queryID->columnCount();
00501         }
00502 
00503         // returns the field object
00504         function &FetchField($fieldOffset = -1) 
00505         {
00506                 $off=$fieldOffset+1; // offsets begin at 1
00507                 
00508                 $o= new ADOFieldObject();
00509                 $arr = @$this->_queryID->getColumnMeta($fieldOffset);
00510                 if (!$arr) {
00511                         $o->name = 'bad getColumnMeta()';
00512                         $o->max_length = -1;
00513                         $o->type = 'VARCHAR';
00514                         $o->precision = 0;
00515         #               $false = false;
00516                         return $o;
00517                 }
00518                 //adodb_pr($arr);
00519                 $o->name = $arr['name'];
00520                 if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type'];
00521                 else $o->type = adodb_pdo_type($arr['pdo_type']);
00522                 $o->max_length = $arr['len'];
00523                 $o->precision = $arr['precision'];
00524                 
00525                 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
00526                 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
00527                 return $o;
00528         }
00529         
00530         function _seek($row)
00531         {
00532                 return false;
00533         }
00534         
00535         function _fetch()
00536         {
00537                 if (!$this->_queryID) return false;
00538                 
00539                 $this->fields = $this->_queryID->fetch($this->fetchMode);
00540                 return !empty($this->fields);
00541         }
00542         
00543         function _close() 
00544         {
00545                 $this->_queryID = false;
00546         }
00547         
00548         function Fields($colname)
00549         {
00550                 if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
00551                 
00552                 if (!$this->bind) {
00553                         $this->bind = array();
00554                         for ($i=0; $i < $this->_numOfFields; $i++) {
00555                                 $o = $this->FetchField($i);
00556                                 $this->bind[strtoupper($o->name)] = $i;
00557                         }
00558                 }
00559                  return $this->fields[$this->bind[strtoupper($colname)]];
00560         }
00561 
00562 }
00563 
00564 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6