Subversion Repositories oidplus

Rev

Rev 502 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusQueryResultPDO extends OIDplusQueryResult {
  23.         protected $no_resultset;
  24.         protected $res;
  25.  
  26.         public function __construct($res) {
  27.                 $this->no_resultset = is_bool($res);
  28.  
  29.                 if (!$this->no_resultset) {
  30.                         $this->res = $res;
  31.                 }
  32.  
  33.                 // This way we can simulate MARS (Multiple Active Result Sets) so that the test case "Simultanous prepared statements" works
  34.                 $this->prefetchedArray = $this->res->fetchAll();
  35.         }
  36.  
  37.         public function __destruct() {
  38.                 if ($this->res) $this->res->closeCursor();
  39.         }
  40.  
  41.         public function containsResultSet(): bool {
  42.                 return !$this->no_resultset;
  43.         }
  44.  
  45.         private $prefetchedArray = null;
  46.         private $countAlreadyFetched = 0;
  47.  
  48.         public function num_rows(): int {
  49.                 if (!is_null($this->prefetchedArray)) {
  50.                         return count($this->prefetchedArray) + $this->countAlreadyFetched;
  51.                 }
  52.  
  53.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  54.                 $ret = $this->res->rowCount();
  55.  
  56.                 // -1 can happen when PDO is connected via ODBC that is running a driver that does not support num_rows (e.g. Microsoft Access)
  57.                 // if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows'));
  58.                 if ($ret === -1) {
  59.                         $this->prefetchedArray = $this->res->fetchAll();
  60.                         return count($this->prefetchedArray) + $this->countAlreadyFetched;
  61.                 }
  62.  
  63.                 return $ret;
  64.         }
  65.  
  66.         public function fetch_array()/*: ?array*/ {
  67.                 if (!is_null($this->prefetchedArray)) {
  68.                         $ret = array_shift($this->prefetchedArray);
  69.                 } else {
  70.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  71.                         $ret = $this->res->fetch(PDO::FETCH_ASSOC);
  72.                         if ($ret === false) $ret = null;
  73.                 }
  74.                 if ($ret) $this->countAlreadyFetched++;
  75.                 return $ret;
  76.         }
  77.  
  78.         private static function array_to_stdobj($ary) {
  79.                 $obj = new stdClass;
  80.                 foreach ($ary as $name => $val) {
  81.                         $obj->$name = $val;
  82.                 }
  83.                 return $obj;
  84.         }
  85.  
  86.         public function fetch_object()/*: ?object*/ {
  87.                 if (!is_null($this->prefetchedArray)) {
  88.                         $ary = array_shift($this->prefetchedArray);
  89.                         $ret = is_null($ary) ? null : self::array_to_stdobj($ary);
  90.                 } else {
  91.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  92.                         $ret = $this->res->fetch(PDO::FETCH_OBJ);
  93.                         if ($ret === false) $ret = null;
  94.                 }
  95.                 if ($ret) $this->countAlreadyFetched++;
  96.                 return $ret;
  97.         }
  98. }