Subversion Repositories oidplus

Rev

Rev 502 | 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
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
277 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
 
511 daniel-mar 20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
277 daniel-mar 22
class OIDplusQueryResultODBC 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
                // odbc_close_cursor($this->res);
36
        }
37
 
38
        public function containsResultSet(): bool {
39
                return !$this->no_resultset;
40
        }
41
 
502 daniel-mar 42
        private function num_rows_workaround(): int {
43
                $dummy = 0;
44
 
45
                // go to the end of the result set
46
                $till_eof = 0;
47
                while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++;
48
 
49
                // reset cursor
50
                @odbc_fetch_row($this->res, 0);
51
 
52
                // count the rows in the result set
53
                $ret = 0;
54
                while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++;
55
 
56
                // this would indicate that the odbc_fetch_row() could not reset the cursor
57
                if ($ret < $till_eof) return -1;
58
 
59
                // go back to the row were started with
60
                @odbc_fetch_row($this->res, $ret-$till_eof);
61
 
62
                return $ret;
63
        }
64
 
277 daniel-mar 65
        public function num_rows(): int {
360 daniel-mar 66
                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 67
                $ret = odbc_num_rows($this->res);
68
 
69
                // Workaround for drivers that do not support odbc_num_rows (e.g. Microsoft Access)
70
                if ($ret === -1) $ret = $this->num_rows_workaround();
71
 
72
                if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows'));
73
 
74
                return $ret;
277 daniel-mar 75
        }
76
 
77
        public function fetch_array()/*: ?array*/ {
360 daniel-mar 78
                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 79
                $ret = odbc_fetch_array($this->res);
80
                if ($ret === false) $ret = null;
81
                if (!is_null($ret)) {
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.
84
                        foreach ($ret as &$value) {
85
                                if ($value === chr(0)) $value = 0;
86
                                if ($value === chr(1)) $value = 1;
87
                        }
88
                }
89
                return $ret;
90
        }
91
 
92
        public function fetch_object()/*: ?object*/ {
360 daniel-mar 93
                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 94
                $ret = odbc_fetch_object($this->res);
95
                if ($ret === false) $ret = null;
96
                if (!is_null($ret)) {
97
                        // ODBC gives bit(1) as binary, MySQL as integer and PDO as string.
98
                        // We'll do it like MySQL does, even if ODBC is actually more correct.
99
                        foreach ($ret as &$value) {
100
                                if ($value === chr(0)) $value = 0;
101
                                if ($value === chr(1)) $value = 1;
102
                        }
103
                }
104
                return $ret;
105
        }
360 daniel-mar 106
}