Subversion Repositories oidplus

Rev

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