Subversion Repositories oidplus

Rev

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

Rev 1151 Rev 1152
Line 34... Line 34...
34
         * @return bool
34
         * @return bool
35
         */
35
         */
36
        private $no_resultset = false;
36
        private $no_resultset = false;
37
 
37
 
38
        /**
38
        /**
-
 
39
         * @return int
-
 
40
         */
-
 
41
        private $cursor = 0;
-
 
42
 
-
 
43
        /**
-
 
44
         * @return int
-
 
45
         */
-
 
46
        private $record_count = 0;
-
 
47
 
-
 
48
        /**
39
        * @param OIDplusQueryResult $res
49
        * @param OIDplusQueryResult $res
40
        * @param string $dbField
50
        * @param string $dbField
41
        */
51
        */
42
        public function __construct(OIDplusQueryResult $res, string $dbField) {
52
        public function __construct(OIDplusQueryResult $res, string $dbField) {
43
                $this->no_resultset = !$res->containsResultSet();
53
                $this->no_resultset = !$res->containsResultSet();
44
 
54
 
45
                if (!$this->no_resultset) {
55
                if (!$this->no_resultset) {
-
 
56
                        $this->rows = array();
46
                        while ($row = $res->fetch_array()) {
57
                        while ($row = $res->fetch_array()) {
47
                                $this->rows[] = $row;
58
                                $this->rows[] = $row;
48
                        }
59
                        }
-
 
60
                        $this->cursor = 0;
-
 
61
                        $this->record_count = count($this->rows);
49
 
62
 
50
                        // Sort $this->rows by field $dbField
63
                        // Sort $this->rows by field $dbField
51
                        natsort_field($this->rows, $dbField);
64
                        natsort_field($this->rows, $dbField);
-
 
65
                } else {
-
 
66
                        $this->rows = array();
-
 
67
                        $this->cursor = 0;
-
 
68
                        $this->record_count = 0;
52
                }
69
                }
53
        }
70
        }
54
 
71
 
55
        /**
72
        /**
56
         * @return bool
73
         * @return bool
Line 60... Line 77...
60
        }
77
        }
61
 
78
 
62
        /**
79
        /**
63
         * @return int
80
         * @return int
64
         */
81
         */
65
        public function num_rows(): int {
82
        protected function do_num_rows(): int {
66
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
-
 
67
                return count($this->rows);
83
                return count($this->rows);
68
        }
84
        }
69
 
85
 
70
        /**
86
        /**
71
         * @return array|null
87
         * @return array|null
72
         */
88
         */
73
        public function fetch_array()/*: ?array*/ {
89
        protected function do_fetch_array()/*: ?array*/ {
74
                if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
-
 
75
                return array_shift($this->rows);
90
                //return array_shift($this->rows);
76
        }
-
 
77
 
-
 
78
        /**
-
 
79
         * @return object|null
-
 
80
         */
-
 
81
        public function fetch_object()/*: ?object*/ {
91
                // This is probably faster for large arrays:
82
                if ($this->no_resultset) throw new OIDplusConfigInitializationException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
-
 
83
 
-
 
84
                $ary = $this->fetch_array();
-
 
85
                if (!$ary) return null;
92
                if ($this->cursor >= $this->record_count) return null;
86
 
-
 
87
                $obj = new \stdClass;
-
 
88
                foreach ($ary as $name => $val) {
93
                return $this->rows[$this->cursor++];
89
                        $obj->$name = $val;
-
 
90
                }
-
 
91
                return $obj;
-
 
92
        }
94
        }
93
 
95
 
94
}
96
}