Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
236 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
236 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;
511 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
 
730 daniel-mar 26
abstract class OIDplusQueryResult extends OIDplusBaseClass {
1116 daniel-mar 27
 
28
        /**
29
         * @return bool
30
         */
236 daniel-mar 31
        abstract public function containsResultSet(): bool;
1116 daniel-mar 32
 
33
        /**
34
         * @return int
35
         */
1152 daniel-mar 36
        abstract protected function do_num_rows(): int;
1116 daniel-mar 37
 
38
        /**
1156 daniel-mar 39
         * @var array|null
40
         */
41
        protected $prefetchedArray = null;
42
 
43
        /**
44
         * @var int
45
         */
46
        protected $countAlreadyFetched = 0;
47
 
48
        /**
1157 daniel-mar 49
         * Please override this method if the database driver can perform a "fetch all" in its own way
1156 daniel-mar 50
         *
51
         * @return void
52
         * @throws OIDplusConfigInitializationException
53
         * @throws OIDplusException
54
         * @throws \ReflectionException
55
         */
56
        public function prefetchAll() {
57
                if (!is_null($this->prefetchedArray)) return;
58
                $pfa = array();
59
                while ($row = $this->fetch_array()) {
60
                        $pfa[] = $row; // you may not edit $this->prefetchedArray at this step, because $this->>fetch_array() checks it
61
                        $this->countAlreadyFetched--; // because fetch_array() increases $this->countAlreadyFetched, we need to revert it
62
                }
63
                $this->prefetchedArray = $pfa;
64
        }
65
 
66
        /**
1152 daniel-mar 67
         * @return int
1156 daniel-mar 68
         * @throws OIDplusConfigInitializationException
69
         * @throws OIDplusException
1152 daniel-mar 70
         */
71
        public final function num_rows(): int {
72
                if (!$this->containsResultSet()) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
1156 daniel-mar 73
 
74
                if (!is_null($this->prefetchedArray)) {
75
                        return count($this->prefetchedArray) + $this->countAlreadyFetched;
76
                }
77
 
78
                $ret = $this->do_num_rows();
79
 
80
                if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows'));
81
 
82
                return $ret;
1152 daniel-mar 83
        }
84
 
85
        /**
1156 daniel-mar 86
         * Plugins can override and extend this method. It post-processes contents of fetch_array() and fetch_object()
87
         * to fix various issues with database drivers.
88
         *
89
         * @param array|object &$ret
90
         * @return void
91
         */
92
        protected function fixFields(&$ret) {
93
                // ODBC gives bit(1) as binary, MySQL as integer and PDO as string.
1157 daniel-mar 94
                // We'll do it like MySQL does, although ODBC semms to be more correct.
1159 daniel-mar 95
                // We don't put this code into OIDplusQueryResultODBC.class.php, because other
96
                // DBMS might do the same - and then we would be prepared.
1156 daniel-mar 97
                foreach ($ret as &$value) {
98
                        if ($value === chr(0)) $value = 0;
99
                        if ($value === chr(1)) $value = 1;
100
                }
101
 
102
                // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
103
                // We can't quote every single column throughout the whole program, so we use this workaround...
104
                if (is_array($ret)) {
105
                        $keys = array_keys($ret);
106
                        foreach ($keys as $key) {
107
                                $ret[strtolower($key)] = $ret[$key];
108
                                $ret[strtoupper($key)] = $ret[$key];
109
                        }
110
                } else if (is_object($ret)) {
111
                        foreach ($ret as $name => $val) {
112
                                $ret->{strtoupper($name)} = $val;
113
                                $ret->{strtolower($name)} = $val;
114
                        }
115
                }
116
        }
117
 
118
        /**
119
         * Please override do_fetch_object(), do_fetch_array(), or both.
1116 daniel-mar 120
         * @return array|null
121
         */
1152 daniel-mar 122
        protected function do_fetch_array()/*: ?array*/ {
123
                assert(false);
124
                return null;
125
        }
1116 daniel-mar 126
 
127
        /**
1152 daniel-mar 128
         * @return array|null
1156 daniel-mar 129
         * @throws OIDplusConfigInitializationException
130
         * @throws OIDplusException
131
         * @throws \ReflectionException
1152 daniel-mar 132
         */
133
        public final function fetch_array()/*: ?array*/ {
134
                if (!$this->containsResultSet()) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
1156 daniel-mar 135
                if (!is_null($this->prefetchedArray)) {
136
                        // Prefetched value exists. Use it.
137
                        $ary = array_shift($this->prefetchedArray);
138
                } else {
139
                        $reflector = new \ReflectionMethod($this, 'do_fetch_array');
140
                        $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
141
                        if ($isImplemented) {
142
                                // do_fetch_array() is implemented. Use it.
143
                                $ary = $this->do_fetch_array();
144
                        } else {
145
                                // Use the implementation of do_fetch_object()
146
                                $reflector = new \ReflectionMethod($this, 'do_fetch_object');
147
                                $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
148
                                if (!$isImplemented) {
149
                                        throw new OIDplusException(_L("Class %1 is erroneous: At least one fetch-method needs to be overridden", get_class($this)));
150
                                }
151
                                $obj = $this->do_fetch_object();
152
                                $ary = is_null($obj) ? null : stdobj_to_array($obj);
153
                        }
1152 daniel-mar 154
                }
1156 daniel-mar 155
                if (!is_null($ary)) {
156
                        $this->countAlreadyFetched++;
157
                        $this->fixFields($ary);
1152 daniel-mar 158
                }
159
                return $ary;
160
        }
161
 
162
        /**
1156 daniel-mar 163
         * Please override do_fetch_object(), do_fetch_array(), or both.
1116 daniel-mar 164
         * @return object|null
165
         */
1156 daniel-mar 166
        protected function do_fetch_object()/*: ?\stdClass*/ {
1152 daniel-mar 167
                assert(false);
168
                return null;
169
        }
790 daniel-mar 170
 
1116 daniel-mar 171
        /**
1152 daniel-mar 172
         * @return object|null
1156 daniel-mar 173
         * @throws OIDplusConfigInitializationException
174
         * @throws OIDplusException
175
         * @throws \ReflectionException
1152 daniel-mar 176
         */
1156 daniel-mar 177
        public final function fetch_object()/*: ?\stdClass*/ {
1152 daniel-mar 178
                if (!$this->containsResultSet()) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
1156 daniel-mar 179
                if (!is_null($this->prefetchedArray)) {
1164 daniel-mar 180
                        // Prefetched value exists (as array). Convert and use it.
1156 daniel-mar 181
                        $ary = array_shift($this->prefetchedArray);
182
                        $obj = is_null($ary) ? null : array_to_stdobj($ary);
183
                } else {
184
                        $reflector = new \ReflectionMethod($this, 'do_fetch_object');
185
                        $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
186
                        if ($isImplemented) {
187
                                // do_fetch_object() is implemented. Use it.
188
                                $obj = $this->do_fetch_object();
189
                        } else {
190
                                // Use the implementation of do_fetch_array()
191
                                $reflector = new \ReflectionMethod($this, 'do_fetch_array');
192
                                $isImplemented = ($reflector->getDeclaringClass()->getName() !== self::class);
193
                                if (!$isImplemented) {
194
                                        throw new OIDplusException(_L("Class %1 is erroneous: At least one fetch-method needs to be overridden", get_class($this)));
195
                                }
196
                                $ary = $this->do_fetch_array();
197
                                $obj = is_null($ary) ? null : array_to_stdobj($ary);
198
                        }
1152 daniel-mar 199
                }
1156 daniel-mar 200
                if (!is_null($obj)) {
201
                        $this->countAlreadyFetched++;
202
                        $this->fixFields($obj);
1152 daniel-mar 203
                }
204
                return $obj;
205
        }
206
 
207
        /**
1116 daniel-mar 208
         * The any() function returns true if there is at least one
209
         * row in the section. By default, num_rows() will be used.
210
         * Plugins can override this method if they have a possibility
211
         * of making this functionality more efficient.
1156 daniel-mar 212
         *
1116 daniel-mar 213
         * @return bool
1156 daniel-mar 214
         * @throws OIDplusException
1116 daniel-mar 215
         */
790 daniel-mar 216
        public function any(): bool {
217
                return $this->num_rows() > 0;
218
        }
1156 daniel-mar 219
 
220
        /**
221
         * @param string $dbField
222
         * @return void
223
         * @throws OIDplusConfigInitializationException
224
         * @throws OIDplusException
225
         * @throws \ReflectionException
226
         */
227
        public final function naturalSortByField(string $dbField) {
228
                if (is_null($this->prefetchedArray)) {
229
                        $this->prefetchAll();
230
                }
231
 
232
                // Sort $this->prefetchedArray by field $dbField
233
                natsort_field($this->prefetchedArray, $dbField);
234
        }
236 daniel-mar 235
}