Rev 1152 | Rev 1232 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1152 | Rev 1156 | ||
---|---|---|---|
Line 33... | Line 33... | ||
33 | * @var mixed |
33 | * @var mixed |
34 | */ |
34 | */ |
35 | protected $res; |
35 | protected $res; |
36 | 36 | ||
37 | /** |
37 | /** |
38 | * @var array|null |
- | |
39 | */ |
- | |
40 | private $prefetchedArray = null; |
- | |
41 | - | ||
42 | /** |
- | |
43 | * @var int |
- | |
44 | */ |
- | |
45 | private $countAlreadyFetched = 0; |
- | |
46 | - | ||
47 | /** |
- | |
48 | * @param mixed $res |
38 | * @param mixed $res |
49 | */ |
39 | */ |
50 | public function __construct($res) { |
40 | public function __construct($res) { |
51 | $this->no_resultset = is_bool($res); |
41 | $this->no_resultset = is_bool($res); |
52 | 42 | ||
Line 71... | Line 61... | ||
71 | public function containsResultSet(): bool { |
61 | public function containsResultSet(): bool { |
72 | return !$this->no_resultset; |
62 | return !$this->no_resultset; |
73 | } |
63 | } |
74 | 64 | ||
75 | /** |
65 | /** |
76 | * @return int |
66 | * @return void |
77 | */ |
67 | */ |
78 | protected function do_num_rows(): int { |
68 | public function prefetchAll() { |
79 | if (!is_null($this->prefetchedArray)) { |
69 | if (!is_null($this->prefetchedArray)) return; |
80 | return count($this->prefetchedArray) + $this->countAlreadyFetched; |
70 | $this->prefetchedArray = $this->res->fetchAll(); |
81 | } |
71 | } |
82 | 72 | ||
- | 73 | /** |
|
- | 74 | * @return int |
|
- | 75 | */ |
|
- | 76 | protected function do_num_rows(): int { |
|
83 | $ret = $this->res->rowCount(); |
77 | $ret = $this->res->rowCount(); |
84 | 78 | ||
85 | // -1 can happen when PDO is connected via ODBC that is running a driver that does not support num_rows (e.g. Microsoft Access) |
79 | // -1 can happen when PDO is connected via ODBC that is running a driver that does not support num_rows (e.g. Microsoft Access) |
86 | // if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows')); |
- | |
87 | if ($ret === -1) { |
80 | if ($ret === -1) { |
88 | $this->prefetchedArray = $this->res->fetchAll(); |
81 | $this->prefetchAll(); |
89 | return count($this->prefetchedArray) + $this->countAlreadyFetched; |
82 | return count($this->prefetchedArray) + $this->countAlreadyFetched; |
90 | } |
83 | } |
91 | 84 | ||
92 | return $ret; |
85 | return $ret; |
93 | } |
86 | } |
94 | 87 | ||
95 | /** |
88 | /** |
96 | * @return array|mixed|null |
89 | * @return array|null |
97 | */ |
90 | */ |
98 | protected function do_fetch_array()/*: ?array*/ { |
91 | protected function do_fetch_array()/*: ?array*/ { |
99 | if (!is_null($this->prefetchedArray)) { |
- | |
100 | $ret = array_shift($this->prefetchedArray); |
- | |
101 | } else { |
- | |
102 | $ret = $this->res->fetch(\PDO::FETCH_ASSOC); |
92 | $ret = $this->res->fetch(\PDO::FETCH_ASSOC); |
103 | if ($ret === false) $ret = null; |
93 | if ($ret === false) $ret = null; |
104 | } |
- | |
105 | if ($ret) $this->countAlreadyFetched++; |
- | |
106 | - | ||
107 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
- | |
108 | // We can't quote every single column throughout the whole program, so we use this workaround... |
- | |
109 | if ($ret) { |
- | |
110 | $keys = array_keys($ret); |
- | |
111 | foreach($keys as $key) { |
- | |
112 | $ret[strtolower($key)]=$ret[$key]; |
- | |
113 | $ret[strtoupper($key)]=$ret[$key]; |
- | |
114 | } |
- | |
115 | } |
- | |
116 | - | ||
117 | return $ret; |
94 | return $ret; |
118 | } |
95 | } |
119 | 96 | ||
120 | /** |
97 | /** |
121 | * @param array $ary |
- | |
122 | * @return \stdClass |
- | |
123 | */ |
- | |
124 | private static function array_to_stdobj(array $ary): \stdClass { |
- | |
125 | $obj = new \stdClass; |
- | |
126 | foreach ($ary as $name => $val) { |
- | |
127 | $obj->$name = $val; |
- | |
128 | } |
- | |
129 | return $obj; |
- | |
130 | } |
- | |
131 | - | ||
132 | /** |
- | |
133 | * @return object|\stdClass|null |
98 | * @return object|null |
134 | */ |
99 | */ |
135 | protected function do_fetch_object()/*: ?object*/ { |
100 | protected function do_fetch_object()/*: ?object*/ { |
136 | if (!is_null($this->prefetchedArray)) { |
- | |
137 | $ary = array_shift($this->prefetchedArray); |
- | |
138 | $ret = is_null($ary) ? null : self::array_to_stdobj($ary); |
- | |
139 | } else { |
- | |
140 | $ret = $this->res->fetch(\PDO::FETCH_OBJ); |
101 | $ret = $this->res->fetch(\PDO::FETCH_OBJ); |
141 | if ($ret === false) $ret = null; |
102 | if ($ret === false) $ret = null; |
142 | } |
- | |
143 | if ($ret) $this->countAlreadyFetched++; |
- | |
144 | - | ||
145 | // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case |
- | |
146 | // We can't quote every single column throughout the whole program, so we use this workaround... |
- | |
147 | if ($ret) { |
- | |
148 | foreach ($ret as $name => $val) { |
- | |
149 | $ret->{strtoupper($name)} = $val; |
- | |
150 | $ret->{strtolower($name)} = $val; |
- | |
151 | } |
- | |
152 | } |
- | |
153 | - | ||
154 | return $ret; |
103 | return $ret; |
155 | } |
104 | } |
156 | } |
105 | } |