Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 daniel-mar 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusQueryResultMySQLNoNativeDriver extends OIDplusQueryResult {
27
        // Based on https://www.php.net/manual/de/mysqli-stmt.get-result.php#113398
28
 
29
        protected $stmt = null;
30
        protected $nCols = null;
31
        protected $no_resultset = null;
32
 
33
        public function __construct($stmt) {
34
                $metadata = mysqli_stmt_result_metadata($stmt);
35
 
36
                $this->no_resultset = is_bool($metadata);
37
 
38
                if (!$this->no_resultset) {
39
                        $this->nCols = mysqli_num_fields($metadata);
40
                        $this->stmt = $stmt;
41
 
42
                        mysqli_free_result($metadata);
43
 
44
                        $this->stmt->store_result();
45
                }
46
        }
47
 
48
        public function containsResultSet(): bool {
49
                return !$this->no_resultset;
50
        }
51
 
52
        public function num_rows(): int {
53
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
54
 
55
                //$this->stmt->store_result();
56
                return $this->stmt->num_rows;
57
        }
58
 
59
        public function fetch_array()/*: ?array*/ {
60
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
61
 
62
                // https://stackoverflow.com/questions/10752815/mysqli-get-result-alternative , modified
63
                $stmt = $this->stmt;
64
                //$this->stmt->store_result();
65
                $resultkeys = array();
66
                $thisName = "";
67
 
68
                if ($stmt->num_rows==0) return null;
69
 
70
                for ($i = 0; $i < $stmt->num_rows; $i++) {
71
                        $metadata = $stmt->result_metadata();
72
                        while ($field = $metadata->fetch_field()) {
73
                                $thisName = $field->name;
74
                                $resultkeys[] = $thisName;
75
                        }
76
                }
77
 
78
                $ret = array();
79
                $args = array();
80
                for ($i=0; $i<$this->nCols; $i++) {
81
                        $ret[$i] = NULL;
82
                        $theValue = $resultkeys[$i];
83
                        $ret[$theValue] = NULL; // will be overwritten by mysqli_stmt_bind_result
84
                        $args[] = &$ret[$theValue];
85
                }
86
                if (!mysqli_stmt_bind_result($this->stmt, ...$args)) {
87
                        return null;
88
                }
89
 
90
                // This should advance the "$stmt" cursor.
91
                if (!mysqli_stmt_fetch($this->stmt)) {
92
                        return null;
93
                }
94
 
95
                // Return the array we built.
96
                return $ret;
97
        }
98
 
99
        public function fetch_object()/*: ?object*/ {
100
                if ($this->no_resultset) throw new OIDplusConfigInitializationException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
101
 
102
                $ary = $this->fetch_array();
103
                if (!$ary) return null;
104
 
1050 daniel-mar 105
                $obj = new \stdClass;
635 daniel-mar 106
                foreach ($ary as $name => $val) {
107
                        $obj->$name = $val;
108
                }
109
                return $obj;
110
        }
1050 daniel-mar 111
}