Subversion Repositories oidplus

Rev

Rev 1116 | 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 OIDplusQueryResultMySQLNoNativeDriver extends OIDplusQueryResult {
  27.         // Based on https://www.php.net/manual/de/mysqli-stmt.get-result.php#113398
  28.  
  29.         /**
  30.          * @var mixed|null
  31.          */
  32.         protected $stmt = null;
  33.  
  34.         /**
  35.          * @var int|null
  36.          */
  37.         protected $nCols = null;
  38.  
  39.         /**
  40.          * @var bool|null
  41.          */
  42.         protected $no_resultset = null;
  43.  
  44.         /**
  45.          * @param mixed $stmt
  46.          */
  47.         public function __construct($stmt) {
  48.                 $metadata = mysqli_stmt_result_metadata($stmt);
  49.  
  50.                 $this->no_resultset = is_bool($metadata);
  51.  
  52.                 if (!$this->no_resultset) {
  53.                         $this->nCols = mysqli_num_fields($metadata);
  54.                         $this->stmt = $stmt;
  55.  
  56.                         mysqli_free_result($metadata);
  57.  
  58.                         $this->stmt->store_result();
  59.                 }
  60.         }
  61.  
  62.         /**
  63.          * @return bool
  64.          */
  65.         public function containsResultSet(): bool {
  66.                 return !$this->no_resultset;
  67.         }
  68.  
  69.         /**
  70.          * @return int
  71.          * @throws OIDplusException
  72.          */
  73.         public function num_rows(): int {
  74.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  75.  
  76.                 //$this->stmt->store_result();
  77.                 return $this->stmt->num_rows;
  78.         }
  79.  
  80.         /**
  81.          * @return array|null
  82.          * @throws OIDplusException
  83.          */
  84.         public function fetch_array()/*: ?array*/ {
  85.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  86.  
  87.                 // https://stackoverflow.com/questions/10752815/mysqli-get-result-alternative , modified
  88.                 $stmt = $this->stmt;
  89.                 //$this->stmt->store_result();
  90.                 $resultkeys = array();
  91.                 $thisName = "";
  92.  
  93.                 if ($stmt->num_rows==0) return null;
  94.  
  95.                 for ($i = 0; $i < $stmt->num_rows; $i++) {
  96.                         $metadata = $stmt->result_metadata();
  97.                         while ($field = $metadata->fetch_field()) {
  98.                                 $thisName = $field->name;
  99.                                 $resultkeys[] = $thisName;
  100.                         }
  101.                 }
  102.  
  103.                 $ret = array();
  104.                 $args = array();
  105.                 for ($i=0; $i<$this->nCols; $i++) {
  106.                         $ret[$i] = NULL;
  107.                         $theValue = $resultkeys[$i];
  108.                         $ret[$theValue] = NULL; // will be overwritten by mysqli_stmt_bind_result
  109.                         $args[] = &$ret[$theValue];
  110.                 }
  111.                 if (!mysqli_stmt_bind_result($this->stmt, ...$args)) {
  112.                         return null;
  113.                 }
  114.  
  115.                 // This should advance the "$stmt" cursor.
  116.                 if (!mysqli_stmt_fetch($this->stmt)) {
  117.                         return null;
  118.                 }
  119.  
  120.                 // Return the array we built.
  121.                 return $ret;
  122.         }
  123.  
  124.         /**
  125.          * @return \stdClass|null
  126.          * @throws OIDplusConfigInitializationException
  127.          * @throws OIDplusException
  128.          */
  129.         public function fetch_object()/*: ?object*/ {
  130.                 if ($this->no_resultset) throw new OIDplusConfigInitializationException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  131.  
  132.                 $ary = $this->fetch_array();
  133.                 if (!$ary) return null;
  134.  
  135.                 $obj = new \stdClass;
  136.                 foreach ($ary as $name => $val) {
  137.                         $obj->$name = $val;
  138.                 }
  139.                 return $obj;
  140.         }
  141. }
  142.