Skip to content

Commit bda6f5e

Browse files
committed
Merge pull request #26
2 parents 0d25dfa + ae0219d commit bda6f5e

10 files changed

+269
-21
lines changed

php_phongo.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,15 @@ bool phongo_execute_write(mongoc_client_t *client, char *namespace, mongoc_bulk_
484484
mongoc_bulk_operation_set_database(bulk, dbname);
485485
mongoc_bulk_operation_set_collection(bulk, collname);
486486
mongoc_bulk_operation_set_client(bulk, client);
487-
mongoc_bulk_operation_set_write_concern (bulk, write_concern);
487+
488+
/* If a write concern was not specified, libmongoc will use the client's
489+
* write concern; however, we should still fetch it for the write result. */
490+
if (write_concern) {
491+
mongoc_bulk_operation_set_write_concern(bulk, write_concern);
492+
} else {
493+
write_concern = mongoc_client_get_write_concern(client);
494+
}
495+
488496
efree(dbname);
489497
efree(collname);
490498

@@ -505,6 +513,7 @@ bool phongo_execute_write(mongoc_client_t *client, char *namespace, mongoc_bulk_
505513
}
506514

507515
writeresult = phongo_writeresult_init(return_value, &bulk->result, server_hint TSRMLS_CC);
516+
writeresult->write_concern = mongoc_write_concern_copy(write_concern);
508517

509518
/* The Write failed */
510519
if (!hint) {
@@ -1051,21 +1060,17 @@ void php_phongo_read_preference_to_zval(zval *retval, mongoc_read_prefs_t *read_
10511060

10521061
void php_phongo_write_concern_to_zval(zval *retval, mongoc_write_concern_t *write_concern) /* {{{ */
10531062
{
1054-
char *wtag;
1063+
const char *wtag = mongoc_write_concern_get_wtag(write_concern);
1064+
const int32_t w = mongoc_write_concern_get_w(write_concern);
10551065

10561066
array_init_size(retval, 5);
10571067

1058-
if (mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_DEFAULT) {
1059-
return;
1060-
}
1061-
1062-
wtag = (char *)mongoc_write_concern_get_wtag(write_concern);
10631068
if (wtag) {
10641069
add_assoc_string_ex(retval, ZEND_STRS("w"), wtag, 1);
1065-
} else {
1066-
if (!mongoc_write_concern_get_wmajority(write_concern)) {
1067-
add_assoc_long_ex(retval, ZEND_STRS("w"), mongoc_write_concern_get_w(write_concern));
1068-
}
1070+
} else if (mongoc_write_concern_get_wmajority(write_concern)) {
1071+
add_assoc_string_ex(retval, ZEND_STRS("w"), "majority", 1);
1072+
} else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) {
1073+
add_assoc_long_ex(retval, ZEND_STRS("w"), w);
10691074
}
10701075

10711076
add_assoc_bool_ex(retval, ZEND_STRS("wmajority"), mongoc_write_concern_get_wmajority(write_concern));

php_phongo_classes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ typedef struct {
129129
zend_object std;
130130
mongoc_write_result_t write_result;
131131
int hint;
132+
mongoc_write_concern_t *write_concern;
132133
} php_phongo_writeresult_t;
133134

134135
typedef struct {

src/MongoDB/WriteResult.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/* External libs */
2828
#include <bson.h>
2929
#include <mongoc.h>
30+
#include <mongoc-write-concern-private.h>
3031

3132
/* PHP Core stuff */
3233
#include <php.h>
@@ -310,6 +311,24 @@ PHP_METHOD(WriteResult, getWriteErrors)
310311
}
311312
}
312313
/* }}} */
314+
/* {{{ proto boolean WriteResult::isAcknowledged()
315+
Returns the number of documents that were upserted */
316+
PHP_METHOD(WriteResult, isAcknowledged)
317+
{
318+
php_phongo_writeresult_t *intern;
319+
(void)return_value_ptr; (void)return_value_used;
320+
321+
322+
intern = (php_phongo_writeresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
323+
324+
if (zend_parse_parameters_none() == FAILURE) {
325+
return;
326+
}
327+
328+
329+
RETURN_BOOL(_mongoc_write_concern_needs_gle(intern->write_concern));
330+
}
331+
/* }}} */
313332

314333
/**
315334
* Result returned by Server and Manager executeBulkWrite() methods.
@@ -349,6 +368,9 @@ ZEND_END_ARG_INFO();
349368
ZEND_BEGIN_ARG_INFO_EX(ai_WriteResult_getWriteErrors, 0, 0, 0)
350369
ZEND_END_ARG_INFO();
351370

371+
ZEND_BEGIN_ARG_INFO_EX(ai_WriteResult_isAcknowledged, 0, 0, 0)
372+
ZEND_END_ARG_INFO();
373+
352374

353375
static zend_function_entry php_phongo_writeresult_me[] = {
354376
PHP_ME(WriteResult, getInsertedCount, ai_WriteResult_getInsertedCount, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
@@ -361,6 +383,7 @@ static zend_function_entry php_phongo_writeresult_me[] = {
361383
PHP_ME(WriteResult, getUpsertedIds, ai_WriteResult_getUpsertedIds, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
362384
PHP_ME(WriteResult, getwriteConcernError, ai_WriteResult_getwriteConcernError, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
363385
PHP_ME(WriteResult, getWriteErrors, ai_WriteResult_getWriteErrors, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
386+
PHP_ME(WriteResult, isAcknowledged, ai_WriteResult_isAcknowledged, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
364387
PHP_FE_END
365388
};
366389

@@ -376,6 +399,10 @@ static void php_phongo_writeresult_free_object(void *object TSRMLS_DC) /* {{{ */
376399

377400
_mongoc_write_result_destroy(&intern->write_result);
378401

402+
if (intern->write_concern) {
403+
mongoc_write_concern_destroy(intern->write_concern);
404+
}
405+
379406
efree(intern);
380407
} /* }}} */
381408

@@ -405,7 +432,7 @@ HashTable *php_phongo_writeresult_get_debug_info(zval *object, int *is_temp TSRM
405432

406433
intern = (php_phongo_writeresult_t *)zend_object_store_get_object(object TSRMLS_CC);
407434
*is_temp = 1;
408-
array_init_size(&retval, 8);
435+
array_init_size(&retval, 9);
409436

410437
add_assoc_long_ex(&retval, ZEND_STRS("nInserted"), intern->write_result.nInserted);
411438
add_assoc_long_ex(&retval, ZEND_STRS("nMatched"), intern->write_result.nMatched);
@@ -426,6 +453,15 @@ HashTable *php_phongo_writeresult_get_debug_info(zval *object, int *is_temp TSRM
426453
bson_to_zval(bson_get_data(&intern->write_result.writeConcernError), intern->write_result.writeConcernError.len, &state);
427454
add_assoc_zval_ex(&retval, ZEND_STRS("writeConcernError"), state.zchild);
428455

456+
if (intern->write_concern) {
457+
zval *write_concern = NULL;
458+
MAKE_STD_ZVAL(write_concern);
459+
php_phongo_write_concern_to_zval(write_concern, intern->write_concern);
460+
add_assoc_zval_ex(&retval, ZEND_STRS("writeConcern"), write_concern);
461+
} else {
462+
add_assoc_null_ex(&retval, ZEND_STRS("writeConcern"));
463+
}
464+
429465
return Z_ARRVAL(retval);
430466
} /* }}} */
431467
/* }}} */

tests/bulk/write-0001.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ object(MongoDB\Driver\BulkWrite)#%d (%d) {
106106
["hint"]=>
107107
int(0)
108108
["write_concern"]=>
109-
array(0) {
110-
}
109+
NULL
111110
}
112111
Inserted: 3
113112
Deleted: 1

tests/standalone/server-executeBulkWrite-001.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ object(MongoDB\Driver\WriteResult)#%d (%d) {
6969
["writeConcernError"]=>
7070
array(0) {
7171
}
72+
["writeConcern"]=>
73+
array(4) {
74+
["wmajority"]=>
75+
bool(false)
76+
["wtimeout"]=>
77+
int(0)
78+
["fsync"]=>
79+
bool(false)
80+
["journal"]=>
81+
bool(false)
82+
}
7283
}
7384

7485
===> Collection
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
MongoDB\Driver\WriteResult::isAcknowledged() with default WriteConcern
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
10+
11+
$result = $manager->executeInsert(NS, array('x' => 1));
12+
13+
printf("WriteResult::isAcknowledged(): %s\n", $result->isAcknowledged() ? 'true' : 'false');
14+
var_dump($result);
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECTF--
20+
WriteResult::isAcknowledged(): true
21+
object(MongoDB\Driver\WriteResult)#%d (%d) {
22+
["nInserted"]=>
23+
int(1)
24+
["nMatched"]=>
25+
int(0)
26+
["nModified"]=>
27+
int(0)
28+
["nRemoved"]=>
29+
int(0)
30+
["nUpserted"]=>
31+
int(0)
32+
["upsertedIds"]=>
33+
array(0) {
34+
}
35+
["writeErrors"]=>
36+
array(0) {
37+
}
38+
["writeConcernError"]=>
39+
array(0) {
40+
}
41+
["writeConcern"]=>
42+
array(4) {
43+
["wmajority"]=>
44+
bool(false)
45+
["wtimeout"]=>
46+
int(0)
47+
["fsync"]=>
48+
bool(false)
49+
["journal"]=>
50+
bool(false)
51+
}
52+
}
53+
===DONE===
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
MongoDB\Driver\WriteResult::isAcknowledged() with inherited WriteConcern
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
/* We use w:0 here because libmongoc detects w:1 as the server's default and
10+
*/
11+
$manager = new MongoDB\Driver\Manager(MONGODB_URI . "/?w=0");
12+
13+
$result = $manager->executeInsert(NS, array('x' => 1));
14+
15+
printf("WriteResult::isAcknowledged(): %s\n", $result->isAcknowledged() ? 'true' : 'false');
16+
var_dump($result);
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECTF--
22+
WriteResult::isAcknowledged(): false
23+
object(MongoDB\Driver\WriteResult)#%d (%d) {
24+
["nInserted"]=>
25+
int(0)
26+
["nMatched"]=>
27+
int(0)
28+
["nModified"]=>
29+
int(0)
30+
["nRemoved"]=>
31+
int(0)
32+
["nUpserted"]=>
33+
int(0)
34+
["upsertedIds"]=>
35+
array(0) {
36+
}
37+
["writeErrors"]=>
38+
array(0) {
39+
}
40+
["writeConcernError"]=>
41+
array(0) {
42+
}
43+
["writeConcern"]=>
44+
array(5) {
45+
["w"]=>
46+
int(0)
47+
["wmajority"]=>
48+
bool(false)
49+
["wtimeout"]=>
50+
int(0)
51+
["fsync"]=>
52+
bool(false)
53+
["journal"]=>
54+
bool(false)
55+
}
56+
}
57+
===DONE===
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
MongoDB\Driver\WriteResult::isAcknowledged() with custom WriteConcern
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
10+
11+
$result = $manager->executeInsert(NS, array('x' => 2), new MongoDB\Driver\WriteConcern(0));
12+
13+
printf("WriteResult::isAcknowledged(): %s\n", $result->isAcknowledged() ? 'true' : 'false');
14+
var_dump($result);
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECTF--
20+
WriteResult::isAcknowledged(): false
21+
object(MongoDB\Driver\WriteResult)#%d (%d) {
22+
["nInserted"]=>
23+
int(0)
24+
["nMatched"]=>
25+
int(0)
26+
["nModified"]=>
27+
int(0)
28+
["nRemoved"]=>
29+
int(0)
30+
["nUpserted"]=>
31+
int(0)
32+
["upsertedIds"]=>
33+
array(0) {
34+
}
35+
["writeErrors"]=>
36+
array(0) {
37+
}
38+
["writeConcernError"]=>
39+
array(0) {
40+
}
41+
["writeConcern"]=>
42+
array(5) {
43+
["w"]=>
44+
int(0)
45+
["wmajority"]=>
46+
bool(false)
47+
["wtimeout"]=>
48+
int(0)
49+
["fsync"]=>
50+
bool(false)
51+
["journal"]=>
52+
bool(false)
53+
}
54+
}
55+
===DONE===

0 commit comments

Comments
 (0)