Rev 786 | Rev 1086 | 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 |
||
5 | * Copyright 2019 - 2022 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; |
786 | daniel-mar | 21 | |
22 | class OIDplusQueryResultOci 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 | |||
34 | public function __destruct() { |
||
35 | if ($this->res) { |
||
36 | oci_free_statement($this->res); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | public function containsResultSet(): bool { |
||
41 | return !$this->no_resultset; |
||
42 | } |
||
43 | |||
44 | private $prefetchedArray = null; |
||
45 | private $countAlreadyFetched = 0; |
||
46 | |||
47 | public function num_rows(): int { |
||
48 | if (!is_null($this->prefetchedArray)) { |
||
49 | return count($this->prefetchedArray) + $this->countAlreadyFetched; |
||
50 | } |
||
51 | |||
52 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
53 | |||
54 | // 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. |
||
55 | //return oci_num_rows($this->res); |
||
56 | |||
57 | $this->prefetchedArray = array(); |
||
58 | oci_fetch_all($this->res, $this->prefetchedArray, 0, -1, OCI_FETCHSTATEMENT_BY_ROW); |
||
59 | return count($this->prefetchedArray) + $this->countAlreadyFetched; |
||
60 | } |
||
61 | |||
62 | public function fetch_array()/*: ?array*/ { |
||
63 | if (!is_null($this->prefetchedArray)) { |
||
64 | $ret = array_shift($this->prefetchedArray); |
||
65 | } else { |
||
66 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
67 | $ret = oci_fetch_array($this->res); |
||
68 | if ($ret === false) $ret = null; |
||
69 | } |
||
70 | if ($ret) $this->countAlreadyFetched++; |
||
71 | |||
72 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
||
73 | // We can't quote every single column throughout the whole program, so we use this workaround... |
||
74 | if ($ret) { |
||
75 | $keys = array_keys($ret); |
||
76 | foreach($keys as $key) { |
||
77 | $ret[strtolower($key)]=$ret[$key]; |
||
78 | $ret[strtoupper($key)]=$ret[$key]; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | return $ret; |
||
83 | } |
||
84 | |||
85 | private static function array_to_stdobj($ary) { |
||
1050 | daniel-mar | 86 | $obj = new \stdClass; |
786 | daniel-mar | 87 | foreach ($ary as $name => $val) { |
88 | $obj->$name = $val; |
||
89 | |||
90 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
||
91 | // We can't quote every single column throughout the whole program, so we use this workaround... |
||
92 | $name = strtolower($name); |
||
93 | $obj->$name = $val; |
||
94 | $name = strtoupper($name); |
||
95 | $obj->$name = $val; |
||
96 | } |
||
97 | return $obj; |
||
98 | } |
||
99 | |||
100 | public function fetch_object()/*: ?object*/ { |
||
101 | if (!is_null($this->prefetchedArray)) { |
||
102 | $ary = array_shift($this->prefetchedArray); |
||
103 | $ret = is_null($ary) ? null : self::array_to_stdobj($ary); |
||
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)')); |
||
106 | $ret = oci_fetch_object($this->res); |
||
107 | if ($ret === false) $ret = null; |
||
108 | } |
||
109 | if ($ret) $this->countAlreadyFetched++; |
||
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 | foreach ($ret as $name => $val) { |
||
115 | $ret->{strtoupper($name)} = $val; |
||
116 | $ret->{strtolower($name)} = $val; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | return $ret; |
||
121 | } |
||
122 | } |