@@ -507,10 +507,46 @@ static int process_read_concern(zval *option, bson_t *mongoc_opts TSRMLS_DC)
507
507
return true;
508
508
}
509
509
510
- static int process_read_preference (zval * option , zval * * zreadPreference TSRMLS_DC )
510
+ static int phongo_do_select_server (mongoc_client_t * client , bson_t * opts , zval * zreadPreference , int server_id TSRMLS_DC )
511
+ {
512
+ bson_error_t error ;
513
+ uint32_t selected_server_id ;
514
+
515
+ if (server_id > 0 ) {
516
+ bson_append_int32 (opts , "serverId" , -1 , server_id );
517
+ selected_server_id = server_id ;
518
+ } else {
519
+ mongoc_server_description_t * selected_server = NULL ;
520
+
521
+ selected_server = mongoc_client_select_server (client , false, (zreadPreference ? phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ) : mongoc_client_get_read_prefs (client )), & error );
522
+ if (selected_server ) {
523
+ selected_server_id = mongoc_server_description_id (selected_server );
524
+ bson_append_int32 (opts , "serverId" , -1 , selected_server_id );
525
+ mongoc_server_description_destroy (selected_server );
526
+ } else {
527
+ /* Check for connection related exceptions */
528
+ if (!EG (exception )) {
529
+ phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
530
+ }
531
+
532
+ return false;
533
+ }
534
+ }
535
+
536
+ return selected_server_id ;
537
+ }
538
+
539
+ static int process_read_preference (zval * option , bson_t * mongoc_opts , zval * * zreadPreference , mongoc_client_t * client , int server_id TSRMLS_DC )
511
540
{
512
541
if (Z_TYPE_P (option ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (option ), php_phongo_readpreference_ce TSRMLS_CC )) {
542
+ int selected_server_id ;
543
+
513
544
* zreadPreference = option ;
545
+
546
+ selected_server_id = phongo_do_select_server (client , mongoc_opts , * zreadPreference , server_id TSRMLS_CC );
547
+ if (!selected_server_id ) {
548
+ return false;
549
+ }
514
550
} else {
515
551
phongo_throw_exception (
516
552
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC ,
@@ -522,10 +558,12 @@ static int process_read_preference(zval *option, zval **zreadPreference TSRMLS_D
522
558
return true;
523
559
}
524
560
525
- static int process_write_concern (zval * option , zval * * zwriteConcern TSRMLS_DC )
561
+ static int process_write_concern (zval * option , bson_t * command_opts , zval * * zwriteConcern TSRMLS_DC )
526
562
{
527
563
if (Z_TYPE_P (option ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (option ), php_phongo_writeconcern_ce TSRMLS_CC )) {
528
564
* zwriteConcern = option ;
565
+
566
+ error : add write concern to command_opts too
529
567
} else {
530
568
phongo_throw_exception (
531
569
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC ,
@@ -537,7 +575,7 @@ static int process_write_concern(zval *option, zval **zwriteConcern TSRMLS_DC)
537
575
return true;
538
576
}
539
577
540
- static int phongo_execute_parse_options (zval * driver_options , int flags , bson_t * mongoc_opts , zval * * zreadPreference , zval * * zwriteConcern TSRMLS_DC )
578
+ static int phongo_execute_parse_options (mongoc_client_t * client , int server_id , zval * driver_options , int flags , bson_t * mongoc_opts , zval * * zreadPreference , zval * * zwriteConcern TSRMLS_DC )
541
579
{
542
580
if (driver_options && Z_TYPE_P (driver_options ) == IS_ARRAY ) {
543
581
HashTable * ht_data = HASH_OF (driver_options );
@@ -561,11 +599,11 @@ static int phongo_execute_parse_options(zval *driver_options, int flags, bson_t
561
599
return false;
562
600
}
563
601
} else if ((!strcasecmp (ZSTR_VAL (string_key ), "readPreference" )) && (flags & PHONGO_READPREFERENCE_ALLOWED )) {
564
- if (!process_read_preference (driver_option , zreadPreference )) {
602
+ if (!process_read_preference (driver_option , mongoc_opts , zreadPreference , client , server_id )) {
565
603
return false;
566
604
}
567
605
} else if ((!strcasecmp (ZSTR_VAL (string_key ), "writeConcern" )) && (flags & PHONGO_WRITECONCERN_ALLOWED )) {
568
- if (!process_write_concern (driver_option , zwriteConcern )) {
606
+ if (!process_write_concern (driver_option , mongoc_opts , zwriteConcern )) {
569
607
return false;
570
608
}
571
609
} else {
@@ -638,7 +676,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_ph
638
676
/* FIXME: Legacy way of specifying the writeConcern option into this function */
639
677
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_writeconcern_ce TSRMLS_CC )) {
640
678
zwriteConcern = options ;
641
- } else if (!phongo_execute_parse_options (options , PHONGO_WRITECONCERN_ALLOWED , NULL , NULL , & zwriteConcern TSRMLS_CC )) {
679
+ } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_WRITECONCERN_ALLOWED , NULL , NULL , & zwriteConcern TSRMLS_CC )) {
642
680
return false;
643
681
}
644
682
@@ -745,7 +783,7 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
745
783
/* FIXME: Legacy way of specifying the readPreference option into this function */
746
784
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
747
785
zreadPreference = options ;
748
- } else if (!phongo_execute_parse_options (options , PHONGO_READPREFERENCE_ALLOWED , NULL , & zreadPreference , NULL TSRMLS_CC )) {
786
+ } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_READPREFERENCE_ALLOWED , NULL , & zreadPreference , NULL TSRMLS_CC )) {
749
787
return false;
750
788
}
751
789
@@ -788,35 +826,6 @@ static bson_t *create_wrapped_command_envelope(const char *db, bson_t *reply)
788
826
return tmp ;
789
827
}
790
828
791
- static int phongo_do_select_server (mongoc_client_t * client , bson_t * opts , zval * zreadPreference , int server_id TSRMLS_DC )
792
- {
793
- bson_error_t error ;
794
- uint32_t selected_server_id ;
795
-
796
- if (server_id > 0 ) {
797
- bson_append_int32 (opts , "serverId" , -1 , server_id );
798
- selected_server_id = server_id ;
799
- } else {
800
- mongoc_server_description_t * selected_server = NULL ;
801
-
802
- selected_server = mongoc_client_select_server (client , false, (zreadPreference ? phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ) : mongoc_client_get_read_prefs (client )), & error );
803
- if (selected_server ) {
804
- selected_server_id = mongoc_server_description_id (selected_server );
805
- bson_append_int32 (opts , "serverId" , -1 , selected_server_id );
806
- mongoc_server_description_destroy (selected_server );
807
- } else {
808
- /* Check for connection related exceptions */
809
- if (!EG (exception )) {
810
- phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
811
- }
812
-
813
- return false;
814
- }
815
- }
816
-
817
- return selected_server_id ;
818
- }
819
-
820
829
int phongo_execute_command (mongoc_client_t * client , php_phongo_command_type_t type , const char * db , zval * zcommand , zval * options , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
821
830
{
822
831
const php_phongo_command_t * command ;
@@ -836,7 +845,7 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
836
845
/* FIXME: Legacy way of specifying the readPreference option into this function */
837
846
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
838
847
zreadPreference = options ;
839
- } else if (!phongo_execute_parse_options (options , PHONGO_READPREFERENCE_ALLOWED , opts , & zreadPreference , NULL TSRMLS_CC )) {
848
+ } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_READPREFERENCE_ALLOWED , opts , & zreadPreference , NULL TSRMLS_CC )) {
840
849
return false;
841
850
}
842
851
@@ -860,11 +869,12 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
860
869
result = mongoc_client_write_command_with_opts (client , db , command -> bson , opts , & reply , & error );
861
870
break ;
862
871
case PHONGO_COMMAND_READ_WRITE :
863
- result = mongoc_client_read_write_command_with_opts (client , db , command -> bson , phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ), opts , & reply , & error );
872
+ /* We can pass NULL as readPreference, as this argument was added historically, but has no function */
873
+ result = mongoc_client_read_write_command_with_opts (client , db , command -> bson , NULL , opts , & reply , & error );
864
874
break ;
865
875
default :
866
- /* Should never happen, but if it does: abort */
867
- assert (false );
876
+ /* Should never happen, but if it does: exception */
877
+ phongo_throw_exception ( PHONGO_ERROR_LOGIC TSRMLS_CC , "Type '%d' should never have been passed to phongo_execute_command, please file a bug report" , type );
868
878
}
869
879
if (!result ) {
870
880
phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
0 commit comments