Skip to content

Commit 09f83fa

Browse files
committed
PHPC-1439: Expose transaction options on session object
1 parent 17005fa commit 09f83fa

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

src/MongoDB/Session.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,87 @@ static PHP_METHOD(Session, getServer)
262262
phongo_server_init(return_value, mongoc_client_session_get_client(intern->client_session), server_id TSRMLS_CC);
263263
} /* }}} */
264264

265+
/* {{{ proto array|null MongoDB\Driver\Session::getTransactionOptions()
266+
Returns options for the currently running transaction */
267+
static PHP_METHOD(Session, getTransactionOptions)
268+
{
269+
php_phongo_session_t* intern;
270+
mongoc_transaction_opt_t* opts;
271+
int64_t max_commit_time_ms;
272+
const mongoc_read_concern_t* read_concern;
273+
const mongoc_read_prefs_t* read_preference;
274+
const mongoc_write_concern_t* write_concern;
275+
276+
intern = Z_SESSION_OBJ_P(getThis());
277+
SESSION_CHECK_LIVELINESS(intern, "getTransactionOptions")
278+
279+
if (zend_parse_parameters_none() == FAILURE) {
280+
return;
281+
}
282+
283+
opts = mongoc_session_opts_get_transaction_opts(intern->client_session);
284+
285+
if (!opts) {
286+
return;
287+
}
288+
289+
max_commit_time_ms = mongoc_transaction_opts_get_max_commit_time_ms(opts);
290+
read_concern = mongoc_transaction_opts_get_read_concern(opts);
291+
read_preference = mongoc_transaction_opts_get_read_prefs(opts);
292+
write_concern = mongoc_transaction_opts_get_write_concern(opts);
293+
294+
array_init_size(return_value, 4);
295+
296+
if (max_commit_time_ms) {
297+
ADD_ASSOC_LONG_EX(return_value, "maxCommitTimeMS", max_commit_time_ms);
298+
}
299+
300+
if (!mongoc_read_concern_is_default(read_concern)) {
301+
#if PHP_VERSION_ID >= 70000
302+
zval zread_concern;
303+
304+
phongo_readconcern_init(&zread_concern, read_concern TSRMLS_CC);
305+
ADD_ASSOC_ZVAL_EX(return_value, "readConcern", &zread_concern);
306+
#else
307+
zval* zread_concern = NULL;
308+
MAKE_STD_ZVAL(zread_concern);
309+
310+
phongo_readconcern_init(zread_concern, read_concern TSRMLS_CC);
311+
ADD_ASSOC_ZVAL_EX(return_value, "readConcern", zread_concern);
312+
#endif
313+
}
314+
315+
if (read_preference) {
316+
#if PHP_VERSION_ID >= 70000
317+
zval zread_preference;
318+
319+
phongo_readpreference_init(&zread_preference, read_preference TSRMLS_CC);
320+
ADD_ASSOC_ZVAL_EX(return_value, "readPreference", &zread_preference);
321+
#else
322+
zval* zread_preference = NULL;
323+
MAKE_STD_ZVAL(zread_preference);
324+
325+
phongo_readpreference_init(zread_preference, read_preference TSRMLS_CC);
326+
ADD_ASSOC_ZVAL_EX(return_value, "readPreference", zread_preference);
327+
#endif
328+
}
329+
330+
if (!mongoc_write_concern_is_default(write_concern)) {
331+
#if PHP_VERSION_ID >= 70000
332+
zval zwrite_concern;
333+
334+
phongo_writeconcern_init(&zwrite_concern, write_concern TSRMLS_CC);
335+
ADD_ASSOC_ZVAL_EX(return_value, "writeConcern", &zwrite_concern);
336+
#else
337+
zval* zwrite_concern = NULL;
338+
MAKE_STD_ZVAL(zwrite_concern);
339+
340+
phongo_writeconcern_init(zwrite_concern, write_concern TSRMLS_CC);
341+
ADD_ASSOC_ZVAL_EX(return_value, "writeConcern", zwrite_concern);
342+
#endif
343+
}
344+
} /* }}} */
345+
265346
/* Creates a opts structure from an array optionally containing an RP, RC,
266347
* WC object, and/or maxCommitTimeMS int. Returns NULL if no options were found,
267348
* or there was an invalid option. If there was an invalid option or structure,
@@ -490,6 +571,7 @@ static zend_function_entry php_phongo_session_me[] = {
490571
PHP_ME(Session, getLogicalSessionId, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
491572
PHP_ME(Session, getOperationTime, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
492573
PHP_ME(Session, getServer, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
574+
PHP_ME(Session, getTransactionOptions, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
493575
PHP_ME(Session, isInTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
494576
PHP_ME(Session, startTransaction, ai_Session_startTransaction, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
495577
ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_Session_void, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
--TEST--
2+
MongoDB\Driver\Session::getTransactionOptions()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
6+
<?php skip_if_server_version('<', '4.0'); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = new MongoDB\Driver\Manager(URI);
12+
$session = $manager->startSession();
13+
14+
var_dump($session->getTransactionOptions());
15+
16+
$options = [
17+
['maxCommitTimeMS' => 0],
18+
['maxCommitTimeMS' => 1],
19+
['readConcern' => new \MongoDB\Driver\ReadConcern('majority')],
20+
['readPreference' => new \MongoDB\Driver\ReadPreference('primaryPreferred')],
21+
['writeConcern' => new \MongoDB\Driver\WriteConcern('majority')],
22+
];
23+
24+
foreach ($options as $test) {
25+
// Session no longer needs to be restarted once CDRIVER-3366 is fixed
26+
$session = $manager->startSession();
27+
28+
$session->startTransaction($test);
29+
30+
var_dump($session->getTransactionOptions());
31+
}
32+
33+
?>
34+
===DONE===
35+
<?php exit(0); ?>
36+
--EXPECTF--
37+
NULL
38+
array(1) {
39+
["readPreference"]=>
40+
object(MongoDB\Driver\ReadPreference)#%d (1) {
41+
["mode"]=>
42+
string(7) "primary"
43+
}
44+
}
45+
array(2) {
46+
["maxCommitTimeMS"]=>
47+
int(1)
48+
["readPreference"]=>
49+
object(MongoDB\Driver\ReadPreference)#%d (1) {
50+
["mode"]=>
51+
string(7) "primary"
52+
}
53+
}
54+
array(2) {
55+
["readConcern"]=>
56+
object(MongoDB\Driver\ReadConcern)#%d (1) {
57+
["level"]=>
58+
string(8) "majority"
59+
}
60+
["readPreference"]=>
61+
object(MongoDB\Driver\ReadPreference)#%d (1) {
62+
["mode"]=>
63+
string(7) "primary"
64+
}
65+
}
66+
array(1) {
67+
["readPreference"]=>
68+
object(MongoDB\Driver\ReadPreference)#%d (1) {
69+
["mode"]=>
70+
string(16) "primaryPreferred"
71+
}
72+
}
73+
array(2) {
74+
["readPreference"]=>
75+
object(MongoDB\Driver\ReadPreference)#%d (1) {
76+
["mode"]=>
77+
string(7) "primary"
78+
}
79+
["writeConcern"]=>
80+
object(MongoDB\Driver\WriteConcern)#%d (1) {
81+
["w"]=>
82+
string(8) "majority"
83+
}
84+
}
85+
===DONE===

0 commit comments

Comments
 (0)