@@ -306,6 +306,17 @@ void phongo_server_init(zval *return_value, mongoc_client_t *client, int server_
306
306
}
307
307
/* }}} */
308
308
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
+
309
320
void phongo_readconcern_init (zval * return_value , const mongoc_read_concern_t * read_concern TSRMLS_DC ) /* {{{ */
310
321
{
311
322
php_phongo_readconcern_t * intern ;
@@ -527,6 +538,34 @@ static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zr
527
538
return true;
528
539
}
529
540
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
+
530
569
static bool process_write_concern (zval * option , bson_t * mongoc_opts , zval * * zwriteConcern TSRMLS_DC )
531
570
{
532
571
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
550
589
return true;
551
590
}
552
591
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 )
554
593
{
555
594
if (driver_options && Z_TYPE_P (driver_options ) == IS_ARRAY ) {
556
595
HashTable * ht_data = HASH_OF (driver_options );
@@ -573,6 +612,10 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
573
612
if (!process_read_preference (driver_option , mongoc_opts , zreadPreference , client , server_id )) {
574
613
return false;
575
614
}
615
+ } else if ((!strcmp (ZSTR_VAL (string_key ), "session" ))) {
616
+ if (!process_session (driver_option , mongoc_opts , zsession , client )) {
617
+ return false;
618
+ }
576
619
} else if ((!strcasecmp (ZSTR_VAL (string_key ), "writeConcern" )) && (type & PHONGO_COMMAND_WRITE )) {
577
620
if (!process_write_concern (driver_option , mongoc_opts , zwriteConcern )) {
578
621
return false;
@@ -606,6 +649,10 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
606
649
if (!process_read_preference (* driver_option , mongoc_opts , zreadPreference , client , server_id TSRMLS_CC )) {
607
650
return false;
608
651
}
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
+ }
609
656
} else if ((!strcasecmp (string_key , "writeConcern" )) && (type & PHONGO_COMMAND_WRITE )) {
610
657
if (!process_write_concern (* driver_option , mongoc_opts , zwriteConcern TSRMLS_CC )) {
611
658
return false;
@@ -628,6 +675,7 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
628
675
mongoc_bulk_operation_t * bulk = bulk_write -> bulk ;
629
676
php_phongo_writeresult_t * writeresult ;
630
677
zval * zwriteConcern = NULL ;
678
+ zval * zsession = NULL ;
631
679
const mongoc_write_concern_t * write_concern ;
632
680
bson_t opts = BSON_INITIALIZER ;
633
681
@@ -644,7 +692,7 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
644
692
/* FIXME: Legacy way of specifying the writeConcern option into this function */
645
693
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_writeconcern_ce TSRMLS_CC )) {
646
694
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 )) {
648
696
bson_destroy (& opts );
649
697
return false;
650
698
}
@@ -655,6 +703,10 @@ bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, p
655
703
mongoc_bulk_operation_set_collection (bulk , bulk_write -> collection );
656
704
mongoc_bulk_operation_set_client (bulk , client );
657
705
706
+ if (zsession ) {
707
+ mongoc_bulk_operation_set_client_session (bulk , Z_SESSION_OBJ_P (zsession )-> client_session );
708
+ }
709
+
658
710
/* If a write concern was not specified, libmongoc will use the client's
659
711
* write concern; however, we should still fetch it for the write result. */
660
712
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
736
788
char * collname ;
737
789
mongoc_collection_t * collection ;
738
790
zval * zreadPreference = NULL ;
739
- bson_t opts = BSON_INITIALIZER ;
740
791
741
792
if (!phongo_split_namespace (namespace , & dbname , & collname )) {
742
793
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
755
806
/* FIXME: Legacy way of specifying the readPreference option into this function */
756
807
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
757
808
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 )) {
760
810
return false;
761
811
}
762
812
763
- bson_destroy (& opts );
764
-
765
813
cursor = mongoc_collection_find_with_opts (collection , query -> filter , query -> opts , phongo_read_preference_from_zval (zreadPreference TSRMLS_CC ));
766
814
mongoc_collection_destroy (collection );
767
815
@@ -819,7 +867,7 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
819
867
/* FIXME: Legacy way of specifying the readPreference option into this function */
820
868
if (options && Z_TYPE_P (options ) == IS_OBJECT && instanceof_function (Z_OBJCE_P (options ), php_phongo_readpreference_ce TSRMLS_CC )) {
821
869
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 )) {
823
871
return false;
824
872
}
825
873
@@ -2693,6 +2741,7 @@ PHP_MINIT_FUNCTION(mongodb)
2693
2741
php_phongo_readconcern_init_ce (INIT_FUNC_ARGS_PASSTHRU );
2694
2742
php_phongo_readpreference_init_ce (INIT_FUNC_ARGS_PASSTHRU );
2695
2743
php_phongo_server_init_ce (INIT_FUNC_ARGS_PASSTHRU );
2744
+ php_phongo_session_init_ce (INIT_FUNC_ARGS_PASSTHRU );
2696
2745
php_phongo_writeconcern_init_ce (INIT_FUNC_ARGS_PASSTHRU );
2697
2746
php_phongo_writeconcernerror_init_ce (INIT_FUNC_ARGS_PASSTHRU );
2698
2747
php_phongo_writeerror_init_ce (INIT_FUNC_ARGS_PASSTHRU );
0 commit comments