Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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