Rev 1086 | Rev 1130 | 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 OIDplusQueryResultPgSql extends OIDplusQueryResult { |
27 | protected $no_resultset; |
||
28 | protected $res; |
||
29 | |||
1116 | daniel-mar | 30 | /** |
31 | * @param $res |
||
32 | */ |
||
635 | daniel-mar | 33 | public function __construct($res) { |
34 | $this->no_resultset = is_bool($res); |
||
35 | |||
36 | if (!$this->no_resultset) { |
||
37 | $this->res = $res; |
||
38 | } |
||
39 | } |
||
40 | |||
1116 | daniel-mar | 41 | /** |
42 | * |
||
43 | */ |
||
635 | daniel-mar | 44 | public function __destruct() { |
45 | if ($this->res) { |
||
46 | pg_free_result($this->res); |
||
47 | } |
||
48 | } |
||
49 | |||
1116 | daniel-mar | 50 | /** |
51 | * @return bool |
||
52 | */ |
||
635 | daniel-mar | 53 | public function containsResultSet(): bool { |
54 | return !$this->no_resultset; |
||
55 | } |
||
56 | |||
1116 | daniel-mar | 57 | /** |
58 | * @return int |
||
59 | * @throws OIDplusException |
||
60 | */ |
||
635 | daniel-mar | 61 | public function num_rows(): int { |
62 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
63 | return pg_num_rows($this->res); |
||
64 | } |
||
65 | |||
1116 | daniel-mar | 66 | /** |
67 | * @return array|false|null |
||
68 | * @throws OIDplusException |
||
69 | */ |
||
635 | daniel-mar | 70 | public function fetch_array()/*: ?array*/ { |
71 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
72 | $ret = pg_fetch_array($this->res, null, PGSQL_ASSOC); |
||
73 | if ($ret === false) $ret = null; |
||
74 | if (!is_null($ret)) { |
||
75 | foreach ($ret as $key => &$value){ |
||
76 | $type = pg_field_type($this->res,pg_field_num($this->res, $key)); |
||
77 | if ($type == 'bool'){ |
||
78 | $value = ($value == 't'); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | return $ret; |
||
83 | } |
||
84 | |||
1116 | daniel-mar | 85 | /** |
86 | * @return false|object|null |
||
87 | * @throws OIDplusException |
||
88 | */ |
||
635 | daniel-mar | 89 | public function fetch_object()/*: ?object*/ { |
90 | if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)')); |
||
91 | $ret = pg_fetch_object($this->res); |
||
92 | if ($ret === false) $ret = null; |
||
93 | if (!is_null($ret)) { |
||
94 | foreach ($ret as $key => &$value){ |
||
95 | $type = pg_field_type($this->res,pg_field_num($this->res, $key)); |
||
96 | if ($type == 'bool'){ |
||
97 | $value = ($value == 't'); |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | return $ret; |
||
102 | } |
||
103 | } |