Rev 360 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
277 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
5 | * Copyright 2019 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 | |||
20 | class OIDplusQueryResultODBC extends OIDplusQueryResult { |
||
21 | protected $no_resultset; |
||
22 | protected $res; |
||
23 | |||
24 | public function __construct($res) { |
||
25 | $this->no_resultset = is_bool($res); |
||
26 | |||
27 | if (!$this->no_resultset) { |
||
28 | $this->res = $res; |
||
29 | } |
||
30 | } |
||
31 | |||
32 | public function __destruct() { |
||
33 | // odbc_close_cursor($this->res); |
||
34 | } |
||
35 | |||
36 | public function containsResultSet(): bool { |
||
37 | return !$this->no_resultset; |
||
38 | } |
||
39 | |||
502 | daniel-mar | 40 | private function num_rows_workaround(): int { |
41 | $dummy = 0; |
||
42 | |||
43 | // go to the end of the result set |
||
44 | $till_eof = 0; |
||
45 | while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++; |
||
46 | |||
47 | // reset cursor |
||
48 | @odbc_fetch_row($this->res, 0); |
||
49 | |||
50 | // count the rows in the result set |
||
51 | $ret = 0; |
||
52 | while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++; |
||
53 | |||
54 | // this would indicate that the odbc_fetch_row() could not reset the cursor |
||
55 | if ($ret < $till_eof) return -1; |
||
56 | |||
57 | // go back to the row were started with |
||
58 | @odbc_fetch_row($this->res, $ret-$till_eof); |
||
59 | |||
60 | return $ret; |
||
61 | } |
||
62 | |||
277 | daniel-mar | 63 | public function num_rows(): int { |
360 | daniel-mar | 64 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
502 | daniel-mar | 65 | $ret = odbc_num_rows($this->res); |
66 | |||
67 | // Workaround for drivers that do not support odbc_num_rows (e.g. Microsoft Access) |
||
68 | if ($ret === -1) $ret = $this->num_rows_workaround(); |
||
69 | |||
70 | if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows')); |
||
71 | |||
72 | return $ret; |
||
277 | daniel-mar | 73 | } |
74 | |||
75 | public function fetch_array()/*: ?array*/ { |
||
360 | daniel-mar | 76 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
277 | daniel-mar | 77 | $ret = odbc_fetch_array($this->res); |
78 | if ($ret === false) $ret = null; |
||
79 | if (!is_null($ret)) { |
||
80 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
||
81 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
||
82 | foreach ($ret as &$value) { |
||
83 | if ($value === chr(0)) $value = 0; |
||
84 | if ($value === chr(1)) $value = 1; |
||
85 | } |
||
86 | } |
||
87 | return $ret; |
||
88 | } |
||
89 | |||
90 | public function fetch_object()/*: ?object*/ { |
||
360 | daniel-mar | 91 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
277 | daniel-mar | 92 | $ret = odbc_fetch_object($this->res); |
93 | if ($ret === false) $ret = null; |
||
94 | if (!is_null($ret)) { |
||
95 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
||
96 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
||
97 | foreach ($ret as &$value) { |
||
98 | if ($value === chr(0)) $value = 0; |
||
99 | if ($value === chr(1)) $value = 1; |
||
100 | } |
||
101 | } |
||
102 | return $ret; |
||
103 | } |
||
360 | daniel-mar | 104 | } |