Skip to content

Commit 4fd0fa9

Browse files
authored
PHPC-2141: Emit deprecation notice for WriteResult getters with unacknowledged writes (#1367)
1 parent d6b2fd6 commit 4fd0fa9

11 files changed

+45
-11
lines changed

src/MongoDB/WriteResult.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@
3131
#include "MongoDB/WriteError.h"
3232
#include "WriteResult_arginfo.h"
3333

34+
#define PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED(method) \
35+
if (!mongoc_write_concern_is_acknowledged(intern->write_concern)) { \
36+
php_error_docref(NULL, E_DEPRECATED, "Calling MongoDB\\Driver\\WriteResult::" method "() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0"); \
37+
RETURN_NULL(); \
38+
}
39+
3440
#define PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(iter, bson, key) \
3541
if (bson_iter_init_find((iter), (bson), (key)) && BSON_ITER_HOLDS_INT32((iter))) { \
3642
RETURN_LONG(bson_iter_int32((iter))); \
37-
}
43+
} \
44+
RETURN_LONG(0);
3845

3946
zend_class_entry* php_phongo_writeresult_ce;
4047

@@ -123,6 +130,8 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getInsertedCount)
123130

124131
PHONGO_PARSE_PARAMETERS_NONE();
125132

133+
PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED("getInsertedCount");
134+
126135
PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(&iter, intern->reply, "nInserted");
127136
}
128137

@@ -136,6 +145,8 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getMatchedCount)
136145

137146
PHONGO_PARSE_PARAMETERS_NONE();
138147

148+
PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED("getMatchedCount");
149+
139150
PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(&iter, intern->reply, "nMatched");
140151
}
141152

@@ -149,6 +160,8 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getModifiedCount)
149160

150161
PHONGO_PARSE_PARAMETERS_NONE();
151162

163+
PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED("getModifiedCount");
164+
152165
PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(&iter, intern->reply, "nModified");
153166
}
154167

@@ -162,6 +175,8 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getDeletedCount)
162175

163176
PHONGO_PARSE_PARAMETERS_NONE();
164177

178+
PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED("getDeletedCount");
179+
165180
PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(&iter, intern->reply, "nRemoved");
166181
}
167182

@@ -175,6 +190,8 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getUpsertedCount)
175190

176191
PHONGO_PARSE_PARAMETERS_NONE();
177192

193+
PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED("getUpsertedCount");
194+
178195
PHONGO_WRITERESULT_RETURN_LONG_FROM_BSON_INT32(&iter, intern->reply, "nUpserted");
179196
}
180197

@@ -200,6 +217,8 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getUpsertedIds)
200217

201218
PHONGO_PARSE_PARAMETERS_NONE();
202219

220+
PHONGO_WRITERESULT_CHECK_ACKNOWLEDGED("getUpsertedIds");
221+
203222
array_init(return_value);
204223

205224
if (bson_iter_init_find(&iter, intern->reply, "upserted") && BSON_ITER_HOLDS_ARRAY(&iter) && bson_iter_recurse(&iter, &child)) {

tests/manager/manager-executeBulkWrite-012.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ foreach ($writeConcerns as $wc) {
2929
?>
3030
===DONE===
3131
<?php exit(0); ?>
32-
--EXPECT--
32+
--EXPECTF--
3333
bool(false)
34+
35+
Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
3436
NULL
3537
bool(true)
3638
int(1)

tests/server/server-executeBulkWrite-002.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ foreach ($writeConcerns as $writeConcern) {
2525
?>
2626
===DONE===
2727
<?php exit(0); ?>
28-
--EXPECT--
28+
--EXPECTF--
2929
bool(false)
30+
31+
Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
3032
NULL
3133
bool(true)
3234
int(1)

tests/server/server-executeBulkWrite-003.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ foreach ($writeConcerns as $wc) {
2626
?>
2727
===DONE===
2828
<?php exit(0); ?>
29-
--EXPECT--
29+
--EXPECTF--
3030
bool(false)
31+
32+
Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
3133
NULL
3234
bool(true)
3335
int(1)

tests/server/server-executeBulkWrite-005.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ $server->executeBulkWrite('local.' . COLLECTION_NAME, $bulk);
3535
?>
3636
===DONE===
3737
<?php exit(0); ?>
38-
--EXPECT--
38+
--EXPECTF--
3939
bool(false)
40+
41+
Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
4042
NULL
4143
bool(true)
4244
int(1)

tests/server/server-executeBulkWrite-006.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ foreach ($writeConcerns as $wc) {
3030
?>
3131
===DONE===
3232
<?php exit(0); ?>
33-
--EXPECT--
33+
--EXPECTF--
3434
bool(false)
35+
36+
Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
3537
NULL
3638
bool(true)
3739
int(1)

tests/writeResult/writeresult-getdeletedcount-002.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var_dump($result->getDeletedCount());
2424
?>
2525
===DONE===
2626
<?php exit(0); ?>
27-
--EXPECT--
27+
--EXPECTF--
28+
Deprecated: MongoDB\Driver\WriteResult::getDeletedCount(): Calling MongoDB\Driver\WriteResult::getDeletedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
2829
NULL
2930
===DONE===

tests/writeResult/writeresult-getinsertedcount-002.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var_dump($result->getInsertedCount());
2424
?>
2525
===DONE===
2626
<?php exit(0); ?>
27-
--EXPECT--
27+
--EXPECTF--
28+
Deprecated: MongoDB\Driver\WriteResult::getInsertedCount(): Calling MongoDB\Driver\WriteResult::getInsertedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
2829
NULL
2930
===DONE===

tests/writeResult/writeresult-getmatchedcount-002.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var_dump($result->getMatchedCount());
2424
?>
2525
===DONE===
2626
<?php exit(0); ?>
27-
--EXPECT--
27+
--EXPECTF--
28+
Deprecated: MongoDB\Driver\WriteResult::getMatchedCount(): Calling MongoDB\Driver\WriteResult::getMatchedCount() for an unacknowledged write is deprecated and will throw an exception in %s
2829
NULL
2930
===DONE===

tests/writeResult/writeresult-getmodifiedcount-002.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var_dump($result->getModifiedCount());
2424
?>
2525
===DONE===
2626
<?php exit(0); ?>
27-
--EXPECT--
27+
--EXPECTF--
28+
Deprecated: MongoDB\Driver\WriteResult::getModifiedCount(): Calling MongoDB\Driver\WriteResult::getModifiedCount() for an unacknowledged write is deprecated and will throw an exception in ext-mongodb 2.0 in %s
2829
NULL
2930
===DONE===

0 commit comments

Comments
 (0)