Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
786 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
786 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;
786 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
 
786 daniel-mar 26
class OIDplusQueryResultOci extends OIDplusQueryResult {
27
        protected $no_resultset;
28
        protected $res;
29
 
30
        public function __construct($res) {
31
                $this->no_resultset = is_bool($res);
32
 
33
                if (!$this->no_resultset) {
34
                        $this->res = $res;
35
                }
36
        }
37
 
38
        public function __destruct() {
39
                if ($this->res) {
40
                        oci_free_statement($this->res);
41
                }
42
        }
43
 
44
        public function containsResultSet(): bool {
45
                return !$this->no_resultset;
46
        }
47
 
48
        private $prefetchedArray = null;
49
        private $countAlreadyFetched = 0;
50
 
51
        public function num_rows(): int {
52
                if (!is_null($this->prefetchedArray)) {
53
                        return count($this->prefetchedArray) + $this->countAlreadyFetched;
54
                }
55
 
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
                // This function does not return number of rows selected! For SELECT statements this function will return the number of rows, that were fetched to the buffer with oci_fetch*() functions.
59
                //return oci_num_rows($this->res);
60
 
61
                $this->prefetchedArray = array();
62
                oci_fetch_all($this->res, $this->prefetchedArray, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
63
                return count($this->prefetchedArray) + $this->countAlreadyFetched;
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)'));
71
                        $ret = oci_fetch_array($this->res);
72
                        if ($ret === false) $ret = null;
73
                }
74
                if ($ret) $this->countAlreadyFetched++;
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
 
86
                return $ret;
87
        }
88
 
89
        private static function array_to_stdobj($ary) {
1050 daniel-mar 90
                $obj = new \stdClass;
786 daniel-mar 91
                foreach ($ary as $name => $val) {
92
                        $obj->$name = $val;
93
 
94
                        // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
95
                        // We can't quote every single column throughout the whole program, so we use this workaround...
96
                        $name = strtolower($name);
97
                        $obj->$name = $val;
98
                        $name = strtoupper($name);
99
                        $obj->$name = $val;
100
                }
101
                return $obj;
102
        }
103
 
104
        public function fetch_object()/*: ?object*/ {
105
                if (!is_null($this->prefetchedArray)) {
106
                        $ary = array_shift($this->prefetchedArray);
107
                        $ret = is_null($ary) ? null : self::array_to_stdobj($ary);
108
                } else {
109
                        if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
110
                        $ret = oci_fetch_object($this->res);
111
                        if ($ret === false) $ret = null;
112
                }
113
                if ($ret) $this->countAlreadyFetched++;
114
 
115
                // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
116
                // We can't quote every single column throughout the whole program, so we use this workaround...
117
                if ($ret) {
118
                        foreach ($ret as $name => $val) {
119
                                $ret->{strtoupper($name)} = $val;
120
                                $ret->{strtolower($name)} = $val;
121
                        }
122
                }
123
 
124
                return $ret;
125
        }
126
}