Subversion Repositories oidplus

Rev

Rev 1116 | 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 OIDplusQueryResultOci 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.  
  41.         /**
  42.          *
  43.          */
  44.         public function __destruct() {
  45.                 if ($this->res) {
  46.                         oci_free_statement($this->res);
  47.                 }
  48.         }
  49.  
  50.         /**
  51.          * @return bool
  52.          */
  53.         public function containsResultSet(): bool {
  54.                 return !$this->no_resultset;
  55.         }
  56.  
  57.         /**
  58.          * @var ?array
  59.          */
  60.         private $prefetchedArray = null;
  61.  
  62.         /**
  63.          * @var int
  64.          */
  65.         private $countAlreadyFetched = 0;
  66.  
  67.         /**
  68.          * @return int
  69.          * @throws OIDplusException
  70.          */
  71.         public function num_rows(): int {
  72.                 if (!is_null($this->prefetchedArray)) {
  73.                         return count($this->prefetchedArray) + $this->countAlreadyFetched;
  74.                 }
  75.  
  76.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  77.  
  78.                 // This function does not return number of rows selected! For SELECT statements this function will return the number of rows, that were fetched to the buffer with oci_fetch*() functions.
  79.                 //return oci_num_rows($this->res);
  80.  
  81.                 $this->prefetchedArray = array();
  82.                 oci_fetch_all($this->res, $this->prefetchedArray, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
  83.                 return count($this->prefetchedArray) + $this->countAlreadyFetched;
  84.         }
  85.  
  86.         /**
  87.          * @return array|mixed|null
  88.          * @throws OIDplusException
  89.          */
  90.         public function fetch_array()/*: ?array*/ {
  91.                 if (!is_null($this->prefetchedArray)) {
  92.                         $ret = array_shift($this->prefetchedArray);
  93.                 } else {
  94.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  95.                         $ret = oci_fetch_array($this->res);
  96.                         if ($ret === false) $ret = null;
  97.                 }
  98.                 if ($ret) $this->countAlreadyFetched++;
  99.  
  100.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  101.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  102.                 if ($ret) {
  103.                         $keys = array_keys($ret);
  104.                         foreach($keys as $key) {
  105.                                 $ret[strtolower($key)]=$ret[$key];
  106.                                 $ret[strtoupper($key)]=$ret[$key];
  107.                         }
  108.                 }
  109.  
  110.                 return $ret;
  111.         }
  112.  
  113.         /**
  114.          * @param $ary
  115.          * @return \stdClass
  116.          */
  117.         private static function array_to_stdobj($ary) {
  118.                 $obj = new \stdClass;
  119.                 foreach ($ary as $name => $val) {
  120.                         $obj->$name = $val;
  121.  
  122.                         // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  123.                         // We can't quote every single column throughout the whole program, so we use this workaround...
  124.                         $name = strtolower($name);
  125.                         $obj->$name = $val;
  126.                         $name = strtoupper($name);
  127.                         $obj->$name = $val;
  128.                 }
  129.                 return $obj;
  130.         }
  131.  
  132.         /**
  133.          * @return false|object|\stdClass|null
  134.          * @throws OIDplusException
  135.          */
  136.         public function fetch_object()/*: ?object*/ {
  137.                 if (!is_null($this->prefetchedArray)) {
  138.                         $ary = array_shift($this->prefetchedArray);
  139.                         $ret = is_null($ary) ? null : self::array_to_stdobj($ary);
  140.                 } else {
  141.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  142.                         $ret = oci_fetch_object($this->res);
  143.                         if ($ret === false) $ret = null;
  144.                 }
  145.                 if ($ret) $this->countAlreadyFetched++;
  146.  
  147.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  148.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  149.                 if ($ret) {
  150.                         foreach ($ret as $name => $val) {
  151.                                 $ret->{strtoupper($name)} = $val;
  152.                                 $ret->{strtolower($name)} = $val;
  153.                         }
  154.                 }
  155.  
  156.                 return $ret;
  157.         }
  158. }
  159.