Subversion Repositories oidplus

Rev

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
 
1130 daniel-mar 29
        /**
30
         * @var mixed|null
31
         */
635 daniel-mar 32
        protected $stmt = null;
1130 daniel-mar 33
 
34
        /**
35
         * @var int|null
36
         */
635 daniel-mar 37
        protected $nCols = null;
1130 daniel-mar 38
 
39
        /**
40
         * @var bool|null
41
         */
635 daniel-mar 42
        protected $no_resultset = null;
43
 
1116 daniel-mar 44
        /**
1130 daniel-mar 45
         * @param mixed $stmt
1116 daniel-mar 46
         */
635 daniel-mar 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
 
1116 daniel-mar 62
        /**
63
         * @return bool
64
         */
635 daniel-mar 65
        public function containsResultSet(): bool {
66
                return !$this->no_resultset;
67
        }
68
 
1116 daniel-mar 69
        /**
70
         * @return int
71
         */
1152 daniel-mar 72
        protected function do_num_rows(): int {
635 daniel-mar 73
                //$this->stmt->store_result();
74
                return $this->stmt->num_rows;
75
        }
76
 
1116 daniel-mar 77
        /**
78
         * @return array|null
79
         */
1152 daniel-mar 80
        protected function do_fetch_array()/*: ?array*/ {
635 daniel-mar 81
                // https://stackoverflow.com/questions/10752815/mysqli-get-result-alternative , modified
82
                $stmt = $this->stmt;
83
                //$this->stmt->store_result();
84
                $resultkeys = array();
85
                $thisName = "";
86
 
87
                if ($stmt->num_rows==0) return null;
88
 
89
                for ($i = 0; $i < $stmt->num_rows; $i++) {
90
                        $metadata = $stmt->result_metadata();
91
                        while ($field = $metadata->fetch_field()) {
92
                                $thisName = $field->name;
93
                                $resultkeys[] = $thisName;
94
                        }
95
                }
96
 
97
                $ret = array();
98
                $args = array();
99
                for ($i=0; $i<$this->nCols; $i++) {
100
                        $ret[$i] = NULL;
101
                        $theValue = $resultkeys[$i];
102
                        $ret[$theValue] = NULL; // will be overwritten by mysqli_stmt_bind_result
103
                        $args[] = &$ret[$theValue];
104
                }
105
                if (!mysqli_stmt_bind_result($this->stmt, ...$args)) {
106
                        return null;
107
                }
108
 
109
                // This should advance the "$stmt" cursor.
110
                if (!mysqli_stmt_fetch($this->stmt)) {
111
                        return null;
112
                }
113
 
114
                // Return the array we built.
115
                return $ret;
116
        }
117
 
1050 daniel-mar 118
}