Subversion Repositories oidplus

Rev

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