Rev 1086 | Rev 1130 | 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 OIDplusQueryResultODBC extends OIDplusQueryResult { |
27 | protected $no_resultset; |
||
28 | protected $res; |
||
29 | |||
1116 | daniel-mar | 30 | /** |
31 | * @param $res |
||
32 | */ |
||
635 | daniel-mar | 33 | public function __construct($res) { |
34 | $this->no_resultset = is_bool($res); |
||
35 | |||
36 | if (!$this->no_resultset) { |
||
37 | $this->res = $res; |
||
38 | } |
||
39 | } |
||
40 | |||
1116 | daniel-mar | 41 | /** |
42 | * |
||
43 | */ |
||
635 | daniel-mar | 44 | public function __destruct() { |
45 | // odbc_close_cursor($this->res); |
||
46 | } |
||
47 | |||
1116 | daniel-mar | 48 | /** |
49 | * @return bool |
||
50 | */ |
||
635 | daniel-mar | 51 | public function containsResultSet(): bool { |
52 | return !$this->no_resultset; |
||
53 | } |
||
54 | |||
1116 | daniel-mar | 55 | /** |
56 | * @return int |
||
57 | */ |
||
635 | daniel-mar | 58 | private function num_rows_workaround(): int { |
59 | $dummy = 0; |
||
60 | |||
61 | // go to the end of the result set |
||
62 | $till_eof = 0; |
||
63 | while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++; |
||
64 | |||
65 | // reset cursor |
||
66 | @odbc_fetch_row($this->res, 0); |
||
67 | |||
68 | // count the rows in the result set |
||
69 | $ret = 0; |
||
70 | while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++; |
||
71 | |||
72 | // this would indicate that the odbc_fetch_row() could not reset the cursor |
||
73 | if ($ret < $till_eof) return -1; |
||
74 | |||
75 | // go back to the row were started with |
||
76 | @odbc_fetch_row($this->res, $ret-$till_eof); |
||
77 | |||
78 | return $ret; |
||
79 | } |
||
80 | |||
1116 | daniel-mar | 81 | /** |
82 | * @return int |
||
83 | * @throws OIDplusException |
||
84 | */ |
||
635 | daniel-mar | 85 | public function num_rows(): int { |
86 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
87 | $ret = odbc_num_rows($this->res); |
||
88 | |||
89 | // Workaround for drivers that do not support odbc_num_rows (e.g. Microsoft Access) |
||
90 | if ($ret === -1) $ret = $this->num_rows_workaround(); |
||
91 | |||
92 | if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows')); |
||
93 | |||
94 | return $ret; |
||
95 | } |
||
96 | |||
1116 | daniel-mar | 97 | /** |
98 | * @return array|null |
||
99 | * @throws OIDplusException |
||
100 | */ |
||
635 | daniel-mar | 101 | public function fetch_array()/*: ?array*/ { |
102 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
103 | $ret = odbc_fetch_array($this->res); |
||
104 | if ($ret === false) $ret = null; |
||
105 | if (!is_null($ret)) { |
||
106 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
||
107 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
||
108 | foreach ($ret as &$value) { |
||
109 | if ($value === chr(0)) $value = 0; |
||
110 | if ($value === chr(1)) $value = 1; |
||
111 | } |
||
112 | } |
||
783 | daniel-mar | 113 | |
114 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
||
115 | // We can't quote every single column throughout the whole program, so we use this workaround... |
||
116 | if ($ret) { |
||
117 | $keys = array_keys($ret); |
||
118 | foreach($keys as $key) { |
||
119 | $ret[strtolower($key)]=$ret[$key]; |
||
120 | $ret[strtoupper($key)]=$ret[$key]; |
||
121 | } |
||
122 | } |
||
123 | |||
635 | daniel-mar | 124 | return $ret; |
125 | } |
||
126 | |||
1116 | daniel-mar | 127 | /** |
128 | * @return false|object|null |
||
129 | * @throws OIDplusException |
||
130 | */ |
||
635 | daniel-mar | 131 | public function fetch_object()/*: ?object*/ { |
132 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
133 | $ret = odbc_fetch_object($this->res); |
||
134 | if ($ret === false) $ret = null; |
||
135 | if (!is_null($ret)) { |
||
136 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
||
137 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
||
861 | daniel-mar | 138 | foreach ((array)$ret as &$value) { |
635 | daniel-mar | 139 | if ($value === chr(0)) $value = 0; |
140 | if ($value === chr(1)) $value = 1; |
||
141 | } |
||
142 | } |
||
783 | daniel-mar | 143 | |
144 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
||
145 | // We can't quote every single column throughout the whole program, so we use this workaround... |
||
146 | if ($ret) { |
||
861 | daniel-mar | 147 | foreach ((array)$ret as $name => $val) { |
783 | daniel-mar | 148 | $ret->{strtoupper($name)} = $val; |
149 | $ret->{strtolower($name)} = $val; |
||
150 | } |
||
151 | } |
||
152 | |||
635 | daniel-mar | 153 | return $ret; |
154 | } |
||
783 | daniel-mar | 155 | } |