Subversion Repositories oidplus

Rev

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