@@ -63,153 +63,53 @@ public function setPreserveTypes($preserveTypes = true) {
63
63
* @return array[]
64
64
*/
65
65
public function fetchRows (Closure $ callback = null ) {
66
- return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ callback ) {
67
- $ statement ->setFetchMode (PDO ::FETCH_ASSOC );
68
- $ data = $ statement ->fetchAll ();
69
- if ($ this ->preserveTypes ) {
70
- $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
71
- foreach ($ data as &$ row ) {
72
- $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
73
- }
74
- }
75
- if ($ callback !== null ) {
76
- return call_user_func (function ($ resultData = []) use ($ data , $ callback ) {
77
- foreach ($ data as $ row ) {
78
- $ result = $ callback ($ row );
79
- if ($ result !== null && !($ result instanceof DBIgnoreRow)) {
80
- $ resultData [] = $ result ;
81
- } else {
82
- $ resultData [] = $ row ;
83
- }
84
- }
85
- return $ resultData ;
86
- });
87
- }
88
- return $ data ;
89
- });
66
+ return $ this ->fetchAll ($ callback , PDO ::FETCH_ASSOC );
90
67
}
91
68
92
69
/**
93
70
* @param Closure $callback
94
71
* @return array[]|\Generator
95
72
*/
96
73
public function fetchRowsLazy (Closure $ callback = null ) {
97
- if (version_compare (PHP_VERSION , '5.5 ' , '< ' )) {
98
- return new YieldPolyfillIterator ($ callback , $ this ->preserveTypes , function () {
99
- $ statement = $ this ->createStatement ();
100
- $ statement ->setFetchMode (PDO ::FETCH_ASSOC );
101
- return $ statement ;
102
- });
103
- }
104
- $ statement = $ this ->createStatement ();
105
- $ statement ->setFetchMode (PDO ::FETCH_ASSOC );
106
- $ generator = new LazyRowGenerator ($ this ->preserveTypes );
107
- return $ generator ->generate ($ statement , $ callback );
74
+ return $ this ->fetchLazy ($ callback , PDO ::FETCH_ASSOC );
108
75
}
109
76
110
77
/**
111
78
* @param Closure|null $callback
112
- * @return string []
79
+ * @return mixed []
113
80
* @throws \Exception
114
81
*/
115
82
public function fetchRow (Closure $ callback = null ) {
116
- return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ callback ) {
117
- $ statement ->setFetchMode (PDO ::FETCH_ASSOC );
118
- $ row = $ statement ->fetch ();
119
- if (!is_array ($ row )) {
120
- return [];
121
- }
122
- if ($ this ->preserveTypes ) {
123
- $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
124
- $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
125
- }
126
- if ($ callback !== null ) {
127
- $ result = $ callback ($ row );
128
- if ($ result !== null ) {
129
- $ row = $ result ;
130
- }
131
- }
132
- return $ row ;
133
- });
83
+ return $ this ->fetch ($ callback , PDO ::FETCH_ASSOC );
134
84
}
135
85
136
86
/**
137
87
* @param string $className
138
88
* @param Closure $callback
139
- * @return \array []
89
+ * @return object []
140
90
* @throws \Exception
141
91
*/
142
92
public function fetchObjects ($ className , Closure $ callback = null ) {
143
- return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ className , $ callback ) {
144
- $ statement ->setFetchMode (PDO ::FETCH_CLASS , $ className );
145
- $ data = $ statement ->fetchAll ();
146
- if ($ this ->preserveTypes ) {
147
- $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
148
- foreach ($ data as &$ row ) {
149
- $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
150
- }
151
- }
152
- if ($ callback !== null ) {
153
- return call_user_func (function ($ resultData = []) use ($ data , $ callback ) {
154
- foreach ($ data as $ row ) {
155
- $ result = $ callback ($ row );
156
- if ($ result !== null && !($ result instanceof DBIgnoreRow)) {
157
- $ resultData [] = $ result ;
158
- } else {
159
- $ resultData [] = $ row ;
160
- }
161
- }
162
- return $ resultData ;
163
- });
164
- }
165
- return $ data ;
166
- });
93
+ return $ this ->fetchAll ($ callback , PDO ::FETCH_CLASS , $ className );
167
94
}
168
95
169
96
/**
170
97
* @param string $className
171
98
* @param Closure $callback
172
- * @return array []|Generator
99
+ * @return object []|Generator
173
100
*/
174
101
public function fetchObjectsLazy ($ className , Closure $ callback = null ) {
175
- if (version_compare (PHP_VERSION , '5.5 ' , '< ' )) {
176
- return new YieldPolyfillIterator ($ callback , $ this ->preserveTypes , function () use ($ className ) {
177
- $ statement = $ this ->createStatement ();
178
- $ statement ->setFetchMode (PDO ::FETCH_CLASS , $ className );
179
- return $ statement ;
180
- });
181
- }
182
- $ statement = $ this ->createStatement ();
183
- $ statement ->setFetchMode (PDO ::FETCH_CLASS , $ className );
184
- $ generator = new LazyRowGenerator ($ this ->preserveTypes );
185
- return $ generator ->generate ($ statement , $ callback );
102
+ return $ this ->fetchLazy ($ callback , PDO ::FETCH_CLASS , $ className );
186
103
}
187
104
188
105
/**
189
106
* @param string $className
190
107
* @param Closure|null $callback
191
- * @return string []
108
+ * @return object []
192
109
* @throws \Exception
193
110
*/
194
111
public function fetchObject ($ className , Closure $ callback = null ) {
195
- return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ className , $ callback ) {
196
- $ statement ->setFetchMode (PDO ::FETCH_CLASS , $ className );
197
- $ row = $ statement ->fetch ();
198
- if (!is_array ($ row )) {
199
- return [];
200
- }
201
- if ($ this ->preserveTypes ) {
202
- $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
203
- $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
204
- }
205
- if ($ callback !== null ) {
206
- $ result = $ callback ($ row );
207
- if ($ result !== null ) {
208
- $ row = $ result ;
209
- }
210
- }
211
- return $ row ;
212
- });
112
+ return $ this ->fetch ($ callback , PDO ::FETCH_CLASS , $ className );
213
113
}
214
114
215
115
/**
@@ -320,4 +220,86 @@ private function createStatement() {
320
220
public function getIterator () {
321
221
return $ this ->fetchRowsLazy ();
322
222
}
223
+
224
+ /**
225
+ * @param callable $callback
226
+ * @param int $mode
227
+ * @param mixed $arg0
228
+ * @return mixed
229
+ * @throws \Exception
230
+ */
231
+ private function fetchAll ($ callback , $ mode , $ arg0 = null ) {
232
+ return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ callback , $ mode , $ arg0 ) {
233
+ $ statement ->setFetchMode ($ mode , $ arg0 );
234
+ $ data = $ statement ->fetchAll ();
235
+ if ($ this ->preserveTypes ) {
236
+ $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
237
+ foreach ($ data as &$ row ) {
238
+ $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
239
+ }
240
+ }
241
+ if ($ callback !== null ) {
242
+ return call_user_func (function ($ resultData = []) use ($ data , $ callback ) {
243
+ foreach ($ data as $ row ) {
244
+ $ result = $ callback ($ row );
245
+ if ($ result !== null && !($ result instanceof DBIgnoreRow)) {
246
+ $ resultData [] = $ result ;
247
+ } else {
248
+ $ resultData [] = $ row ;
249
+ }
250
+ }
251
+ return $ resultData ;
252
+ });
253
+ }
254
+ return $ data ;
255
+ });
256
+ }
257
+
258
+ /**
259
+ * @param callable $callback
260
+ * @param int $mode
261
+ * @param mixed $arg0
262
+ * @return Generator|YieldPolyfillIterator|mixed[]
263
+ */
264
+ private function fetchLazy ($ callback , $ mode , $ arg0 = null ) {
265
+ if (version_compare (PHP_VERSION , '5.5 ' , '< ' )) {
266
+ return new YieldPolyfillIterator ($ callback , $ this ->preserveTypes , function () use ($ mode , $ arg0 ) {
267
+ $ statement = $ this ->createStatement ();
268
+ $ statement ->setFetchMode ($ mode , $ arg0 );
269
+ return $ statement ;
270
+ });
271
+ }
272
+ $ statement = $ this ->createStatement ();
273
+ $ statement ->setFetchMode ($ mode , $ arg0 );
274
+ $ generator = new LazyRowGenerator ($ this ->preserveTypes );
275
+ return $ generator ->generate ($ statement , $ callback );
276
+ }
277
+
278
+ /**
279
+ * @param callable $callback
280
+ * @param int $mode
281
+ * @param mixed $arg0
282
+ * @return mixed
283
+ * @throws \Exception
284
+ */
285
+ private function fetch ($ callback , $ mode , $ arg0 = null ) {
286
+ return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ callback , $ mode , $ arg0 ) {
287
+ $ statement ->setFetchMode ($ mode , $ arg0 );
288
+ $ row = $ statement ->fetch ();
289
+ if (!is_array ($ row )) {
290
+ return [];
291
+ }
292
+ if ($ this ->preserveTypes ) {
293
+ $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
294
+ $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
295
+ }
296
+ if ($ callback !== null ) {
297
+ $ result = $ callback ($ row );
298
+ if ($ result !== null ) {
299
+ $ row = $ result ;
300
+ }
301
+ }
302
+ return $ row ;
303
+ });
304
+ }
323
305
}
0 commit comments