Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/adodb/drivers/adodb-pdo.inc.php
Go to the documentation of this file.
00001 <?php
00002 /* 
00003 V5.14 8 Sept 2011  (c) 2000-2011 John Lim (jlim#natsoft.com). 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 
00068 
00069 
00070 class ADODB_pdo extends ADOConnection {
00071         var $databaseType = "pdo";      
00072         var $dataProvider = "pdo";
00073         var $fmtDate = "'Y-m-d'";
00074         var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
00075         var $replaceQuote = "''"; // string to use to replace quotes
00076         var $hasAffectedRows = true;
00077         var $_bindInputArray = true;    
00078         var $_genSeqSQL = "create table %s (id integer)";
00079         var $_autocommit = true;
00080         var $_haserrorfunctions = true;
00081         var $_lastAffectedRows = 0;
00082         
00083         var $_errormsg = false;
00084         var $_errorno = false;
00085         
00086         var $dsnType = '';
00087         var $stmt = false;
00088         
00089         function ADODB_pdo()
00090         {
00091         }
00092         
00093         function _UpdatePDO()
00094         {
00095                 $d = $this->_driver;
00096                 $this->fmtDate = $d->fmtDate;
00097                 $this->fmtTimeStamp = $d->fmtTimeStamp;
00098                 $this->replaceQuote = $d->replaceQuote;
00099                 $this->sysDate = $d->sysDate;
00100                 $this->sysTimeStamp = $d->sysTimeStamp;
00101                 $this->random = $d->random;
00102                 $this->concat_operator = $d->concat_operator;
00103                 $this->nameQuote = $d->nameQuote;
00104                                 
00105                 $this->hasGenID = $d->hasGenID;
00106                 $this->_genIDSQL = $d->_genIDSQL;
00107                 $this->_genSeqSQL = $d->_genSeqSQL;
00108                 $this->_dropSeqSQL = $d->_dropSeqSQL;
00109 
00110                 $d->_init($this);
00111         }
00112         
00113         function Time()
00114         {
00115                 if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
00116                 else $sql = "select $this->sysTimeStamp";
00117                 
00118                 $rs = $this->_Execute($sql);
00119                 if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
00120                 
00121                 return false;
00122         }
00123         
00124         // returns true or false
00125         function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
00126         {
00127                 $at = strpos($argDSN,':');
00128                 $this->dsnType = substr($argDSN,0,$at);
00129 
00130                 if ($argDatabasename) {
00131                         $argDSN .= ';dbname='.$argDatabasename;
00132                 }
00133                 try {
00134                         $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
00135                 } catch (Exception $e) {
00136                         $this->_connectionID = false;
00137                         $this->_errorno = -1;
00138                         //var_dump($e);
00139                         $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
00140                         return false;
00141                 }
00142                 
00143                 if ($this->_connectionID) {
00144                         switch(ADODB_ASSOC_CASE){
00145                         case 0: $m = PDO::CASE_LOWER; break;
00146                         case 1: $m = PDO::CASE_UPPER; break;
00147                         default:
00148                         case 2: $m = PDO::CASE_NATURAL; break;
00149                         }
00150                         
00151                         //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
00152                         $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
00153                         
00154                         $class = 'ADODB_pdo_'.$this->dsnType;
00155                         //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00156                         switch($this->dsnType) {
00157                         case 'oci':
00158                         case 'mysql':
00159                         case 'pgsql':
00160                         case 'mssql':
00161                         case 'sqlite':
00162                                 include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
00163                                 break;
00164                         }
00165                         if (class_exists($class))
00166                                 $this->_driver = new $class();
00167                         else
00168                                 $this->_driver = new ADODB_pdo_base();
00169                         
00170                         $this->_driver->_connectionID = $this->_connectionID;
00171                         $this->_UpdatePDO();
00172                         return true;
00173                 }
00174                 $this->_driver = new ADODB_pdo_base();
00175                 return false;
00176         }
00177         
00178         function Concat() 
00179         {
00180                 $args = func_get_args();
00181                 if(method_exists($this->_driver, 'Concat')) 
00182                         return call_user_func_array(array($this->_driver, 'Concat'), $args); 
00183                 
00184                 return call_user_func_array(array($this,'parent::Concat'), $args); 
00185         }
00186         
00187         // returns true or false
00188         function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
00189         {
00190                 return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
00191         }
00192         
00193         /*------------------------------------------------------------------------------*/
00194         
00195         
00196         function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
00197         {       
00198                 $save = $this->_driver->fetchMode;
00199                 $this->_driver->fetchMode = $this->fetchMode;
00200                 $this->_driver->debug = $this->debug;
00201                 $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
00202                 $this->_driver->fetchMode = $save;
00203                 return $ret;
00204         }
00205         
00206         
00207         function ServerInfo()
00208         {
00209                 return $this->_driver->ServerInfo();
00210         }
00211         
00212         function MetaTables($ttype=false,$showSchema=false,$mask=false)
00213         {
00214                 return $this->_driver->MetaTables($ttype,$showSchema,$mask);
00215         }
00216         
00217         function MetaColumns($table,$normalize=true)
00218         {
00219                 return $this->_driver->MetaColumns($table,$normalize);
00220         }
00221         
00222         function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
00223         {
00224                 $obj = $stmt[1];
00225                 if ($type) $obj->bindParam($name,$var,$type,$maxLen);
00226                 else $obj->bindParam($name, $var);
00227         }
00228         
00229         function OffsetDate($dayFraction,$date=false)
00230     {   
00231         return $this->_driver->OffsetDate($dayFraction,$date);
00232     }
00233         
00234         function ErrorMsg()
00235         {
00236                 if ($this->_errormsg !== false) return $this->_errormsg;
00237                 if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo();
00238                 else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo();
00239                 else return 'No Connection Established';
00240                 
00241                 
00242                 if ($arr) {
00243                         if (sizeof($arr)<2) return '';
00244                         if ((integer)$arr[1]) return $arr[2];
00245                         else return '';
00246                 } else return '-1';
00247         }
00248         
00249 
00250         function ErrorNo()
00251         {
00252                 if ($this->_errorno !== false) return $this->_errorno;
00253                 if (!empty($this->_stmt)) $err = $this->_stmt->errorCode();
00254                 else if (!empty($this->_connectionID)) {
00255                         $arr = $this->_connectionID->errorInfo();
00256                         if (isset($arr[0])) $err = $arr[0];
00257                         else $err = -1;
00258                 } else
00259                         return 0;
00260                         
00261                 if ($err == '00000') return 0; // allows empty check
00262                 return $err;
00263         }
00264 
00265         function SetTransactionMode($transaction_mode) 
00266         {
00267                 if(method_exists($this->_driver, 'SetTransactionMode')) 
00268                         return $this->_driver->SetTransactionMode($transaction_mode); 
00269                 
00270                 return parent::SetTransactionMode($seqname); 
00271         }
00272 
00273         function BeginTrans()
00274         {       
00275                 if(method_exists($this->_driver, 'BeginTrans')) 
00276                         return $this->_driver->BeginTrans(); 
00277                 
00278                 if (!$this->hasTransactions) return false;
00279                 if ($this->transOff) return true; 
00280                 $this->transCnt += 1;
00281                 $this->_autocommit = false;
00282                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
00283                 return $this->_connectionID->beginTransaction();
00284         }
00285         
00286         function CommitTrans($ok=true) 
00287         { 
00288                 if(method_exists($this->_driver, 'CommitTrans')) 
00289                         return $this->_driver->CommitTrans($ok); 
00290                 
00291                 if (!$this->hasTransactions) return false;
00292                 if ($this->transOff) return true; 
00293                 if (!$ok) return $this->RollbackTrans();
00294                 if ($this->transCnt) $this->transCnt -= 1;
00295                 $this->_autocommit = true;
00296                 
00297                 $ret = $this->_connectionID->commit();
00298                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00299                 return $ret;
00300         }
00301         
00302         function RollbackTrans()
00303         {
00304                 if(method_exists($this->_driver, 'RollbackTrans')) 
00305                         return $this->_driver->RollbackTrans(); 
00306                 
00307                 if (!$this->hasTransactions) return false;
00308                 if ($this->transOff) return true; 
00309                 if ($this->transCnt) $this->transCnt -= 1;
00310                 $this->_autocommit = true;
00311                 
00312                 $ret = $this->_connectionID->rollback();
00313                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
00314                 return $ret;
00315         }
00316         
00317         function Prepare($sql)
00318         {
00319                 $this->_stmt = $this->_connectionID->prepare($sql);
00320                 if ($this->_stmt) return array($sql,$this->_stmt);
00321                 
00322                 return false;
00323         }
00324         
00325         function PrepareStmt($sql)
00326         {
00327                 $stmt = $this->_connectionID->prepare($sql);
00328                 if (!$stmt) return false;
00329                 $obj = new ADOPDOStatement($stmt,$this);
00330                 return $obj;
00331         }
00332         
00333         function CreateSequence($seqname='adodbseq',$startID=1)
00334         {
00335                 if(method_exists($this->_driver, 'CreateSequence')) 
00336                         return $this->_driver->CreateSequence($seqname, $startID); 
00337                 
00338                 return parent::CreateSequence($seqname, $startID); 
00339         }
00340         
00341         function DropSequence($seqname='adodbseq')
00342         {
00343                 if(method_exists($this->_driver, 'DropSequence')) 
00344                         return $this->_driver->DropSequence($seqname); 
00345                 
00346                 return parent::DropSequence($seqname); 
00347         }
00348 
00349         function GenID($seqname='adodbseq',$startID=1)
00350         {
00351                 if(method_exists($this->_driver, 'GenID')) 
00352                         return $this->_driver->GenID($seqname, $startID); 
00353                 
00354                 return parent::GenID($seqname, $startID); 
00355         }
00356 
00357         
00358         /* returns queryID or false */
00359         function _query($sql,$inputarr=false) 
00360         {
00361                 if (is_array($sql)) {
00362                         $stmt = $sql[1];
00363                 } else {
00364                         $stmt = $this->_connectionID->prepare($sql);
00365                 }
00366                 #adodb_backtrace();
00367                 #var_dump($this->_bindInputArray);
00368                 if ($stmt) {
00369                         $this->_driver->debug = $this->debug;
00370                         if ($inputarr) $ok = $stmt->execute($inputarr);
00371                         else $ok = $stmt->execute();
00372                 } 
00373                 
00374                 
00375                 $this->_errormsg = false;
00376                 $this->_errorno = false;
00377                         
00378                 if ($ok) {
00379                         $this->_stmt = $stmt;
00380                         return $stmt;
00381                 }
00382                 
00383                 if ($stmt) {
00384                         
00385                         $arr = $stmt->errorinfo();
00386                         if ((integer)$arr[1]) {
00387                                 $this->_errormsg = $arr[2];
00388                                 $this->_errorno = $arr[1];
00389                         }
00390 
00391                 } else {
00392                         $this->_errormsg = false;
00393                         $this->_errorno = false;
00394                 }
00395                 return false;
00396         }
00397 
00398         // returns true or false
00399         function _close()
00400         {
00401                 $this->_stmt = false;
00402                 return true;
00403         }
00404 
00405         function _affectedrows()
00406         {
00407                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00408         }
00409         
00410         function _insertid()
00411         {
00412                 return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
00413         }
00414 }
00415 
00416 class ADODB_pdo_base extends ADODB_pdo {
00417 
00418         var $sysDate = "'?'";
00419         var $sysTimeStamp = "'?'";
00420         
00421 
00422         function _init($parentDriver)
00423         {
00424                 $parentDriver->_bindInputArray = true;
00425                 #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
00426         }
00427         
00428         function ServerInfo()
00429         {
00430                 return ADOConnection::ServerInfo();
00431         }
00432         
00433         function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
00434         {
00435                 $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
00436                 return $ret;
00437         }
00438         
00439         function MetaTables()
00440         {
00441                 return false;
00442         }
00443         
00444         function MetaColumns()
00445         {
00446                 return false;
00447         }
00448 }
00449 
00450 class ADOPDOStatement {
00451 
00452         var $databaseType = "pdo";              
00453         var $dataProvider = "pdo";
00454         var $_stmt;
00455         var $_connectionID;
00456         
00457         function ADOPDOStatement($stmt,$connection)
00458         {
00459                 $this->_stmt = $stmt;
00460                 $this->_connectionID = $connection;
00461         }
00462         
00463         function Execute($inputArr=false)
00464         {
00465                 $savestmt = $this->_connectionID->_stmt;
00466                 $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
00467                 $this->_connectionID->_stmt = $savestmt;
00468                 return $rs;
00469         }
00470         
00471         function InParameter(&$var,$name,$maxLen=4000,$type=false)
00472         {
00473 
00474                 if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
00475                 else $this->_stmt->bindParam($name, $var);
00476         }
00477         
00478         function Affected_Rows()
00479         {
00480                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
00481         }
00482         
00483         function ErrorMsg()
00484         {
00485                 if ($this->_stmt) $arr = $this->_stmt->errorInfo();
00486                 else $arr = $this->_connectionID->errorInfo();
00487 
00488                 if (is_array($arr)) {
00489                         if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
00490                         else return '';
00491                 } else return '-1';
00492         }
00493         
00494         function NumCols()
00495         {
00496                 return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
00497         }
00498         
00499         function ErrorNo()
00500         {
00501                 if ($this->_stmt) return $this->_stmt->errorCode();
00502                 else return $this->_connectionID->errorInfo();
00503         }
00504 }
00505 
00506 /*--------------------------------------------------------------------------------------
00507          Class Name: Recordset
00508 --------------------------------------------------------------------------------------*/
00509 
00510 class ADORecordSet_pdo extends ADORecordSet {   
00511         
00512         var $bind = false;
00513         var $databaseType = "pdo";              
00514         var $dataProvider = "pdo";
00515         
00516         function ADORecordSet_pdo($id,$mode=false)
00517         {
00518                 if ($mode === false) {  
00519                         global $ADODB_FETCH_MODE;
00520                         $mode = $ADODB_FETCH_MODE;
00521                 }
00522                 $this->adodbFetchMode = $mode;
00523                 switch($mode) {
00524                 case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
00525                 case ADODB_FETCH_ASSOC:  $mode = PDO::FETCH_ASSOC; break;
00526                 
00527                 case ADODB_FETCH_BOTH: 
00528                 default: $mode = PDO::FETCH_BOTH; break;
00529                 }
00530                 $this->fetchMode = $mode;
00531                 
00532                 $this->_queryID = $id;
00533                 $this->ADORecordSet($id);
00534         }
00535 
00536         
00537         function Init()
00538         {
00539                 if ($this->_inited) return;
00540                 $this->_inited = true;
00541                 if ($this->_queryID) @$this->_initrs();
00542                 else {
00543                         $this->_numOfRows = 0;
00544                         $this->_numOfFields = 0;
00545                 }
00546                 if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
00547                         $this->_currentRow = 0;
00548                         if ($this->EOF = ($this->_fetch() === false)) {
00549                                 $this->_numOfRows = 0; // _numOfRows could be -1
00550                         }
00551                 } else {
00552                         $this->EOF = true;
00553                 }
00554         }
00555         
00556         function _initrs()
00557         {
00558         global $ADODB_COUNTRECS;
00559         
00560                 $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
00561                 if (!$this->_numOfRows) $this->_numOfRows = -1;
00562                 $this->_numOfFields = $this->_queryID->columnCount();
00563         }
00564 
00565         // returns the field object
00566         function FetchField($fieldOffset = -1) 
00567         {
00568                 $off=$fieldOffset+1; // offsets begin at 1
00569                 
00570                 $o= new ADOFieldObject();
00571                 $arr = @$this->_queryID->getColumnMeta($fieldOffset);
00572                 if (!$arr) {
00573                         $o->name = 'bad getColumnMeta()';
00574                         $o->max_length = -1;
00575                         $o->type = 'VARCHAR';
00576                         $o->precision = 0;
00577         #               $false = false;
00578                         return $o;
00579                 }
00580                 //adodb_pr($arr);
00581                 $o->name = $arr['name'];
00582                 if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type'];
00583                 else $o->type = adodb_pdo_type($arr['pdo_type']);
00584                 $o->max_length = $arr['len'];
00585                 $o->precision = $arr['precision'];
00586                 
00587                 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
00588                 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
00589                 return $o;
00590         }
00591         
00592         function _seek($row)
00593         {
00594                 return false;
00595         }
00596         
00597         function _fetch()
00598         {
00599                 if (!$this->_queryID) return false;
00600                 
00601                 $this->fields = $this->_queryID->fetch($this->fetchMode);
00602                 return !empty($this->fields);
00603         }
00604         
00605         function _close() 
00606         {
00607                 $this->_queryID = false;
00608         }
00609         
00610         function Fields($colname)
00611         {
00612                 if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
00613                 
00614                 if (!$this->bind) {
00615                         $this->bind = array();
00616                         for ($i=0; $i < $this->_numOfFields; $i++) {
00617                                 $o = $this->FetchField($i);
00618                                 $this->bind[strtoupper($o->name)] = $i;
00619                         }
00620                 }
00621                  return $this->fields[$this->bind[strtoupper($colname)]];
00622         }
00623 
00624 }
00625 
00626 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations