Subversion Repositories oidplus

Rev

Rev 277 | 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
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
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
 
20
class OIDplusQueryResultPgSql extends OIDplusQueryResult {
21
        protected $no_resultset;
22
        protected $res;
23
 
24
        public function __construct($res) {
25
                $this->no_resultset = is_bool($res);
26
 
27
                if (!$this->no_resultset) {
28
                        $this->res = $res;
29
                }
30
        }
31
 
32
        public function __destruct() {
33
                if ($this->res) {
34
                        pg_free_result($this->res);
35
                }
36
        }
37
 
38
        public function containsResultSet(): bool {
39
                return !$this->no_resultset;
40
        }
41
 
42
        public function num_rows(): int {
360 daniel-mar 43
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
277 daniel-mar 44
                return pg_num_rows($this->res);
45
        }
46
 
47
        public function fetch_array()/*: ?array*/ {
360 daniel-mar 48
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
277 daniel-mar 49
                $ret = pg_fetch_array($this->res, null, PGSQL_ASSOC);
50
                if ($ret === false) $ret = null;
51
                if (!is_null($ret)) {
52
                        foreach ($ret as $key => &$value){
53
                                $type = pg_field_type($this->res,pg_field_num($this->res, $key));
54
                                if ($type == 'bool'){
55
                                        $value = ($value == 't');
56
                                }
57
                        }
58
                }
59
                return $ret;
60
        }
61
 
62
        public function fetch_object()/*: ?object*/ {
360 daniel-mar 63
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
277 daniel-mar 64
                $ret = pg_fetch_object($this->res);
65
                if ($ret === false) $ret = null;
66
                if (!is_null($ret)) {
67
                        foreach ($ret as $key => &$value){
68
                                $type = pg_field_type($this->res,pg_field_num($this->res, $key));
69
                                if ($type == 'bool'){
70
                                        $value = ($value == 't');
71
                                }
72
                        }
73
                }
74
                return $ret;
75
        }
360 daniel-mar 76
}