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 { |
1130 | daniel-mar | 27 | /** |
28 | * @var bool |
||
29 | */ |
||
635 | daniel-mar | 30 | protected $no_resultset; |
1130 | daniel-mar | 31 | |
32 | /** |
||
33 | * @var mixed |
||
34 | */ |
||
635 | daniel-mar | 35 | protected $res; |
1130 | daniel-mar | 36 | |
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
635 | daniel-mar | 40 | protected $all_results = array(); |
1130 | daniel-mar | 41 | |
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
635 | daniel-mar | 45 | protected $cursor = 0; |
46 | |||
1116 | daniel-mar | 47 | /** |
1130 | daniel-mar | 48 | * @param mixed $res |
1116 | daniel-mar | 49 | */ |
635 | daniel-mar | 50 | public function __construct($res) { |
51 | if (is_bool($res) || ($res->numColumns() == 0)) { |
||
52 | // Why do qe need to check numColumns() ? |
||
53 | // We need to do this because SQLite3::query() will always |
||
54 | // return a result, even for Non-SELECT queries. |
||
55 | // If you call fetchArray(), the query (e.g. INSERT) |
||
56 | // will be executed again. |
||
57 | $this->no_resultset = true; |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | if (!$this->no_resultset) { |
||
62 | $this->res = $res; |
||
63 | while ($row = $this->res->fetchArray(SQLITE3_ASSOC)) { |
||
64 | // we need that because there is no numRows() function! |
||
65 | $this->all_results[] = $row; |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | |||
1116 | daniel-mar | 70 | /** |
71 | * |
||
72 | */ |
||
635 | daniel-mar | 73 | public function __destruct() { |
74 | $this->all_results = array(); |
||
75 | if (!is_null($this->res)) { |
||
76 | $this->res->finalize(); |
||
77 | $this->res = null; |
||
78 | } |
||
79 | } |
||
80 | |||
1116 | daniel-mar | 81 | /** |
82 | * @return bool |
||
83 | */ |
||
635 | daniel-mar | 84 | public function containsResultSet(): bool { |
85 | return !$this->no_resultset; |
||
86 | } |
||
87 | |||
1116 | daniel-mar | 88 | /** |
89 | * @return int |
||
90 | * @throws OIDplusException |
||
91 | */ |
||
635 | daniel-mar | 92 | public function num_rows(): int { |
93 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
94 | return count($this->all_results); |
||
95 | } |
||
96 | |||
1116 | daniel-mar | 97 | /** |
98 | * @return array|mixed|null |
||
99 | * @throws OIDplusException |
||
100 | */ |
||
635 | daniel-mar | 101 | public function fetch_array()/*: ?array*/ { |
102 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
103 | |||
104 | //$ret = $this->res->fetchArray(SQLITE3_ASSOC); |
||
105 | $cursor = $this->cursor; |
||
106 | if (!isset($this->all_results[$cursor])) return null; |
||
107 | $ret = $this->all_results[$cursor]; |
||
108 | $cursor++; |
||
109 | $this->cursor = $cursor; |
||
110 | |||
111 | if ($ret === false) $ret = null; |
||
112 | return $ret; |
||
113 | } |
||
114 | |||
1116 | daniel-mar | 115 | /** |
116 | * @return \stdClass|null |
||
117 | * @throws OIDplusException |
||
118 | */ |
||
635 | daniel-mar | 119 | public function fetch_object()/*: ?object*/ { |
120 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
121 | |||
122 | $ary = $this->fetch_array(); |
||
123 | if (!$ary) return null; |
||
124 | |||
1050 | daniel-mar | 125 | $obj = new \stdClass; |
635 | daniel-mar | 126 | foreach ($ary as $name => $val) { |
127 | $obj->$name = $val; |
||
128 | } |
||
129 | return $obj; |
||
130 | } |
||
1050 | daniel-mar | 131 | } |