Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1152 | 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 OIDplusQueryResultPDO extends OIDplusQueryResult {
1130 daniel-mar 27
        /**
28
         * @var bool
29
         */
635 daniel-mar 30
        protected $no_resultset;
1130 daniel-mar 31
 
32
        /**
33
         * @var mixed
34
         */
635 daniel-mar 35
        protected $res;
36
 
1116 daniel-mar 37
        /**
1130 daniel-mar 38
         * @param mixed $res
1116 daniel-mar 39
         */
635 daniel-mar 40
        public function __construct($res) {
41
                $this->no_resultset = is_bool($res);
42
 
43
                if (!$this->no_resultset) {
44
                        $this->res = $res;
45
                }
46
 
47
                // This way we can simulate MARS (Multiple Active Result Sets) so that the test case "Simultanous prepared statements" works
48
                $this->prefetchedArray = $this->res->fetchAll();
49
        }
50
 
1116 daniel-mar 51
        /**
52
         *
53
         */
635 daniel-mar 54
        public function __destruct() {
55
                if ($this->res) $this->res->closeCursor();
56
        }
57
 
1116 daniel-mar 58
        /**
59
         * @return bool
60
         */
635 daniel-mar 61
        public function containsResultSet(): bool {
62
                return !$this->no_resultset;
63
        }
64
 
1116 daniel-mar 65
        /**
66
         * @var ?array
67
         */
635 daniel-mar 68
        private $prefetchedArray = null;
1116 daniel-mar 69
 
70
        /**
71
         * @var int
72
         */
635 daniel-mar 73
        private $countAlreadyFetched = 0;
74
 
1116 daniel-mar 75
        /**
76
         * @return int
77
         * @throws OIDplusException
78
         */
635 daniel-mar 79
        public function num_rows(): int {
80
                if (!is_null($this->prefetchedArray)) {
81
                        return count($this->prefetchedArray) + $this->countAlreadyFetched;
82
                }
83
 
84
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
85
                $ret = $this->res->rowCount();
86
 
87
                // -1 can happen when PDO is connected via ODBC that is running a driver that does not support num_rows (e.g. Microsoft Access)
88
                // if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows'));
89
                if ($ret === -1) {
90
                        $this->prefetchedArray = $this->res->fetchAll();
91
                        return count($this->prefetchedArray) + $this->countAlreadyFetched;
92
                }
93
 
94
                return $ret;
95
        }
96
 
1116 daniel-mar 97
        /**
98
         * @return array|mixed|null
99
         * @throws OIDplusException
100
         */
635 daniel-mar 101
        public function fetch_array()/*: ?array*/ {
102
                if (!is_null($this->prefetchedArray)) {
103
                        $ret = array_shift($this->prefetchedArray);
104
                } else {
105
                        if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
1050 daniel-mar 106
                        $ret = $this->res->fetch(\PDO::FETCH_ASSOC);
635 daniel-mar 107
                        if ($ret === false) $ret = null;
108
                }
109
                if ($ret) $this->countAlreadyFetched++;
783 daniel-mar 110
 
111
                // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
112
                // We can't quote every single column throughout the whole program, so we use this workaround...
113
                if ($ret) {
114
                        $keys = array_keys($ret);
115
                        foreach($keys as $key) {
116
                                $ret[strtolower($key)]=$ret[$key];
117
                                $ret[strtoupper($key)]=$ret[$key];
118
                        }
119
                }
120
 
635 daniel-mar 121
                return $ret;
122
        }
123
 
1116 daniel-mar 124
        /**
1130 daniel-mar 125
         * @param array $ary
1116 daniel-mar 126
         * @return \stdClass
127
         */
1130 daniel-mar 128
        private static function array_to_stdobj(array $ary): \stdClass {
1050 daniel-mar 129
                $obj = new \stdClass;
635 daniel-mar 130
                foreach ($ary as $name => $val) {
131
                        $obj->$name = $val;
132
                }
133
                return $obj;
134
        }
135
 
1116 daniel-mar 136
        /**
137
         * @return object|\stdClass|null
138
         * @throws OIDplusException
139
         */
635 daniel-mar 140
        public function fetch_object()/*: ?object*/ {
141
                if (!is_null($this->prefetchedArray)) {
142
                        $ary = array_shift($this->prefetchedArray);
143
                        $ret = is_null($ary) ? null : self::array_to_stdobj($ary);
144
                } else {
145
                        if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
1050 daniel-mar 146
                        $ret = $this->res->fetch(\PDO::FETCH_OBJ);
635 daniel-mar 147
                        if ($ret === false) $ret = null;
148
                }
149
                if ($ret) $this->countAlreadyFetched++;
786 daniel-mar 150
 
151
                // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
152
                // We can't quote every single column throughout the whole program, so we use this workaround...
153
                if ($ret) {
154
                        foreach ($ret as $name => $val) {
155
                                $ret->{strtoupper($name)} = $val;
156
                                $ret->{strtolower($name)} = $val;
157
                        }
158
                }
159
 
635 daniel-mar 160
                return $ret;
161
        }
783 daniel-mar 162
}