@@ -179,6 +179,78 @@ function getWriteOptions() { /* {{{ */
179
179
"limit " => 1 ,
180
180
);
181
181
} /* }}} */
182
+ function getBulkOptions () { /* {{{ */
183
+ return array (
184
+ "ordered " => false ,
185
+ );
186
+ } /* }}} */
187
+
188
+ function bulkWrite (array $ bulk , array $ options = array ()) {
189
+ $ options = array_merge ($ this ->getBulkOptions (), $ options );
190
+
191
+ $ batch = new WriteBatch ($ options ["ordered " ]);
192
+
193
+ foreach ($ bulk as $ n => $ op ) {
194
+ foreach ($ op as $ opname => $ args ) {
195
+ if (!isset ($ args [0 ])) {
196
+ throw \RuntimeException (sprintf ("Missing argument#1 for '%s' (operation#%d) " , $ opname , $ n ));
197
+ }
198
+
199
+ switch ($ opname ) {
200
+ case "insertOne " :
201
+ $ batch ->insert ($ args [0 ]);
202
+ break ;
203
+
204
+ case "updateMany " :
205
+ if (!isset ($ args [1 ])) {
206
+ throw \RuntimeException (sprintf ("Missing argument#2 for '%s' (operation#%d) " , $ opname , $ n ));
207
+ }
208
+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("limit " => 0 ));
209
+
210
+ $ batch ->update ($ args [0 ], $ args [1 ], $ options );
211
+ break ;
212
+
213
+ case "updateOne " :
214
+ if (!isset ($ args [1 ])) {
215
+ throw \RuntimeException (sprintf ("Missing argument#2 for '%s' (operation#%d) " , $ opname , $ n ));
216
+ }
217
+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("limit " => 1 ));
218
+ if (key ($ args [1 ])[0 ] != '$ ' ) {
219
+ throw new \RuntimeException ("First key in \$update must be a \$operator " );
220
+ }
221
+
222
+ $ batch ->update ($ args [0 ], $ args [1 ], $ options );
223
+ break ;
224
+
225
+ case "replaceOne " :
226
+ if (!isset ($ args [1 ])) {
227
+ throw \RuntimeException (sprintf ("Missing argument#2 for '%s' (operation#%d) " , $ opname , $ n ));
228
+ }
229
+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("limit " => 1 ));
230
+ if (key ($ args [1 ])[0 ] == '$ ' ) {
231
+ throw new \RuntimeException ("First key in \$update must NOT be a \$operator " );
232
+ }
233
+
234
+ $ batch ->update ($ args [0 ], $ args [1 ], $ options );
235
+ break ;
236
+
237
+ case "deleteOne " :
238
+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [1 ]) ? $ args [1 ] : array (), array ("limit " => 1 ));
239
+ $ batch ->delete ($ args [0 ], $ options );
240
+ break ;
241
+
242
+ case "deleteMany " :
243
+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [1 ]) ? $ args [1 ] : array (), array ("limit " => 1 ));
244
+ $ batch ->delete ($ args [0 ], $ options );
245
+ break ;
246
+
247
+ default :
248
+ throw \RuntimeException (sprintf ("Unknown operation type called '%s' (operation#%d) " , $ opname , $ n ));
249
+ }
250
+ }
251
+ }
252
+ return $ this ->manager ->executeWriteBatch ($ this ->ns , $ batch , $ this ->wc );
253
+ }
182
254
183
255
function insertOne (array $ filter ) { /* {{{ */
184
256
$ options = array_merge ($ this ->getWriteOptions ());
@@ -215,17 +287,17 @@ protected function _update($filter, $update, $options) { /* {{{ */
215
287
$ batch ->update ($ filter , $ update , $ options );
216
288
return $ this ->manager ->executeWriteBatch ($ this ->ns , $ batch , $ this ->wc );
217
289
} /* }}} */
218
- function updateOne (array $ filter , array $ update , array $ options = array ()) { /* {{{ */
219
- if (key ($ update )[0 ] ! = '$ ' ) {
220
- throw new \RuntimeException ("First key in \$update must be a \$operator " );
290
+ function replaceOne (array $ filter , array $ update , array $ options = array ()) { /* {{{ */
291
+ if (key ($ update )[0 ] = = '$ ' ) {
292
+ throw new \RuntimeException ("First key in \$update must NOT be a \$operator " );
221
293
}
222
294
$ wr = $ this ->_update ($ filter , $ update , $ options );
223
295
224
296
return new UpdateResult ($ wr );
225
297
} /* }}} */
226
- function replaceOne (array $ filter , array $ update , array $ options = array ()) { /* {{{ */
227
- if (key ($ update )[0 ] = = '$ ' ) {
228
- throw new \RuntimeException ("First key in \$update must NOT be a \$operator " );
298
+ function updateOne (array $ filter , array $ update , array $ options = array ()) { /* {{{ */
299
+ if (key ($ update )[0 ] ! = '$ ' ) {
300
+ throw new \RuntimeException ("First key in \$update must be a \$operator " );
229
301
}
230
302
$ wr = $ this ->_update ($ filter , $ update , $ options );
231
303
0 commit comments