Subversion Repositories oidplus

Rev

Rev 360 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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