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 OIDplusQueryResultPDO 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 | } |
||
502 | daniel-mar | 32 | |
33 | // This way we can simulate MARS (Multiple Active Result Sets) so that the test case "Simultanous prepared statements" works |
||
34 | $this->prefetchedArray = $this->res->fetchAll(); |
||
277 | daniel-mar | 35 | } |
36 | |||
37 | public function __destruct() { |
||
38 | if ($this->res) $this->res->closeCursor(); |
||
39 | } |
||
40 | |||
41 | public function containsResultSet(): bool { |
||
42 | return !$this->no_resultset; |
||
43 | } |
||
44 | |||
502 | daniel-mar | 45 | private $prefetchedArray = null; |
46 | private $countAlreadyFetched = 0; |
||
47 | |||
277 | daniel-mar | 48 | public function num_rows(): int { |
502 | daniel-mar | 49 | if (!is_null($this->prefetchedArray)) { |
50 | return count($this->prefetchedArray) + $this->countAlreadyFetched; |
||
51 | } |
||
52 | |||
360 | daniel-mar | 53 | 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 | 54 | $ret = $this->res->rowCount(); |
55 | |||
56 | // -1 can happen when PDO is connected via ODBC that is running a driver that does not support num_rows (e.g. Microsoft Access) |
||
57 | // if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows')); |
||
58 | if ($ret === -1) { |
||
59 | $this->prefetchedArray = $this->res->fetchAll(); |
||
60 | return count($this->prefetchedArray) + $this->countAlreadyFetched; |
||
61 | } |
||
62 | |||
63 | return $ret; |
||
277 | daniel-mar | 64 | } |
65 | |||
66 | public function fetch_array()/*: ?array*/ { |
||
502 | daniel-mar | 67 | if (!is_null($this->prefetchedArray)) { |
68 | $ret = array_shift($this->prefetchedArray); |
||
69 | } else { |
||
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 | $ret = $this->res->fetch(PDO::FETCH_ASSOC); |
||
72 | if ($ret === false) $ret = null; |
||
73 | } |
||
74 | if ($ret) $this->countAlreadyFetched++; |
||
277 | daniel-mar | 75 | return $ret; |
76 | } |
||
77 | |||
502 | daniel-mar | 78 | private static function array_to_stdobj($ary) { |
79 | $obj = new stdClass; |
||
80 | foreach ($ary as $name => $val) { |
||
81 | $obj->$name = $val; |
||
82 | } |
||
83 | return $obj; |
||
84 | } |
||
85 | |||
277 | daniel-mar | 86 | public function fetch_object()/*: ?object*/ { |
502 | daniel-mar | 87 | if (!is_null($this->prefetchedArray)) { |
88 | $ary = array_shift($this->prefetchedArray); |
||
89 | $ret = is_null($ary) ? null : self::array_to_stdobj($ary); |
||
90 | } else { |
||
91 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
92 | $ret = $this->res->fetch(PDO::FETCH_OBJ); |
||
93 | if ($ret === false) $ret = null; |
||
94 | } |
||
95 | if ($ret) $this->countAlreadyFetched++; |
||
277 | daniel-mar | 96 | return $ret; |
97 | } |
||
360 | daniel-mar | 98 | } |