Subversion Repositories oidplus

Rev

Rev 360 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 360 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 OIDplusQueryResultSQLite3 extends OIDplusQueryResult {
22
class OIDplusQueryResultSQLite3 extends OIDplusQueryResult {
21
        protected $no_resultset;
23
        protected $no_resultset;
22
        protected $res;
24
        protected $res;
23
        protected $all_results = array();
25
        protected $all_results = array();
24
        protected $cursor = 0;
26
        protected $cursor = 0;
25
 
27
 
26
        public function __construct($res) {
28
        public function __construct($res) {
27
                if (is_bool($res) || ($res->numColumns() == 0)) {
29
                if (is_bool($res) || ($res->numColumns() == 0)) {
28
                        // Why do qe need to check numColumns() ?
30
                        // Why do qe need to check numColumns() ?
29
                        // We need to do this because SQLite3::query() will always
31
                        // We need to do this because SQLite3::query() will always
30
                        // return a result, even for Non-SELECT queries.
32
                        // return a result, even for Non-SELECT queries.
31
                        // If you call fetchArray(), the query (e.g. INSERT)
33
                        // If you call fetchArray(), the query (e.g. INSERT)
32
                        // will be executed again.
34
                        // will be executed again.
33
                        $this->no_resultset = true;
35
                        $this->no_resultset = true;
34
                        return;
36
                        return;
35
                }
37
                }
36
 
38
 
37
                if (!$this->no_resultset) {
39
                if (!$this->no_resultset) {
38
                        $this->res = $res;
40
                        $this->res = $res;
39
                        while ($row = $this->res->fetchArray(SQLITE3_ASSOC)) {
41
                        while ($row = $this->res->fetchArray(SQLITE3_ASSOC)) {
40
                                // we need that because there is no numRows() function!
42
                                // we need that because there is no numRows() function!
41
                                $this->all_results[] = $row;
43
                                $this->all_results[] = $row;
42
                        }
44
                        }
43
                }
45
                }
44
        }
46
        }
45
 
47
 
46
        public function __destruct() {
48
        public function __destruct() {
47
                $this->all_results = array();
49
                $this->all_results = array();
48
                if (!is_null($this->res)) {
50
                if (!is_null($this->res)) {
49
                        $this->res->finalize();
51
                        $this->res->finalize();
50
                        $this->res = null;
52
                        $this->res = null;
51
                }
53
                }
52
        }
54
        }
53
 
55
 
54
        public function containsResultSet(): bool {
56
        public function containsResultSet(): bool {
55
                return !$this->no_resultset;
57
                return !$this->no_resultset;
56
        }
58
        }
57
 
59
 
58
        public function num_rows(): int {
60
        public function num_rows(): int {
59
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
61
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
60
                return count($this->all_results);
62
                return count($this->all_results);
61
        }
63
        }
62
 
64
 
63
        public function fetch_array()/*: ?array*/ {
65
        public function fetch_array()/*: ?array*/ {
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
 
67
 
66
                //$ret = $this->res->fetchArray(SQLITE3_ASSOC);
68
                //$ret = $this->res->fetchArray(SQLITE3_ASSOC);
67
                $cursor = $this->cursor;
69
                $cursor = $this->cursor;
68
                if (!isset($this->all_results[$cursor])) return null;
70
                if (!isset($this->all_results[$cursor])) return null;
69
                $ret = $this->all_results[$cursor];
71
                $ret = $this->all_results[$cursor];
70
                $cursor++;
72
                $cursor++;
71
                $this->cursor = $cursor;
73
                $this->cursor = $cursor;
72
 
74
 
73
                if ($ret === false) $ret = null;
75
                if ($ret === false) $ret = null;
74
                return $ret;
76
                return $ret;
75
        }
77
        }
76
 
78
 
77
        public function fetch_object()/*: ?object*/ {
79
        public function fetch_object()/*: ?object*/ {
78
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
80
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
79
 
81
 
80
                $ary = $this->fetch_array();
82
                $ary = $this->fetch_array();
81
                if (!$ary) return null;
83
                if (!$ary) return null;
82
 
84
 
83
                $obj = new stdClass;
85
                $obj = new stdClass;
84
                foreach ($ary as $name => $val) {
86
                foreach ($ary as $name => $val) {
85
                        $obj->$name = $val;
87
                        $obj->$name = $val;
86
                }
88
                }
87
                return $obj;
89
                return $obj;
88
        }
90
        }
89
}
91
}