[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/adodb/drivers/ -> adodb-pdo.inc.php (source)

   1  <?php
   2  /**
   3      @version   v5.20.3  01-Jan-2016
   4      @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   5      @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   6  
   7      Released under both BSD license and Lesser GPL library license.
   8      Whenever there is any discrepancy between the two licenses,
   9      the BSD license will take precedence.
  10  
  11      Set tabs to 4 for best viewing.
  12  
  13      Latest version is available at http://adodb.sourceforge.net
  14  
  15      Requires ODBC. Works on Windows and Unix.
  16  
  17      Problems:
  18          Where is float/decimal type in pdo_param_type
  19          LOB handling for CLOB/BLOB differs significantly
  20  */
  21  
  22  // security - hide paths
  23  if (!defined('ADODB_DIR')) die();
  24  
  25  
  26  /*
  27  enum pdo_param_type {
  28  PDO::PARAM_NULL, 0
  29  
  30  /* int as in long (the php native int type).
  31   * If you mark a column as an int, PDO expects get_col to return
  32   * a pointer to a long
  33  PDO::PARAM_INT, 1
  34  
  35  /* get_col ptr should point to start of the string buffer
  36  PDO::PARAM_STR, 2
  37  
  38  /* get_col: when len is 0 ptr should point to a php_stream *,
  39   * otherwise it should behave like a string. Indicate a NULL field
  40   * value by setting the ptr to NULL
  41  PDO::PARAM_LOB, 3
  42  
  43  /* get_col: will expect the ptr to point to a new PDOStatement object handle,
  44   * but this isn't wired up yet
  45  PDO::PARAM_STMT, 4 /* hierarchical result set
  46  
  47  /* get_col ptr should point to a zend_bool
  48  PDO::PARAM_BOOL, 5
  49  
  50  
  51  /* magic flag to denote a parameter as being input/output
  52  PDO::PARAM_INPUT_OUTPUT = 0x80000000
  53  };
  54  */
  55  
  56  function adodb_pdo_type($t)
  57  {
  58      switch($t) {
  59      case 2: return 'VARCHAR';
  60      case 3: return 'BLOB';
  61      default: return 'NUMERIC';
  62      }
  63  }
  64  
  65  /*----------------------------------------------------------------------------*/
  66  
  67  
  68  class ADODB_pdo extends ADOConnection {
  69      var $databaseType = "pdo";
  70      var $dataProvider = "pdo";
  71      var $fmtDate = "'Y-m-d'";
  72      var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  73      var $replaceQuote = "''"; // string to use to replace quotes
  74      var $hasAffectedRows = true;
  75      var $_bindInputArray = true;
  76      var $_genIDSQL;
  77      var $_genSeqSQL = "create table %s (id integer)";
  78      var $_dropSeqSQL;
  79      var $_autocommit = true;
  80      var $_haserrorfunctions = true;
  81      var $_lastAffectedRows = 0;
  82  
  83      var $_errormsg = false;
  84      var $_errorno = false;
  85  
  86      var $dsnType = '';
  87      var $stmt = false;
  88      var $_driver;
  89  
  90  	function __construct()
  91      {
  92      }
  93  
  94  	function _UpdatePDO()
  95      {
  96          $d = $this->_driver;
  97          $this->fmtDate = $d->fmtDate;
  98          $this->fmtTimeStamp = $d->fmtTimeStamp;
  99          $this->replaceQuote = $d->replaceQuote;
 100          $this->sysDate = $d->sysDate;
 101          $this->sysTimeStamp = $d->sysTimeStamp;
 102          $this->random = $d->random;
 103          $this->concat_operator = $d->concat_operator;
 104          $this->nameQuote = $d->nameQuote;
 105  
 106          $this->hasGenID = $d->hasGenID;
 107          $this->_genIDSQL = $d->_genIDSQL;
 108          $this->_genSeqSQL = $d->_genSeqSQL;
 109          $this->_dropSeqSQL = $d->_dropSeqSQL;
 110  
 111          $d->_init($this);
 112      }
 113  
 114  	function Time()
 115      {
 116          if (!empty($this->_driver->_hasdual)) {
 117              $sql = "select $this->sysTimeStamp from dual";
 118          }
 119          else {
 120              $sql = "select $this->sysTimeStamp";
 121          }
 122  
 123          $rs = $this->_Execute($sql);
 124          if ($rs && !$rs->EOF) {
 125              return $this->UnixTimeStamp(reset($rs->fields));
 126          }
 127  
 128          return false;
 129      }
 130  
 131      // returns true or false
 132  	function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
 133      {
 134          $at = strpos($argDSN,':');
 135          $this->dsnType = substr($argDSN,0,$at);
 136  
 137          if ($argDatabasename) {
 138              switch($this->dsnType){
 139                  case 'sqlsrv':
 140                      $argDSN .= ';database='.$argDatabasename;
 141                      break;
 142                  case 'mssql':
 143                  case 'mysql':
 144                  case 'oci':
 145                  case 'pgsql':
 146                  case 'sqlite':
 147                  default:
 148                      $argDSN .= ';dbname='.$argDatabasename;
 149              }
 150          }
 151          try {
 152              $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
 153          } catch (Exception $e) {
 154              $this->_connectionID = false;
 155              $this->_errorno = -1;
 156              //var_dump($e);
 157              $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
 158              return false;
 159          }
 160  
 161          if ($this->_connectionID) {
 162              switch(ADODB_ASSOC_CASE){
 163                  case ADODB_ASSOC_CASE_LOWER:
 164                      $m = PDO::CASE_LOWER;
 165                      break;
 166                  case ADODB_ASSOC_CASE_UPPER:
 167                      $m = PDO::CASE_UPPER;
 168                      break;
 169                  default:
 170                  case ADODB_ASSOC_CASE_NATIVE:
 171                      $m = PDO::CASE_NATURAL;
 172                      break;
 173              }
 174  
 175              //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
 176              $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
 177  
 178              $class = 'ADODB_pdo_'.$this->dsnType;
 179              //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
 180              switch($this->dsnType) {
 181                  case 'mssql':
 182                  case 'mysql':
 183                  case 'oci':
 184                  case 'pgsql':
 185                  case 'sqlite':
 186                  case 'sqlsrv':
 187                      include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
 188                      break;
 189              }
 190              if (class_exists($class)) {
 191                  $this->_driver = new $class();
 192              }
 193              else {
 194                  $this->_driver = new ADODB_pdo_base();
 195              }
 196  
 197              $this->_driver->_connectionID = $this->_connectionID;
 198              $this->_UpdatePDO();
 199              return true;
 200          }
 201          $this->_driver = new ADODB_pdo_base();
 202          return false;
 203      }
 204  
 205  	function Concat()
 206      {
 207          $args = func_get_args();
 208          if(method_exists($this->_driver, 'Concat')) {
 209              return call_user_func_array(array($this->_driver, 'Concat'), $args);
 210          }
 211  
 212          if (PHP_VERSION >= 5.3) {
 213              return call_user_func_array('parent::Concat', $args);
 214          }
 215          return call_user_func_array(array($this,'parent::Concat'), $args);
 216      }
 217  
 218      // returns true or false
 219  	function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
 220      {
 221          return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
 222      }
 223  
 224      /*------------------------------------------------------------------------------*/
 225  
 226  
 227  	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
 228      {
 229          $save = $this->_driver->fetchMode;
 230          $this->_driver->fetchMode = $this->fetchMode;
 231          $this->_driver->debug = $this->debug;
 232          $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
 233          $this->_driver->fetchMode = $save;
 234          return $ret;
 235      }
 236  
 237  
 238  	function ServerInfo()
 239      {
 240          return $this->_driver->ServerInfo();
 241      }
 242  
 243  	function MetaTables($ttype=false,$showSchema=false,$mask=false)
 244      {
 245          return $this->_driver->MetaTables($ttype,$showSchema,$mask);
 246      }
 247  
 248  	function MetaColumns($table,$normalize=true)
 249      {
 250          return $this->_driver->MetaColumns($table,$normalize);
 251      }
 252  
 253  	function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
 254      {
 255          $obj = $stmt[1];
 256          if ($type) {
 257              $obj->bindParam($name, $var, $type, $maxLen);
 258          }
 259          else {
 260              $obj->bindParam($name, $var);
 261          }
 262      }
 263  
 264  	function OffsetDate($dayFraction,$date=false)
 265      {
 266          return $this->_driver->OffsetDate($dayFraction,$date);
 267      }
 268  
 269  	function ErrorMsg()
 270      {
 271          if ($this->_errormsg !== false) {
 272              return $this->_errormsg;
 273          }
 274          if (!empty($this->_stmt)) {
 275              $arr = $this->_stmt->errorInfo();
 276          }
 277          else if (!empty($this->_connectionID)) {
 278              $arr = $this->_connectionID->errorInfo();
 279          }
 280          else {
 281              return 'No Connection Established';
 282          }
 283  
 284          if ($arr) {
 285              if (sizeof($arr)<2) {
 286                  return '';
 287              }
 288              if ((integer)$arr[0]) {
 289                  return $arr[2];
 290              }
 291              else {
 292                  return '';
 293              }
 294          }
 295          else {
 296              return '-1';
 297          }
 298      }
 299  
 300  
 301  	function ErrorNo()
 302      {
 303          if ($this->_errorno !== false) {
 304              return $this->_errorno;
 305          }
 306          if (!empty($this->_stmt)) {
 307              $err = $this->_stmt->errorCode();
 308          }
 309          else if (!empty($this->_connectionID)) {
 310              $arr = $this->_connectionID->errorInfo();
 311              if (isset($arr[0])) {
 312                  $err = $arr[0];
 313              }
 314              else {
 315                  $err = -1;
 316              }
 317          } else {
 318              return 0;
 319          }
 320  
 321          if ($err == '00000') {
 322              return 0; // allows empty check
 323          }
 324          return $err;
 325      }
 326  
 327  	function SetTransactionMode($transaction_mode)
 328      {
 329          if(method_exists($this->_driver, 'SetTransactionMode')) {
 330              return $this->_driver->SetTransactionMode($transaction_mode);
 331          }
 332  
 333          return parent::SetTransactionMode($seqname);
 334      }
 335  
 336  	function BeginTrans()
 337      {
 338          if(method_exists($this->_driver, 'BeginTrans')) {
 339              return $this->_driver->BeginTrans();
 340          }
 341  
 342          if (!$this->hasTransactions) {
 343              return false;
 344          }
 345          if ($this->transOff) {
 346              return true;
 347          }
 348          $this->transCnt += 1;
 349          $this->_autocommit = false;
 350          $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
 351  
 352          return $this->_connectionID->beginTransaction();
 353      }
 354  
 355  	function CommitTrans($ok=true)
 356      {
 357          if(method_exists($this->_driver, 'CommitTrans')) {
 358              return $this->_driver->CommitTrans($ok);
 359          }
 360  
 361          if (!$this->hasTransactions) {
 362              return false;
 363          }
 364          if ($this->transOff) {
 365              return true;
 366          }
 367          if (!$ok) {
 368              return $this->RollbackTrans();
 369          }
 370          if ($this->transCnt) {
 371              $this->transCnt -= 1;
 372          }
 373          $this->_autocommit = true;
 374  
 375          $ret = $this->_connectionID->commit();
 376          $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
 377          return $ret;
 378      }
 379  
 380  	function RollbackTrans()
 381      {
 382          if(method_exists($this->_driver, 'RollbackTrans')) {
 383              return $this->_driver->RollbackTrans();
 384          }
 385  
 386          if (!$this->hasTransactions) {
 387              return false;
 388          }
 389          if ($this->transOff) {
 390              return true;
 391          }
 392          if ($this->transCnt) {
 393              $this->transCnt -= 1;
 394          }
 395          $this->_autocommit = true;
 396  
 397          $ret = $this->_connectionID->rollback();
 398          $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
 399          return $ret;
 400      }
 401  
 402  	function Prepare($sql)
 403      {
 404          $this->_stmt = $this->_connectionID->prepare($sql);
 405          if ($this->_stmt) {
 406              return array($sql,$this->_stmt);
 407          }
 408  
 409          return false;
 410      }
 411  
 412  	function PrepareStmt($sql)
 413      {
 414          $stmt = $this->_connectionID->prepare($sql);
 415          if (!$stmt) {
 416              return false;
 417          }
 418          $obj = new ADOPDOStatement($stmt,$this);
 419          return $obj;
 420      }
 421  
 422  	function CreateSequence($seqname='adodbseq',$startID=1)
 423      {
 424          if(method_exists($this->_driver, 'CreateSequence')) {
 425              return $this->_driver->CreateSequence($seqname, $startID);
 426          }
 427  
 428          return parent::CreateSequence($seqname, $startID);
 429      }
 430  
 431  	function DropSequence($seqname='adodbseq')
 432      {
 433          if(method_exists($this->_driver, 'DropSequence')) {
 434              return $this->_driver->DropSequence($seqname);
 435          }
 436  
 437          return parent::DropSequence($seqname);
 438      }
 439  
 440  	function GenID($seqname='adodbseq',$startID=1)
 441      {
 442          if(method_exists($this->_driver, 'GenID')) {
 443              return $this->_driver->GenID($seqname, $startID);
 444          }
 445  
 446          return parent::GenID($seqname, $startID);
 447      }
 448  
 449  
 450      /* returns queryID or false */
 451  	function _query($sql,$inputarr=false)
 452      {
 453          if (is_array($sql)) {
 454              $stmt = $sql[1];
 455          } else {
 456              $stmt = $this->_connectionID->prepare($sql);
 457          }
 458          #adodb_backtrace();
 459          #var_dump($this->_bindInputArray);
 460          if ($stmt) {
 461              $this->_driver->debug = $this->debug;
 462              if ($inputarr) {
 463                  $ok = $stmt->execute($inputarr);
 464              }
 465              else {
 466                  $ok = $stmt->execute();
 467              }
 468          }
 469  
 470  
 471          $this->_errormsg = false;
 472          $this->_errorno = false;
 473  
 474          if ($ok) {
 475              $this->_stmt = $stmt;
 476              return $stmt;
 477          }
 478  
 479          if ($stmt) {
 480  
 481              $arr = $stmt->errorinfo();
 482              if ((integer)$arr[1]) {
 483                  $this->_errormsg = $arr[2];
 484                  $this->_errorno = $arr[1];
 485              }
 486  
 487          } else {
 488              $this->_errormsg = false;
 489              $this->_errorno = false;
 490          }
 491          return false;
 492      }
 493  
 494      // returns true or false
 495  	function _close()
 496      {
 497          $this->_stmt = false;
 498          return true;
 499      }
 500  
 501  	function _affectedrows()
 502      {
 503          return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
 504      }
 505  
 506  	function _insertid()
 507      {
 508          return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
 509      }
 510  }
 511  
 512  class ADODB_pdo_base extends ADODB_pdo {
 513  
 514      var $sysDate = "'?'";
 515      var $sysTimeStamp = "'?'";
 516  
 517  
 518  	function _init($parentDriver)
 519      {
 520          $parentDriver->_bindInputArray = true;
 521          #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
 522      }
 523  
 524  	function ServerInfo()
 525      {
 526          return ADOConnection::ServerInfo();
 527      }
 528  
 529  	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
 530      {
 531          $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
 532          return $ret;
 533      }
 534  
 535  	function MetaTables($ttype=false,$showSchema=false,$mask=false)
 536      {
 537          return false;
 538      }
 539  
 540  	function MetaColumns($table,$normalize=true)
 541      {
 542          return false;
 543      }
 544  }
 545  
 546  class ADOPDOStatement {
 547  
 548      var $databaseType = "pdo";
 549      var $dataProvider = "pdo";
 550      var $_stmt;
 551      var $_connectionID;
 552  
 553  	function __construct($stmt,$connection)
 554      {
 555          $this->_stmt = $stmt;
 556          $this->_connectionID = $connection;
 557      }
 558  
 559  	function Execute($inputArr=false)
 560      {
 561          $savestmt = $this->_connectionID->_stmt;
 562          $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
 563          $this->_connectionID->_stmt = $savestmt;
 564          return $rs;
 565      }
 566  
 567  	function InParameter(&$var,$name,$maxLen=4000,$type=false)
 568      {
 569  
 570          if ($type) {
 571              $this->_stmt->bindParam($name,$var,$type,$maxLen);
 572          }
 573          else {
 574              $this->_stmt->bindParam($name, $var);
 575          }
 576      }
 577  
 578  	function Affected_Rows()
 579      {
 580          return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
 581      }
 582  
 583  	function ErrorMsg()
 584      {
 585          if ($this->_stmt) {
 586              $arr = $this->_stmt->errorInfo();
 587          }
 588          else {
 589              $arr = $this->_connectionID->errorInfo();
 590          }
 591  
 592          if (is_array($arr)) {
 593              if ((integer) $arr[0] && isset($arr[2])) {
 594                  return $arr[2];
 595              }
 596              else {
 597                  return '';
 598              }
 599          } else {
 600              return '-1';
 601          }
 602      }
 603  
 604  	function NumCols()
 605      {
 606          return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
 607      }
 608  
 609  	function ErrorNo()
 610      {
 611          if ($this->_stmt) {
 612              return $this->_stmt->errorCode();
 613          }
 614          else {
 615              return $this->_connectionID->errorInfo();
 616          }
 617      }
 618  }
 619  
 620  /*--------------------------------------------------------------------------------------
 621      Class Name: Recordset
 622  --------------------------------------------------------------------------------------*/
 623  
 624  class ADORecordSet_pdo extends ADORecordSet {
 625  
 626      var $bind = false;
 627      var $databaseType = "pdo";
 628      var $dataProvider = "pdo";
 629  
 630  	function __construct($id,$mode=false)
 631      {
 632          if ($mode === false) {
 633              global $ADODB_FETCH_MODE;
 634              $mode = $ADODB_FETCH_MODE;
 635          }
 636          $this->adodbFetchMode = $mode;
 637          switch($mode) {
 638          case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
 639          case ADODB_FETCH_ASSOC:  $mode = PDO::FETCH_ASSOC; break;
 640  
 641          case ADODB_FETCH_BOTH:
 642          default: $mode = PDO::FETCH_BOTH; break;
 643          }
 644          $this->fetchMode = $mode;
 645  
 646          $this->_queryID = $id;
 647          parent::__construct($id);
 648      }
 649  
 650  
 651  	function Init()
 652      {
 653          if ($this->_inited) {
 654              return;
 655          }
 656          $this->_inited = true;
 657          if ($this->_queryID) {
 658              @$this->_initrs();
 659          }
 660          else {
 661              $this->_numOfRows = 0;
 662              $this->_numOfFields = 0;
 663          }
 664          if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
 665              $this->_currentRow = 0;
 666              if ($this->EOF = ($this->_fetch() === false)) {
 667                  $this->_numOfRows = 0; // _numOfRows could be -1
 668              }
 669          } else {
 670              $this->EOF = true;
 671          }
 672      }
 673  
 674  	function _initrs()
 675      {
 676      global $ADODB_COUNTRECS;
 677  
 678          $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
 679          if (!$this->_numOfRows) {
 680              $this->_numOfRows = -1;
 681          }
 682          $this->_numOfFields = $this->_queryID->columnCount();
 683      }
 684  
 685      // returns the field object
 686  	function FetchField($fieldOffset = -1)
 687      {
 688          $off=$fieldOffset+1; // offsets begin at 1
 689  
 690          $o= new ADOFieldObject();
 691          $arr = @$this->_queryID->getColumnMeta($fieldOffset);
 692          if (!$arr) {
 693              $o->name = 'bad getColumnMeta()';
 694              $o->max_length = -1;
 695              $o->type = 'VARCHAR';
 696              $o->precision = 0;
 697      #        $false = false;
 698              return $o;
 699          }
 700          //adodb_pr($arr);
 701          $o->name = $arr['name'];
 702          if (isset($arr['native_type']) && $arr['native_type'] <> "null") {
 703              $o->type = $arr['native_type'];
 704          }
 705          else {
 706              $o->type = adodb_pdo_type($arr['pdo_type']);
 707          }
 708          $o->max_length = $arr['len'];
 709          $o->precision = $arr['precision'];
 710  
 711          switch(ADODB_ASSOC_CASE) {
 712              case ADODB_ASSOC_CASE_LOWER:
 713                  $o->name = strtolower($o->name);
 714                  break;
 715              case ADODB_ASSOC_CASE_UPPER:
 716                  $o->name = strtoupper($o->name);
 717                  break;
 718          }
 719          return $o;
 720      }
 721  
 722  	function _seek($row)
 723      {
 724          return false;
 725      }
 726  
 727  	function _fetch()
 728      {
 729          if (!$this->_queryID) {
 730              return false;
 731          }
 732  
 733          $this->fields = $this->_queryID->fetch($this->fetchMode);
 734          return !empty($this->fields);
 735      }
 736  
 737  	function _close()
 738      {
 739          $this->_queryID = false;
 740      }
 741  
 742  	function Fields($colname)
 743      {
 744          if ($this->adodbFetchMode != ADODB_FETCH_NUM) {
 745              return @$this->fields[$colname];
 746          }
 747  
 748          if (!$this->bind) {
 749              $this->bind = array();
 750              for ($i=0; $i < $this->_numOfFields; $i++) {
 751                  $o = $this->FetchField($i);
 752                  $this->bind[strtoupper($o->name)] = $i;
 753              }
 754          }
 755          return $this->fields[$this->bind[strtoupper($colname)]];
 756      }
 757  
 758  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1