@@ -538,7 +538,7 @@ static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zr
538538 return true;
539539}
540540
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 )
542542{
543543 const mongoc_client_session_t * client_session ;
544544
@@ -549,11 +549,20 @@ static bool process_session(zval *option, bson_t *mongoc_opts TSRMLS_DC)
549549
550550 client_session = Z_SESSION_OBJ_P (option )-> client_session ;
551551
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+
552557 if (!mongoc_client_session_append (Z_SESSION_OBJ_P (option )-> client_session , mongoc_opts , NULL )) {
553558 phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Error appending \"session\" option" );
554559 return false;
555560 }
556561
562+ if (zsession ) {
563+ * zsession = option ;
564+ }
565+
557566 return true;
558567}
559568
@@ -580,7 +589,7 @@ static bool process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwri
580589 return true;
581590}
582591
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 )
584593{
585594 if (driver_options && Z_TYPE_P (driver_options ) == IS_ARRAY ) {
586595 HashTable * ht_data = HASH_OF (driver_options );
@@ -604,7 +613,7 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
604613 return false;
605614 }
606615 } 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 )) {
608617 return false;
609618 }
610619 } 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,
640649 if (!process_read_preference (* driver_option , mongoc_opts , zreadPreference , client , server_id TSRMLS_CC )) {
641650 return false;
642651 }
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 )) {
645654 return false;
646655 }
647656 } 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
666675 mongoc_bulk_operation_t * bulk = bulk_write -> bulk ;
667676 php_phongo_writeresult_t * writeresult ;
668677 zval * zwriteConcern = NULL ;
678+ zval * zsession = NULL ;
669679 const mongoc_write_concern_t * write_concern ;
670680 bson_t opts = BSON_INITIALIZER ;
671681
@@ -682,7 +692,7 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
682692 /* FIXME: Legacy way of specifying the writeConcern option into this function */
683693 if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_writeconcern_ce TSRMLS_CC )) {
684694 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 )) {
686696 bson_destroy (& opts );
687697 return false;
688698 }
@@ -693,6 +703,10 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
693703 mongoc_bulk_operation_set_collection (bulk , bulk_write -> collection );
694704 mongoc_bulk_operation_set_client (bulk , client );
695705
706+ if (zsession ) {
707+ mongoc_bulk_operation_set_client_session (bulk , Z_SESSION_OBJ_P (zsession )-> client_session );
708+ }
709+
696710 /* If a write concern was not specified, libmongoc will use the client's
697711 * write concern; however, we should still fetch it for the write result. */
698712 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
774788 char * collname ;
775789 mongoc_collection_t * collection ;
776790 zval * zreadPreference = NULL ;
777- bson_t opts = BSON_INITIALIZER ;
778791
779792 if (!phongo_split_namespace (namespace , & dbname , & collname )) {
780793 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
793806 /* FIXME: Legacy way of specifying the readPreference option into this function */
794807 if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
795808 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 )) {
798810 return false;
799811 }
800812
801- bson_destroy (& opts );
802-
803813 cursor = mongoc_collection_find_with_opts (collection , query -> filter , query -> opts , phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ));
804814 mongoc_collection_destroy (collection );
805815
@@ -857,7 +867,7 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
857867 /* FIXME: Legacy way of specifying the readPreference option into this function */
858868 if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
859869 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 )) {
861871 return false;
862872 }
863873
0 commit comments