Rev 861 | Rev 1086 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 861 | Rev 1050 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 - 2021 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(); |
20 | namespace ViaThinkSoft\OIDplus; |
21 | 21 | ||
22 | class OIDplusQueryResultODBC extends OIDplusQueryResult { |
22 | class OIDplusQueryResultODBC extends OIDplusQueryResult { |
23 | protected $no_resultset; |
23 | protected $no_resultset; |
24 | protected $res; |
24 | protected $res; |
25 | 25 | ||
26 | public function __construct($res) { |
26 | public function __construct($res) { |
27 | $this->no_resultset = is_bool($res); |
27 | $this->no_resultset = is_bool($res); |
28 | 28 | ||
29 | if (!$this->no_resultset) { |
29 | if (!$this->no_resultset) { |
30 | $this->res = $res; |
30 | $this->res = $res; |
31 | } |
31 | } |
32 | } |
32 | } |
33 | 33 | ||
34 | public function __destruct() { |
34 | public function __destruct() { |
35 | // odbc_close_cursor($this->res); |
35 | // odbc_close_cursor($this->res); |
36 | } |
36 | } |
37 | 37 | ||
38 | public function containsResultSet(): bool { |
38 | public function containsResultSet(): bool { |
39 | return !$this->no_resultset; |
39 | return !$this->no_resultset; |
40 | } |
40 | } |
41 | 41 | ||
42 | private function num_rows_workaround(): int { |
42 | private function num_rows_workaround(): int { |
43 | $dummy = 0; |
43 | $dummy = 0; |
44 | 44 | ||
45 | // go to the end of the result set |
45 | // go to the end of the result set |
46 | $till_eof = 0; |
46 | $till_eof = 0; |
47 | while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++; |
47 | while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++; |
48 | 48 | ||
49 | // reset cursor |
49 | // reset cursor |
50 | @odbc_fetch_row($this->res, 0); |
50 | @odbc_fetch_row($this->res, 0); |
51 | 51 | ||
52 | // count the rows in the result set |
52 | // count the rows in the result set |
53 | $ret = 0; |
53 | $ret = 0; |
54 | while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++; |
54 | while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++; |
55 | 55 | ||
56 | // 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 |
57 | if ($ret < $till_eof) return -1; |
57 | if ($ret < $till_eof) return -1; |
58 | 58 | ||
59 | // go back to the row were started with |
59 | // go back to the row were started with |
60 | @odbc_fetch_row($this->res, $ret-$till_eof); |
60 | @odbc_fetch_row($this->res, $ret-$till_eof); |
61 | 61 | ||
62 | return $ret; |
62 | return $ret; |
63 | } |
63 | } |
64 | 64 | ||
65 | public function num_rows(): int { |
65 | public function num_rows(): int { |
66 | 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)')); |
67 | $ret = odbc_num_rows($this->res); |
67 | $ret = odbc_num_rows($this->res); |
68 | 68 | ||
69 | // 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) |
70 | if ($ret === -1) $ret = $this->num_rows_workaround(); |
70 | if ($ret === -1) $ret = $this->num_rows_workaround(); |
71 | 71 | ||
72 | 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')); |
73 | 73 | ||
74 | return $ret; |
74 | return $ret; |
75 | } |
75 | } |
76 | 76 | ||
77 | public function fetch_array()/*: ?array*/ { |
77 | public function fetch_array()/*: ?array*/ { |
78 | 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)')); |
79 | $ret = odbc_fetch_array($this->res); |
79 | $ret = odbc_fetch_array($this->res); |
80 | if ($ret === false) $ret = null; |
80 | if ($ret === false) $ret = null; |
81 | if (!is_null($ret)) { |
81 | if (!is_null($ret)) { |
82 | // 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. |
83 | // 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. |
84 | foreach ($ret as &$value) { |
84 | foreach ($ret as &$value) { |
85 | if ($value === chr(0)) $value = 0; |
85 | if ($value === chr(0)) $value = 0; |
86 | if ($value === chr(1)) $value = 1; |
86 | if ($value === chr(1)) $value = 1; |
87 | } |
87 | } |
88 | } |
88 | } |
89 | 89 | ||
90 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
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... |
91 | // We can't quote every single column throughout the whole program, so we use this workaround... |
92 | if ($ret) { |
92 | if ($ret) { |
93 | $keys = array_keys($ret); |
93 | $keys = array_keys($ret); |
94 | foreach($keys as $key) { |
94 | foreach($keys as $key) { |
95 | $ret[strtolower($key)]=$ret[$key]; |
95 | $ret[strtolower($key)]=$ret[$key]; |
96 | $ret[strtoupper($key)]=$ret[$key]; |
96 | $ret[strtoupper($key)]=$ret[$key]; |
97 | } |
97 | } |
98 | } |
98 | } |
99 | 99 | ||
100 | return $ret; |
100 | return $ret; |
101 | } |
101 | } |
102 | 102 | ||
103 | public function fetch_object()/*: ?object*/ { |
103 | public function fetch_object()/*: ?object*/ { |
104 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
104 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
105 | $ret = odbc_fetch_object($this->res); |
105 | $ret = odbc_fetch_object($this->res); |
106 | if ($ret === false) $ret = null; |
106 | if ($ret === false) $ret = null; |
107 | if (!is_null($ret)) { |
107 | if (!is_null($ret)) { |
108 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
108 | // ODBC gives bit(1) as binary, MySQL as integer and PDO as string. |
109 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
109 | // We'll do it like MySQL does, even if ODBC is actually more correct. |
110 | foreach ((array)$ret as &$value) { |
110 | foreach ((array)$ret as &$value) { |
111 | if ($value === chr(0)) $value = 0; |
111 | if ($value === chr(0)) $value = 0; |
112 | if ($value === chr(1)) $value = 1; |
112 | if ($value === chr(1)) $value = 1; |
113 | } |
113 | } |
114 | } |
114 | } |
115 | 115 | ||
116 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
116 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
117 | // We can't quote every single column throughout the whole program, so we use this workaround... |
117 | // We can't quote every single column throughout the whole program, so we use this workaround... |
118 | if ($ret) { |
118 | if ($ret) { |
119 | foreach ((array)$ret as $name => $val) { |
119 | foreach ((array)$ret as $name => $val) { |
120 | $ret->{strtoupper($name)} = $val; |
120 | $ret->{strtoupper($name)} = $val; |
121 | $ret->{strtolower($name)} = $val; |
121 | $ret->{strtolower($name)} = $val; |
122 | } |
122 | } |
123 | } |
123 | } |
124 | 124 | ||
125 | return $ret; |
125 | return $ret; |
126 | } |
126 | } |
127 | } |
127 | } |
128 | 128 |