Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1130 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusQueryResultPDO extends OIDplusQueryResult {
  27.         protected $no_resultset;
  28.         protected $res;
  29.  
  30.         /**
  31.          * @param $res
  32.          */
  33.         public function __construct($res) {
  34.                 $this->no_resultset = is_bool($res);
  35.  
  36.                 if (!$this->no_resultset) {
  37.                         $this->res = $res;
  38.                 }
  39.  
  40.                 // This way we can simulate MARS (Multiple Active Result Sets) so that the test case "Simultanous prepared statements" works
  41.                 $this->prefetchedArray = $this->res->fetchAll();
  42.         }
  43.  
  44.         /**
  45.          *
  46.          */
  47.         public function __destruct() {
  48.                 if ($this->res) $this->res->closeCursor();
  49.         }
  50.  
  51.         /**
  52.          * @return bool
  53.          */
  54.         public function containsResultSet(): bool {
  55.                 return !$this->no_resultset;
  56.         }
  57.  
  58.         /**
  59.          * @var ?array
  60.          */
  61.         private $prefetchedArray = null;
  62.  
  63.         /**
  64.          * @var int
  65.          */
  66.         private $countAlreadyFetched = 0;
  67.  
  68.         /**
  69.          * @return int
  70.          * @throws OIDplusException
  71.          */
  72.         public function num_rows(): int {
  73.                 if (!is_null($this->prefetchedArray)) {
  74.                         return count($this->prefetchedArray) + $this->countAlreadyFetched;
  75.                 }
  76.  
  77.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  78.                 $ret = $this->res->rowCount();
  79.  
  80.                 // -1 can happen when PDO is connected via ODBC that is running a driver that does not support num_rows (e.g. Microsoft Access)
  81.                 // if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows'));
  82.                 if ($ret === -1) {
  83.                         $this->prefetchedArray = $this->res->fetchAll();
  84.                         return count($this->prefetchedArray) + $this->countAlreadyFetched;
  85.                 }
  86.  
  87.                 return $ret;
  88.         }
  89.  
  90.         /**
  91.          * @return array|mixed|null
  92.          * @throws OIDplusException
  93.          */
  94.         public function fetch_array()/*: ?array*/ {
  95.                 if (!is_null($this->prefetchedArray)) {
  96.                         $ret = array_shift($this->prefetchedArray);
  97.                 } else {
  98.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  99.                         $ret = $this->res->fetch(\PDO::FETCH_ASSOC);
  100.                         if ($ret === false) $ret = null;
  101.                 }
  102.                 if ($ret) $this->countAlreadyFetched++;
  103.  
  104.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  105.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  106.                 if ($ret) {
  107.                         $keys = array_keys($ret);
  108.                         foreach($keys as $key) {
  109.                                 $ret[strtolower($key)]=$ret[$key];
  110.                                 $ret[strtoupper($key)]=$ret[$key];
  111.                         }
  112.                 }
  113.  
  114.                 return $ret;
  115.         }
  116.  
  117.         /**
  118.          * @param $ary
  119.          * @return \stdClass
  120.          */
  121.         private static function array_to_stdobj($ary) {
  122.                 $obj = new \stdClass;
  123.                 foreach ($ary as $name => $val) {
  124.                         $obj->$name = $val;
  125.                 }
  126.                 return $obj;
  127.         }
  128.  
  129.         /**
  130.          * @return object|\stdClass|null
  131.          * @throws OIDplusException
  132.          */
  133.         public function fetch_object()/*: ?object*/ {
  134.                 if (!is_null($this->prefetchedArray)) {
  135.                         $ary = array_shift($this->prefetchedArray);
  136.                         $ret = is_null($ary) ? null : self::array_to_stdobj($ary);
  137.                 } else {
  138.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  139.                         $ret = $this->res->fetch(\PDO::FETCH_OBJ);
  140.                         if ($ret === false) $ret = null;
  141.                 }
  142.                 if ($ret) $this->countAlreadyFetched++;
  143.  
  144.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  145.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  146.                 if ($ret) {
  147.                         foreach ($ret as $name => $val) {
  148.                                 $ret->{strtoupper($name)} = $val;
  149.                                 $ret->{strtolower($name)} = $val;
  150.                         }
  151.                 }
  152.  
  153.                 return $ret;
  154.         }
  155. }
  156.