Rev 502 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 502 | Rev 511 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with 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 |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
- | 20 | if (!defined('INSIDE_OIDPLUS')) die(); |
|
- | 21 | ||
20 | class OIDplusQueryResultODBC extends OIDplusQueryResult { |
22 | class OIDplusQueryResultODBC extends OIDplusQueryResult { |
21 | protected $no_resultset; |
23 | protected $no_resultset; |
22 | protected $res; |
24 | protected $res; |
23 | 25 | ||
24 | public function __construct($res) { |
26 | public function __construct($res) { |
25 | $this->no_resultset = is_bool($res); |
27 | $this->no_resultset = is_bool($res); |
26 | 28 | ||
27 | if (!$this->no_resultset) { |
29 | if (!$this->no_resultset) { |
28 | $this->res = $res; |
30 | $this->res = $res; |
29 | } |
31 | } |
30 | } |
32 | } |
31 | 33 | ||
32 | public function __destruct() { |
34 | public function __destruct() { |
33 | // odbc_close_cursor($this->res); |
35 | // odbc_close_cursor($this->res); |
34 | } |
36 | } |
35 | 37 | ||
36 | public function containsResultSet(): bool { |
38 | public function containsResultSet(): bool { |
37 | return !$this->no_resultset; |
39 | return !$this->no_resultset; |
38 | } |
40 | } |
39 | 41 | ||
40 | private function num_rows_workaround(): int { |
42 | private function num_rows_workaround(): int { |
41 | $dummy = 0; |
43 | $dummy = 0; |
42 | 44 | ||
43 | // go to the end of the result set |
45 | // go to the end of the result set |
44 | $till_eof = 0; |
46 | $till_eof = 0; |
45 | while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++; |
47 | while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++; |
46 | 48 | ||
47 | // reset cursor |
49 | // reset cursor |
48 | @odbc_fetch_row($this->res, 0); |
50 | @odbc_fetch_row($this->res, 0); |
49 | 51 | ||
50 | // count the rows in the result set |
52 | // count the rows in the result set |
51 | $ret = 0; |
53 | $ret = 0; |
52 | while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++; |
54 | while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++; |
53 | 55 | ||
54 | // this would indicate that the odbc_fetch_row() could not reset the cursor |
56 | // this would indicate that the odbc_fetch_row() could not reset the cursor |
55 | if ($ret < $till_eof) return -1; |
57 | if ($ret < $till_eof) return -1; |
56 | 58 | ||
57 | // go back to the row were started with |
59 | // go back to the row were started with |
58 | @odbc_fetch_row($this->res, $ret-$till_eof); |
60 | @odbc_fetch_row($this->res, $ret-$till_eof); |
59 | 61 | ||
60 | return $ret; |
62 | return $ret; |
61 | } |
63 | } |
62 | 64 | ||
63 | public function num_rows(): int { |
65 | public function num_rows(): int { |
64 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
66 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
65 | $ret = odbc_num_rows($this->res); |
67 | $ret = odbc_num_rows($this->res); |
66 | 68 | ||
67 | // Workaround for drivers that do not support odbc_num_rows (e.g. Microsoft Access) |
69 | // Workaround for drivers that do not support odbc_num_rows (e.g. Microsoft Access) |
68 | if ($ret === -1) $ret = $this->num_rows_workaround(); |
70 | if ($ret === -1) $ret = $this->num_rows_workaround(); |
69 | 71 | ||
70 | if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows')); |
72 | if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows')); |
71 | 73 | ||
72 | return $ret; |
74 | return $ret; |
73 | } |
75 | } |
74 | 76 | ||
75 | public function fetch_array()/*: ?array*/ { |
77 | public function fetch_array()/*: ?array*/ { |
76 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
78 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
77 | $ret = odbc_fetch_array($this->res); |
79 | $ret = odbc_fetch_array($this->res); |
78 | if ($ret === false) $ret = null; |
80 | if ($ret === false) $ret = null; |
79 | if (!is_null($ret)) { |
81 | if (!is_null($ret)) { |
80 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
82 | // 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. |
83 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
82 | foreach ($ret as &$value) { |
84 | foreach ($ret as &$value) { |
83 | if ($value === chr(0)) $value = 0; |
85 | if ($value === chr(0)) $value = 0; |
84 | if ($value === chr(1)) $value = 1; |
86 | if ($value === chr(1)) $value = 1; |
85 | } |
87 | } |
86 | } |
88 | } |
87 | return $ret; |
89 | return $ret; |
88 | } |
90 | } |
89 | 91 | ||
90 | public function fetch_object()/*: ?object*/ { |
92 | public function fetch_object()/*: ?object*/ { |
91 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
93 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
92 | $ret = odbc_fetch_object($this->res); |
94 | $ret = odbc_fetch_object($this->res); |
93 | if ($ret === false) $ret = null; |
95 | if ($ret === false) $ret = null; |
94 | if (!is_null($ret)) { |
96 | if (!is_null($ret)) { |
95 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
97 | // 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. |
98 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
97 | foreach ($ret as &$value) { |
99 | foreach ($ret as &$value) { |
98 | if ($value === chr(0)) $value = 0; |
100 | if ($value === chr(0)) $value = 0; |
99 | if ($value === chr(1)) $value = 1; |
101 | if ($value === chr(1)) $value = 1; |
100 | } |
102 | } |
101 | } |
103 | } |
102 | return $ret; |
104 | return $ret; |
103 | } |
105 | } |
104 | } |
106 | } |