@@ -306,6 +306,17 @@ void phongo_server_init(zval *return_value, mongoc_client_t *client, int server_
306306}
307307/* }}} */
308308
309+ void phongo_session_init (zval * return_value , mongoc_client_session_t * client_session TSRMLS_DC ) /* {{{ */
310+ {
311+ php_phongo_session_t * session ;
312+
313+ object_init_ex (return_value , php_phongo_session_ce );
314+
315+ session = Z_SESSION_OBJ_P (return_value );
316+ session -> client_session = client_session ;
317+ }
318+ /* }}} */
319+
309320void phongo_readconcern_init (zval * return_value , const mongoc_read_concern_t * read_concern TSRMLS_DC ) /* {{{ */
310321{
311322 php_phongo_readconcern_t * intern ;
@@ -527,6 +538,34 @@ static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zr
527538 return true;
528539}
529540
541+ static bool process_session (zval * option , bson_t * mongoc_opts , zval * * zsession , mongoc_client_t * client TSRMLS_DC )
542+ {
543+ const mongoc_client_session_t * client_session ;
544+
545+ if (Z_TYPE_P (option ) != IS_OBJECT || !instanceof_function (Z_OBJCE_P (option ), php_phongo_session_ce TSRMLS_CC )) {
546+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected \"session\" option to be %s, %s given" , ZSTR_VAL (php_phongo_session_ce -> name ), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P (option ));
547+ return false;
548+ }
549+
550+ client_session = Z_SESSION_OBJ_P (option )-> client_session ;
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+
557+ if (!mongoc_client_session_append (Z_SESSION_OBJ_P (option )-> client_session , mongoc_opts , NULL )) {
558+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Error appending \"session\" option" );
559+ return false;
560+ }
561+
562+ if (zsession ) {
563+ * zsession = option ;
564+ }
565+
566+ return true;
567+ }
568+
530569static bool process_write_concern (zval * option , bson_t * mongoc_opts , zval * * zwriteConcern TSRMLS_DC )
531570{
532571 if (Z_TYPE_P (option ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (option ), php_phongo_writeconcern_ce TSRMLS_CC )) {
@@ -550,7 +589,7 @@ static bool process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwri
550589 return true;
551590}
552591
553- 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 )
554593{
555594 if (driver_options && Z_TYPE_P (driver_options ) == IS_ARRAY ) {
556595 HashTable * ht_data = HASH_OF (driver_options );
@@ -573,6 +612,10 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
573612 if (!process_read_preference (driver_option , mongoc_opts , zreadPreference , client , server_id )) {
574613 return false;
575614 }
615+ } else if ((!strcmp (ZSTR_VAL (string_key ), "session" ))) {
616+ if (!process_session (driver_option , mongoc_opts , zsession , client )) {
617+ return false;
618+ }
576619 } else if ((!strcasecmp (ZSTR_VAL (string_key ), "writeConcern" )) && (type & PHONGO_COMMAND_WRITE )) {
577620 if (!process_write_concern (driver_option , mongoc_opts , zwriteConcern )) {
578621 return false;
@@ -606,6 +649,10 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
606649 if (!process_read_preference (* driver_option , mongoc_opts , zreadPreference , client , server_id TSRMLS_CC )) {
607650 return false;
608651 }
652+ } else if ((!strcasecmp (ZSTR_VAL (string_key ), "session" ))) {
653+ if (!process_session (* driver_option , mongoc_opts , zsession , client TSRMLS_CC )) {
654+ return false;
655+ }
609656 } else if ((!strcasecmp (string_key , "writeConcern" )) && (type & PHONGO_COMMAND_WRITE )) {
610657 if (!process_write_concern (* driver_option , mongoc_opts , zwriteConcern TSRMLS_CC )) {
611658 return false;
@@ -628,6 +675,7 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
628675 mongoc_bulk_operation_t * bulk = bulk_write -> bulk ;
629676 php_phongo_writeresult_t * writeresult ;
630677 zval * zwriteConcern = NULL ;
678+ zval * zsession = NULL ;
631679 const mongoc_write_concern_t * write_concern ;
632680 bson_t opts = BSON_INITIALIZER ;
633681
@@ -644,7 +692,7 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
644692 /* FIXME: Legacy way of specifying the writeConcern option into this function */
645693 if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_writeconcern_ce TSRMLS_CC )) {
646694 zwriteConcern = options ;
647- } 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 )) {
648696 bson_destroy (& opts );
649697 return false;
650698 }
@@ -655,6 +703,10 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
655703 mongoc_bulk_operation_set_collection (bulk , bulk_write -> collection );
656704 mongoc_bulk_operation_set_client (bulk , client );
657705
706+ if (zsession ) {
707+ mongoc_bulk_operation_set_client_session (bulk , Z_SESSION_OBJ_P (zsession )-> client_session );
708+ }
709+
658710 /* If a write concern was not specified, libmongoc will use the client's
659711 * write concern; however, we should still fetch it for the write result. */
660712 write_concern = phongo_write_concern_from_zval (zwriteConcern TSRMLS_CC );
@@ -736,7 +788,6 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
736788 char * collname ;
737789 mongoc_collection_t * collection ;
738790 zval * zreadPreference = NULL ;
739- bson_t opts = BSON_INITIALIZER ;
740791
741792 if (!phongo_split_namespace (namespace , & dbname , & collname )) {
742793 phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "%s: %s" , "Invalid namespace provided" , namespace );
@@ -755,13 +806,10 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
755806 /* FIXME: Legacy way of specifying the readPreference option into this function */
756807 if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
757808 zreadPreference = options ;
758- } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_COMMAND_READ , & opts , & zreadPreference , NULL TSRMLS_CC )) {
759- bson_destroy (& opts );
809+ } else if (!phongo_execute_parse_options (client , server_id , options , PHONGO_COMMAND_READ , query -> opts , & zreadPreference , NULL , NULL TSRMLS_CC )) {
760810 return false;
761811 }
762812
763- bson_destroy (& opts );
764-
765813 cursor = mongoc_collection_find_with_opts (collection , query -> filter , query -> opts , phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ));
766814 mongoc_collection_destroy (collection );
767815
@@ -819,7 +867,7 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
819867 /* FIXME: Legacy way of specifying the readPreference option into this function */
820868 if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
821869 zreadPreference = options ;
822- } 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 )) {
823871 return false;
824872 }
825873
@@ -2693,6 +2741,7 @@ PHP_MINIT_FUNCTION(mongodb)
26932741 php_phongo_readconcern_init_ce (INIT_FUNC_ARGS_PASSTHRU );
26942742 php_phongo_readpreference_init_ce (INIT_FUNC_ARGS_PASSTHRU );
26952743 php_phongo_server_init_ce (INIT_FUNC_ARGS_PASSTHRU );
2744+ php_phongo_session_init_ce (INIT_FUNC_ARGS_PASSTHRU );
26962745 php_phongo_writeconcern_init_ce (INIT_FUNC_ARGS_PASSTHRU );
26972746 php_phongo_writeconcernerror_init_ce (INIT_FUNC_ARGS_PASSTHRU );
26982747 php_phongo_writeerror_init_ce (INIT_FUNC_ARGS_PASSTHRU );
0 commit comments