@@ -538,7 +538,7 @@ static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zr
538
538
return true;
539
539
}
540
540
541
- static bool process_session (zval * option , bson_t * mongoc_opts TSRMLS_DC )
541
+ static bool process_session (zval * option , bson_t * mongoc_opts , zval * * zsession , mongoc_client_t * client TSRMLS_DC )
542
542
{
543
543
const mongoc_client_session_t * client_session ;
544
544
@@ -549,11 +549,20 @@ static bool process_session(zval *option, bson_t *mongoc_opts TSRMLS_DC)
549
549
550
550
client_session = Z_SESSION_OBJ_P (option )-> client_session ;
551
551
552
+ if (client != mongoc_client_session_get_client (client_session )) {
553
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Cannot use Session started from a different Manager" );
554
+ return false;
555
+ }
556
+
552
557
if (!mongoc_client_session_append (Z_SESSION_OBJ_P (option )-> client_session , mongoc_opts , NULL )) {
553
558
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Error appending \"session\" option" );
554
559
return false;
555
560
}
556
561
562
+ if (zsession ) {
563
+ * zsession = option ;
564
+ }
565
+
557
566
return true;
558
567
}
559
568
@@ -580,7 +589,7 @@ static bool process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwri
580
589
return true;
581
590
}
582
591
583
- static int phongo_execute_parse_options (mongoc_client_t * client , int server_id , zval * driver_options , int type , bson_t * mongoc_opts , zval * * zreadPreference , zval * * zwriteConcern TSRMLS_DC )
592
+ static int phongo_execute_parse_options (mongoc_client_t * client , int server_id , zval * driver_options , int type , bson_t * mongoc_opts , zval * * zreadPreference , zval * * zwriteConcern , zval * * zsession TSRMLS_DC )
584
593
{
585
594
if (driver_options && Z_TYPE_P (driver_options ) == IS_ARRAY ) {
586
595
HashTable * ht_data = HASH_OF (driver_options );
@@ -604,7 +613,7 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
604
613
return false;
605
614
}
606
615
} else if ((!strcmp (ZSTR_VAL (string_key ), "session" ))) {
607
- if (!process_session (driver_option , mongoc_opts )) {
616
+ if (!process_session (driver_option , mongoc_opts , zsession , client )) {
608
617
return false;
609
618
}
610
619
} else if ((!strcasecmp (ZSTR_VAL (string_key ), "writeConcern" )) && (type & PHONGO_COMMAND_WRITE )) {
@@ -640,8 +649,8 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
640
649
if (!process_read_preference (* driver_option , mongoc_opts , zreadPreference , client , server_id TSRMLS_CC )) {
641
650
return false;
642
651
}
643
- } else if ((!strcmp (ZSTR_VAL (string_key ), "session" ))) {
644
- if (!process_session (* driver_option , mongoc_opts )) {
652
+ } else if ((!strcasecmp (ZSTR_VAL (string_key ), "session" ))) {
653
+ if (!process_session (* driver_option , mongoc_opts , zsession , client TSRMLS_CC )) {
645
654
return false;
646
655
}
647
656
} else if ((!strcasecmp (string_key , "writeConcern" )) && (type & PHONGO_COMMAND_WRITE )) {
@@ -666,6 +675,7 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
666
675
mongoc_bulk_operation_t * bulk = bulk_write -> bulk ;
667
676
php_phongo_writeresult_t * writeresult ;
668
677
zval * zwriteConcern = NULL ;
678
+ zval * zsession = NULL ;
669
679
const mongoc_write_concern_t * write_concern ;
670
680
bson_t opts = BSON_INITIALIZER ;
671
681
@@ -682,7 +692,7 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
682
692
/* FIXME: Legacy way of specifying the writeConcern option into this function */
683
693
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_writeconcern_ce TSRMLS_CC )) {
684
694
zwriteConcern = options ;
685
- } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_COMMAND_WRITE , & opts , NULL , & zwriteConcern TSRMLS_CC )) {
695
+ } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_COMMAND_WRITE , & opts , NULL , & zwriteConcern , & zsession TSRMLS_CC )) {
686
696
bson_destroy (& opts );
687
697
return false;
688
698
}
@@ -693,6 +703,10 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
693
703
mongoc_bulk_operation_set_collection (bulk , bulk_write -> collection );
694
704
mongoc_bulk_operation_set_client (bulk , client );
695
705
706
+ if (zsession ) {
707
+ mongoc_bulk_operation_set_client_session (bulk , Z_SESSION_OBJ_P (zsession )-> client_session );
708
+ }
709
+
696
710
/* If a write concern was not specified, libmongoc will use the client's
697
711
* write concern; however, we should still fetch it for the write result. */
698
712
write_concern = phongo_write_concern_from_zval (zwriteConcern TSRMLS_CC );
@@ -774,7 +788,6 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
774
788
char * collname ;
775
789
mongoc_collection_t * collection ;
776
790
zval * zreadPreference = NULL ;
777
- bson_t opts = BSON_INITIALIZER ;
778
791
779
792
if (!phongo_split_namespace (namespace , & dbname , & collname )) {
780
793
phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "%s: %s" , "Invalid namespace provided" , namespace );
@@ -793,13 +806,10 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
793
806
/* FIXME: Legacy way of specifying the readPreference option into this function */
794
807
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
795
808
zreadPreference = options ;
796
- } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_COMMAND_READ , & opts , & zreadPreference , NULL TSRMLS_CC )) {
797
- bson_destroy (& opts );
809
+ } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_COMMAND_READ , query -> opts , & zreadPreference , NULL , NULL TSRMLS_CC )) {
798
810
return false;
799
811
}
800
812
801
- bson_destroy (& opts );
802
-
803
813
cursor = mongoc_collection_find_with_opts (collection , query -> filter , query -> opts , phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ));
804
814
mongoc_collection_destroy (collection );
805
815
@@ -857,7 +867,7 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
857
867
/* FIXME: Legacy way of specifying the readPreference option into this function */
858
868
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
859
869
zreadPreference = options ;
860
- } else if (!phongo_execute_parse_options (client , server_id , options , type , & opts , & zreadPreference , NULL TSRMLS_CC )) {
870
+ } else if (!phongo_execute_parse_options (client , server_id , options , type , & opts , & zreadPreference , NULL , NULL TSRMLS_CC )) {
861
871
return false;
862
872
}
863
873
0 commit comments