Subversion Repositories oidplus

Rev

Rev 360 | 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 OIDplusQueryResultPgSql 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
                }
32
        }
33
 
34
        public function __destruct() {
35
                if ($this->res) {
36
                        pg_free_result($this->res);
37
                }
38
        }
39
 
40
        public function containsResultSet(): bool {
41
                return !$this->no_resultset;
42
        }
43
 
44
        public function num_rows(): int {
360 daniel-mar 45
                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 46
                return pg_num_rows($this->res);
47
        }
48
 
49
        public function fetch_array()/*: ?array*/ {
360 daniel-mar 50
                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 51
                $ret = pg_fetch_array($this->res, null, PGSQL_ASSOC);
52
                if ($ret === false) $ret = null;
53
                if (!is_null($ret)) {
54
                        foreach ($ret as $key => &$value){
55
                                $type = pg_field_type($this->res,pg_field_num($this->res, $key));
56
                                if ($type == 'bool'){
57
                                        $value = ($value == 't');
58
                                }
59
                        }
60
                }
61
                return $ret;
62
        }
63
 
64
        public function fetch_object()/*: ?object*/ {
360 daniel-mar 65
                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 66
                $ret = pg_fetch_object($this->res);
67
                if ($ret === false) $ret = null;
68
                if (!is_null($ret)) {
69
                        foreach ($ret as $key => &$value){
70
                                $type = pg_field_type($this->res,pg_field_num($this->res, $key));
71
                                if ($type == 'bool'){
72
                                        $value = ($value == 't');
73
                                }
74
                        }
75
                }
76
                return $ret;
77
        }
360 daniel-mar 78
}