Subversion Repositories oidplus

Rev

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