Subversion Repositories oidplus

Rev

Rev 1117 | 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 OIDplusQueryResultOci extends OIDplusQueryResult {
  27.  
  28.         /**
  29.          * @var bool
  30.          */
  31.         protected $no_resultset;
  32.  
  33.         /**
  34.          * @var mixed
  35.          */
  36.         protected $res;
  37.  
  38.         /**
  39.          * @param mixed $res
  40.          */
  41.         public function __construct($res) {
  42.                 $this->no_resultset = is_bool($res);
  43.  
  44.                 if (!$this->no_resultset) {
  45.                         $this->res = $res;
  46.                 }
  47.         }
  48.  
  49.         /**
  50.          *
  51.          */
  52.         public function __destruct() {
  53.                 if ($this->res) {
  54.                         oci_free_statement($this->res);
  55.                 }
  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.  
  86.                 // 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.
  87.                 //return oci_num_rows($this->res);
  88.  
  89.                 $this->prefetchedArray = array();
  90.                 oci_fetch_all($this->res, $this->prefetchedArray, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
  91.                 return count($this->prefetchedArray) + $this->countAlreadyFetched;
  92.         }
  93.  
  94.         /**
  95.          * @return array|mixed|null
  96.          * @throws OIDplusException
  97.          */
  98.         public function fetch_array()/*: ?array*/ {
  99.                 if (!is_null($this->prefetchedArray)) {
  100.                         $ret = array_shift($this->prefetchedArray);
  101.                 } else {
  102.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  103.                         $ret = oci_fetch_array($this->res);
  104.                         if ($ret === false) $ret = null;
  105.                 }
  106.                 if ($ret) $this->countAlreadyFetched++;
  107.  
  108.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  109.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  110.                 if ($ret) {
  111.                         $keys = array_keys($ret);
  112.                         foreach($keys as $key) {
  113.                                 $ret[strtolower($key)]=$ret[$key];
  114.                                 $ret[strtoupper($key)]=$ret[$key];
  115.                         }
  116.                 }
  117.  
  118.                 return $ret;
  119.         }
  120.  
  121.         /**
  122.          * @param array $ary
  123.          * @return \stdClass
  124.          */
  125.         private static function array_to_stdobj(array $ary): \stdClass {
  126.                 $obj = new \stdClass;
  127.                 foreach ($ary as $name => $val) {
  128.                         $obj->$name = $val;
  129.  
  130.                         // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  131.                         // We can't quote every single column throughout the whole program, so we use this workaround...
  132.                         $name = strtolower($name);
  133.                         $obj->$name = $val;
  134.                         $name = strtoupper($name);
  135.                         $obj->$name = $val;
  136.                 }
  137.                 return $obj;
  138.         }
  139.  
  140.         /**
  141.          * @return false|object|\stdClass|null
  142.          * @throws OIDplusException
  143.          */
  144.         public function fetch_object()/*: ?object*/ {
  145.                 if (!is_null($this->prefetchedArray)) {
  146.                         $ary = array_shift($this->prefetchedArray);
  147.                         $ret = is_null($ary) ? null : self::array_to_stdobj($ary);
  148.                 } else {
  149.                         if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  150.                         $ret = oci_fetch_object($this->res);
  151.                         if ($ret === false) $ret = null;
  152.                 }
  153.                 if ($ret) $this->countAlreadyFetched++;
  154.  
  155.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  156.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  157.                 if ($ret) {
  158.                         foreach ($ret as $name => $val) {
  159.                                 $ret->{strtoupper($name)} = $val;
  160.                                 $ret->{strtolower($name)} = $val;
  161.                         }
  162.                 }
  163.  
  164.                 return $ret;
  165.         }
  166. }
  167.