Skip to content

Commit 624c5a4

Browse files
committed
Merged pull request #882
2 parents 36f2577 + a917c35 commit 624c5a4

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

src/MongoDB/Session.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,24 @@ static PHP_METHOD(Session, endSession)
390390
mongoc_client_session_destroy(intern->client_session);
391391
} /* }}} */
392392

393+
/* {{{ proto void MongoDB\Driver\Session::isInTransaction(void)
394+
Returns whether a multi-document transaction is in progress */
395+
static PHP_METHOD(Session, isInTransaction)
396+
{
397+
php_phongo_session_t* intern;
398+
SUPPRESS_UNUSED_WARNING(return_value_ptr)
399+
SUPPRESS_UNUSED_WARNING(return_value_used)
400+
401+
intern = Z_SESSION_OBJ_P(getThis());
402+
403+
if (zend_parse_parameters_none() == FAILURE) {
404+
return;
405+
}
406+
407+
RETURN_BOOL(mongoc_client_session_in_transaction(intern->client_session));
408+
} /* }}} */
409+
410+
393411
/* {{{ MongoDB\Driver\Session function entries */
394412
ZEND_BEGIN_ARG_INFO_EX(ai_Session_advanceClusterTime, 0, 0, 1)
395413
ZEND_ARG_INFO(0, clusterTime)
@@ -408,15 +426,16 @@ ZEND_END_ARG_INFO()
408426

409427
static zend_function_entry php_phongo_session_me[] = {
410428
/* clang-format off */
429+
PHP_ME(Session, abortTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
411430
PHP_ME(Session, advanceClusterTime, ai_Session_advanceClusterTime, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
412431
PHP_ME(Session, advanceOperationTime, ai_Session_advanceOperationTime, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
432+
PHP_ME(Session, commitTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
433+
PHP_ME(Session, endSession, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
413434
PHP_ME(Session, getClusterTime, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
414435
PHP_ME(Session, getLogicalSessionId, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
415436
PHP_ME(Session, getOperationTime, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
437+
PHP_ME(Session, isInTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
416438
PHP_ME(Session, startTransaction, ai_Session_startTransaction, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
417-
PHP_ME(Session, commitTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
418-
PHP_ME(Session, abortTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
419-
PHP_ME(Session, endSession, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
420439
ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_Session_void, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
421440
ZEND_NAMED_ME(__wakeup, PHP_FN(MongoDB_disabled___wakeup), ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
422441
PHP_FE_END

src/libmongoc

Submodule libmongoc updated 231 files
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--TEST--
2+
MongoDB\Driver\Session::isInTransaction()
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+
<?php skip_if_not_replica_set(); ?>
8+
<?php skip_if_not_clean(DATABASE_NAME, COLLECTION_NAME); ?>
9+
--FILE--
10+
<?php
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
$manager = new MongoDB\Driver\Manager(URI);
14+
15+
/* Create collections as that can't be (automatically) done in a transaction */
16+
$cmd = new \MongoDB\Driver\Command([
17+
'create' => COLLECTION_NAME,
18+
]);
19+
$manager->executeCommand(DATABASE_NAME, $cmd);
20+
21+
/* Start a session */
22+
$session = $manager->startSession();
23+
24+
/* Empty transaction, and aborted empty transaction */
25+
var_dump($session->isInTransaction());
26+
$session->startTransaction();
27+
var_dump($session->isInTransaction());
28+
$session->abortTransaction();
29+
var_dump($session->isInTransaction());
30+
31+
/* Empty transaction, and committed empty transaction */
32+
var_dump($session->isInTransaction());
33+
$session->startTransaction();
34+
var_dump($session->isInTransaction());
35+
$session->commitTransaction();
36+
var_dump($session->isInTransaction());
37+
38+
/* Aborted transaction with one operation */
39+
var_dump($session->isInTransaction());
40+
$session->startTransaction();
41+
$bw = new \MongoDB\Driver\BulkWrite();
42+
$bw->insert( [ '_id' => 0, 'msg' => 'Initial Value' ] );
43+
$manager->executeBulkWrite(NS, $bw, ['session' => $session]);
44+
var_dump($session->isInTransaction());
45+
$session->abortTransaction();
46+
var_dump($session->isInTransaction());
47+
48+
/* Committed transaction with one operation */
49+
var_dump($session->isInTransaction());
50+
$session->startTransaction();
51+
$bw = new \MongoDB\Driver\BulkWrite();
52+
$bw->insert( [ '_id' => 0, 'msg' => 'Initial Value' ] );
53+
$manager->executeBulkWrite(NS, $bw, ['session' => $session]);
54+
var_dump($session->isInTransaction());
55+
$session->commitTransaction();
56+
var_dump($session->isInTransaction());
57+
58+
?>
59+
===DONE===
60+
<?php exit(0); ?>
61+
--EXPECTF--
62+
bool(false)
63+
bool(true)
64+
bool(false)
65+
bool(false)
66+
bool(true)
67+
bool(false)
68+
bool(false)
69+
bool(true)
70+
bool(false)
71+
bool(false)
72+
bool(true)
73+
bool(false)
74+
===DONE===

0 commit comments

Comments
 (0)