@@ -242,6 +242,40 @@ static void php_phongo_manager_prep_uri_options(zval *options TSRMLS_DC) /* {{{
242
242
return ;
243
243
} /* }}} */
244
244
245
+ /* Selects a server for an execute method. If "for_writes" is true, a primary
246
+ * will be selected. Otherwise, a read preference will be used to select the
247
+ * server. If zreadPreference is NULL, the client's read preference will be
248
+ * used.
249
+ *
250
+ * On success, server_id will be set and the function will return true;
251
+ * otherwise, false is returned and an exception is thrown. */
252
+ static bool php_phongo_manager_select_server (bool for_writes , zval * zreadPreference , mongoc_client_t * client , uint32_t * server_id TSRMLS_DC ) /* {{{ */
253
+ {
254
+ const mongoc_read_prefs_t * read_preference = NULL ;
255
+ mongoc_server_description_t * selected_server ;
256
+ bson_error_t error ;
257
+
258
+ if (!for_writes ) {
259
+ read_preference = zreadPreference ? phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ) : mongoc_client_get_read_prefs (client );
260
+ }
261
+
262
+ selected_server = mongoc_client_select_server (client , for_writes , read_preference , & error );
263
+
264
+ if (selected_server ) {
265
+ * server_id = mongoc_server_description_id (selected_server );
266
+ mongoc_server_description_destroy (selected_server );
267
+
268
+ return true;
269
+ }
270
+
271
+ /* Check for connection related exceptions */
272
+ if (!EG (exception )) {
273
+ phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
274
+ }
275
+
276
+ return false;
277
+ } /* }}} */
278
+
245
279
/* {{{ proto void MongoDB\Driver\Manager::__construct([string $uri = "mongodb://127.0.0.1/"[, array $options = array()[, array $driverOptions = array()]]])
246
280
Constructs a new Manager */
247
281
static PHP_METHOD (Manager , __construct )
@@ -292,6 +326,9 @@ static PHP_METHOD(Manager, executeCommand)
292
326
phongo_zpp_char_len db_len ;
293
327
zval * command ;
294
328
zval * options = NULL ;
329
+ bool free_options = false;
330
+ zval * zreadPreference = NULL ;
331
+ uint32_t server_id = 0 ;
295
332
DECLARE_RETURN_VALUE_USED
296
333
SUPPRESS_UNUSED_WARNING (return_value_ptr )
297
334
@@ -301,7 +338,24 @@ static PHP_METHOD(Manager, executeCommand)
301
338
302
339
intern = Z_MANAGER_OBJ_P (getThis ());
303
340
304
- phongo_execute_command (intern -> client , PHONGO_COMMAND_RAW , db , command , options , -1 , return_value , return_value_used TSRMLS_CC );
341
+ options = php_phongo_prep_legacy_option (options , "readPreference" , & free_options TSRMLS_CC );
342
+
343
+ if (!phongo_parse_read_preference (options , & zreadPreference TSRMLS_CC )) {
344
+ /* Exception should already have been thrown */
345
+ goto cleanup ;
346
+ }
347
+
348
+ if (!php_phongo_manager_select_server (false, zreadPreference , intern -> client , & server_id TSRMLS_CC )) {
349
+ /* Exception should already have been thrown */
350
+ goto cleanup ;
351
+ }
352
+
353
+ phongo_execute_command (intern -> client , PHONGO_COMMAND_RAW , db , command , options , server_id , return_value , return_value_used TSRMLS_CC );
354
+
355
+ cleanup :
356
+ if (free_options ) {
357
+ php_phongo_prep_legacy_option_free (options TSRMLS_CC );
358
+ }
305
359
} /* }}} */
306
360
307
361
/* {{{ proto MongoDB\Driver\Cursor MongoDB\Driver\Manager::executeReadCommand(string $db, MongoDB\Driver\Command $command[, array $options = null])
@@ -313,6 +367,8 @@ static PHP_METHOD(Manager, executeReadCommand)
313
367
phongo_zpp_char_len db_len ;
314
368
zval * command ;
315
369
zval * options = NULL ;
370
+ zval * zreadPreference = NULL ;
371
+ uint32_t server_id = 0 ;
316
372
DECLARE_RETURN_VALUE_USED
317
373
SUPPRESS_UNUSED_WARNING (return_value_ptr )
318
374
@@ -322,7 +378,17 @@ static PHP_METHOD(Manager, executeReadCommand)
322
378
323
379
intern = Z_MANAGER_OBJ_P (getThis ());
324
380
325
- phongo_execute_command (intern -> client , PHONGO_COMMAND_READ , db , command , options , -1 , return_value , return_value_used TSRMLS_CC );
381
+ if (!phongo_parse_read_preference (options , & zreadPreference TSRMLS_CC )) {
382
+ /* Exception should already have been thrown */
383
+ return ;
384
+ }
385
+
386
+ if (!php_phongo_manager_select_server (false, zreadPreference , intern -> client , & server_id TSRMLS_CC )) {
387
+ /* Exception should already have been thrown */
388
+ return ;
389
+ }
390
+
391
+ phongo_execute_command (intern -> client , PHONGO_COMMAND_READ , db , command , options , server_id , return_value , return_value_used TSRMLS_CC );
326
392
} /* }}} */
327
393
328
394
/* {{{ proto MongoDB\Driver\Cursor MongoDB\Driver\Manager::executeWriteCommand(string $db, MongoDB\Driver\Command $command[, array $options = null])
@@ -334,6 +400,7 @@ static PHP_METHOD(Manager, executeWriteCommand)
334
400
phongo_zpp_char_len db_len ;
335
401
zval * command ;
336
402
zval * options = NULL ;
403
+ uint32_t server_id = 0 ;
337
404
DECLARE_RETURN_VALUE_USED
338
405
SUPPRESS_UNUSED_WARNING (return_value_ptr )
339
406
@@ -343,7 +410,12 @@ static PHP_METHOD(Manager, executeWriteCommand)
343
410
344
411
intern = Z_MANAGER_OBJ_P (getThis ());
345
412
346
- phongo_execute_command (intern -> client , PHONGO_COMMAND_WRITE , db , command , options , -1 , return_value , return_value_used TSRMLS_CC );
413
+ if (!php_phongo_manager_select_server (true, NULL , intern -> client , & server_id TSRMLS_CC )) {
414
+ /* Exception should already have been thrown */
415
+ return ;
416
+ }
417
+
418
+ phongo_execute_command (intern -> client , PHONGO_COMMAND_WRITE , db , command , options , server_id , return_value , return_value_used TSRMLS_CC );
347
419
} /* }}} */
348
420
349
421
/* {{{ proto MongoDB\Driver\Cursor MongoDB\Driver\Manager::executeReadWriteCommand(string $db, MongoDB\Driver\Command $command[, array $options = null])
@@ -355,6 +427,7 @@ static PHP_METHOD(Manager, executeReadWriteCommand)
355
427
phongo_zpp_char_len db_len ;
356
428
zval * command ;
357
429
zval * options = NULL ;
430
+ uint32_t server_id = 0 ;
358
431
DECLARE_RETURN_VALUE_USED
359
432
SUPPRESS_UNUSED_WARNING (return_value_ptr )
360
433
@@ -364,7 +437,12 @@ static PHP_METHOD(Manager, executeReadWriteCommand)
364
437
365
438
intern = Z_MANAGER_OBJ_P (getThis ());
366
439
367
- phongo_execute_command (intern -> client , PHONGO_COMMAND_READ_WRITE , db , command , options , -1 , return_value , return_value_used TSRMLS_CC );
440
+ if (!php_phongo_manager_select_server (true, NULL , intern -> client , & server_id TSRMLS_CC )) {
441
+ /* Exception should already have been thrown */
442
+ return ;
443
+ }
444
+
445
+ phongo_execute_command (intern -> client , PHONGO_COMMAND_READ_WRITE , db , command , options , server_id , return_value , return_value_used TSRMLS_CC );
368
446
} /* }}} */
369
447
370
448
/* {{{ proto MongoDB\Driver\Cursor MongoDB\Driver\Manager::executeQuery(string $namespace, MongoDB\Driver\Query $query[, array $options = null])
@@ -376,6 +454,9 @@ static PHP_METHOD(Manager, executeQuery)
376
454
phongo_zpp_char_len namespace_len ;
377
455
zval * query ;
378
456
zval * options = NULL ;
457
+ bool free_options = false;
458
+ zval * zreadPreference = NULL ;
459
+ uint32_t server_id = 0 ;
379
460
DECLARE_RETURN_VALUE_USED
380
461
SUPPRESS_UNUSED_WARNING (return_value_ptr )
381
462
@@ -385,7 +466,24 @@ static PHP_METHOD(Manager, executeQuery)
385
466
386
467
intern = Z_MANAGER_OBJ_P (getThis ());
387
468
388
- phongo_execute_query (intern -> client , namespace , query , options , -1 , return_value , return_value_used TSRMLS_CC );
469
+ options = php_phongo_prep_legacy_option (options , "readPreference" , & free_options TSRMLS_CC );
470
+
471
+ if (!phongo_parse_read_preference (options , & zreadPreference TSRMLS_CC )) {
472
+ /* Exception should already have been thrown */
473
+ goto cleanup ;
474
+ }
475
+
476
+ if (!php_phongo_manager_select_server (false, zreadPreference , intern -> client , & server_id TSRMLS_CC )) {
477
+ /* Exception should already have been thrown */
478
+ goto cleanup ;
479
+ }
480
+
481
+ phongo_execute_query (intern -> client , namespace , query , options , server_id , return_value , return_value_used TSRMLS_CC );
482
+
483
+ cleanup :
484
+ if (free_options ) {
485
+ php_phongo_prep_legacy_option_free (options TSRMLS_CC );
486
+ }
389
487
} /* }}} */
390
488
391
489
/* {{{ proto MongoDB\Driver\WriteResult MongoDB\Driver\Manager::executeBulkWrite(string $namespace, MongoDB\Driver\BulkWrite $zbulk[, array $options = null])
@@ -396,8 +494,10 @@ static PHP_METHOD(Manager, executeBulkWrite)
396
494
char * namespace ;
397
495
phongo_zpp_char_len namespace_len ;
398
496
zval * zbulk ;
399
- zval * options = NULL ;
400
497
php_phongo_bulkwrite_t * bulk ;
498
+ zval * options = NULL ;
499
+ bool free_options = false;
500
+ uint32_t server_id = 0 ;
401
501
DECLARE_RETURN_VALUE_USED
402
502
SUPPRESS_UNUSED_WARNING (return_value_ptr )
403
503
@@ -408,7 +508,19 @@ static PHP_METHOD(Manager, executeBulkWrite)
408
508
intern = Z_MANAGER_OBJ_P (getThis ());
409
509
bulk = Z_BULKWRITE_OBJ_P (zbulk );
410
510
411
- phongo_execute_bulk_write (intern -> client , namespace , bulk , options , -1 , return_value , return_value_used TSRMLS_CC );
511
+ options = php_phongo_prep_legacy_option (options , "writeConcern" , & free_options TSRMLS_CC );
512
+
513
+ if (!php_phongo_manager_select_server (true, NULL , intern -> client , & server_id TSRMLS_CC )) {
514
+ /* Exception should already have been thrown */
515
+ goto cleanup ;
516
+ }
517
+
518
+ phongo_execute_bulk_write (intern -> client , namespace , bulk , options , server_id , return_value , return_value_used TSRMLS_CC );
519
+
520
+ cleanup :
521
+ if (free_options ) {
522
+ php_phongo_prep_legacy_option_free (options TSRMLS_CC );
523
+ }
412
524
} /* }}} */
413
525
414
526
/* {{{ proto MongoDB\Driver\ReadConcern MongoDB\Driver\Manager::getReadConcern()
@@ -511,9 +623,7 @@ static PHP_METHOD(Manager, selectServer)
511
623
{
512
624
php_phongo_manager_t * intern ;
513
625
zval * zreadPreference = NULL ;
514
- const mongoc_read_prefs_t * readPreference ;
515
- bson_error_t error ;
516
- mongoc_server_description_t * selected_server = NULL ;
626
+ uint32_t server_id = 0 ;
517
627
SUPPRESS_UNUSED_WARNING (return_value_ptr ) SUPPRESS_UNUSED_WARNING (return_value_used )
518
628
519
629
@@ -523,19 +633,12 @@ static PHP_METHOD(Manager, selectServer)
523
633
return ;
524
634
}
525
635
526
- readPreference = phongo_read_preference_from_zval (zreadPreference TSRMLS_CC );
527
- selected_server = mongoc_client_select_server (intern -> client , false, readPreference , & error );
528
- if (selected_server ) {
529
- phongo_server_init (return_value , intern -> client , mongoc_server_description_id (selected_server ) TSRMLS_CC );
530
- mongoc_server_description_destroy (selected_server );
531
- } else {
532
- /* Check for connection related exceptions */
533
- if (EG (exception )) {
534
- return ;
535
- }
536
-
537
- phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
636
+ if (!php_phongo_manager_select_server (false, zreadPreference , intern -> client , & server_id TSRMLS_CC )) {
637
+ /* Exception should already have been thrown */
638
+ return ;
538
639
}
640
+
641
+ phongo_server_init (return_value , intern -> client , server_id TSRMLS_CC );
539
642
} /* }}} */
540
643
541
644
/* {{{ proto MongoDB\Driver\Session MongoDB\Driver\Manager::startSession([array $options = null])
0 commit comments