Skip to content

Commit 52dfd6b

Browse files
committed
PHPC-444: Support options array in BulkWrite and executeInsert()
1 parent 82a31a3 commit 52dfd6b

27 files changed

+43
-35
lines changed

scripts/convert-mo-tests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ function clientOperation($phase, &$output) {
242242
$retval = <<< CODE
243243
try {
244244
\$wc = new MongoDB\\Driver\\WriteConcern($wc);
245-
\$result = \$manager->executeInsert("databaseName.collectionName", $doc, \$wc);
245+
\$result = \$manager->executeInsert("databaseName.collectionName", $doc, [], \$wc);
246246
if (\$result->getInsertedCount() == 1) {
247247
var_dump(array("ok" => 1));
248248
} else {

src/MongoDB/BulkWrite.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,29 @@ PHONGO_API zend_class_entry *php_phongo_bulkwrite_ce;
5151

5252
zend_object_handlers php_phongo_handler_bulkwrite;
5353

54-
/* {{{ proto MongoDB\Driver\BulkWrite BulkWrite::__construct(boolean $ordered)
54+
/* {{{ proto MongoDB\Driver\BulkWrite BulkWrite::__construct([array $options = array()])
5555
Constructs a new BulkWrite */
5656
PHP_METHOD(BulkWrite, __construct)
5757
{
5858
php_phongo_bulkwrite_t *intern;
5959
zend_error_handling error_handling;
60+
zval *options = NULL;
6061
zend_bool ordered = 1;
6162
(void)return_value_ptr; (void)return_value; (void)return_value_used;
6263

6364

6465
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
6566
intern = (php_phongo_bulkwrite_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
6667

67-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &ordered) == FAILURE) {
68+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!", &options) == FAILURE) {
6869
zend_restore_error_handling(&error_handling TSRMLS_CC);
6970
return;
7071
}
7172
zend_restore_error_handling(&error_handling TSRMLS_CC);
7273

74+
if (options && php_array_exists(options, "ordered")) {
75+
ordered = php_array_fetch_bool(options, "ordered");
76+
}
7377

7478
intern->bulk = phongo_bulkwrite_init(ordered);
7579
}
@@ -225,7 +229,7 @@ PHP_METHOD(BulkWrite, count)
225229
/* {{{ MongoDB\Driver\BulkWrite */
226230

227231
ZEND_BEGIN_ARG_INFO_EX(ai_BulkWrite___construct, 0, 0, 0)
228-
ZEND_ARG_INFO(0, ordered)
232+
ZEND_ARG_ARRAY_INFO(0, options, 1)
229233
ZEND_END_ARG_INFO();
230234

231235
ZEND_BEGIN_ARG_INFO_EX(ai_BulkWrite_insert, 0, 0, 1)

src/MongoDB/Manager.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,33 @@ PHP_METHOD(Manager, executeBulkWrite)
180180
phongo_execute_write(intern->client, namespace, bulk->bulk, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), -1, return_value, return_value_used TSRMLS_CC);
181181
}
182182
/* }}} */
183-
/* {{{ proto MongoDB\Driver\WriteResult Manager::executeInsert(string $namespace, array|object $document[, MongoDB\Driver\WriteConcern $writeConcern = null])
183+
/* {{{ proto MongoDB\Driver\WriteResult Manager::executeInsert(string $namespace, array|object $document[, array $insertOptions = array()[, MongoDB\Driver\WriteConcern $writeConcern = null]])
184184
Convenience method for single insert operation. */
185185
PHP_METHOD(Manager, executeInsert)
186186
{
187187
php_phongo_manager_t *intern;
188188
char *namespace;
189189
int namespace_len;
190190
zval *document;
191+
zval *insertOptions = NULL;
191192
zval *zwrite_concern = NULL;
192193
bson_t *bson;
193194
(void)return_value_ptr;
194195

195196

196197
intern = (php_phongo_manager_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
197198

198-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sA|O!", &namespace, &namespace_len, &document, &zwrite_concern, php_phongo_writeconcern_ce) == FAILURE) {
199+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sA|a!O!", &namespace, &namespace_len, &document, &insertOptions, &zwrite_concern, php_phongo_writeconcern_ce) == FAILURE) {
199200
return;
200201
}
201202

202203

203204
bson = bson_new();
204205
zval_to_bson(document, PHONGO_BSON_ADD_ODS|PHONGO_BSON_ADD_CHILD_ODS, bson, NULL TSRMLS_CC);
206+
207+
/* When PHPC-443 is implemented, insertOptions will be parsed for the
208+
* bypassDocumentValidation option; however, there's nothing to do now. */
209+
205210
phongo_execute_single_insert(intern->client, namespace, bson, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), -1, return_value, return_value_used TSRMLS_CC);
206211
bson_clear(&bson);
207212
}
@@ -422,6 +427,7 @@ ZEND_END_ARG_INFO();
422427
ZEND_BEGIN_ARG_INFO_EX(ai_Manager_executeInsert, 0, 0, 2)
423428
ZEND_ARG_INFO(0, namespace)
424429
ZEND_ARG_INFO(0, document)
430+
ZEND_ARG_ARRAY_INFO(0, insertOptions, 1)
425431
ZEND_ARG_OBJ_INFO(0, writeConcern, MongoDB\\Driver\\WriteConcern, 1)
426432
ZEND_END_ARG_INFO();
427433

tests/bulk/write-0002.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ $manager = new MongoDB\Driver\Manager(STANDALONE);
1010

1111
$hannes = array("name" => "Hannes", "country" => "USA", "gender" => "male");
1212
$hayley = array("name" => "Hayley", "country" => "USA", "gender" => "female");
13-
$ordered = true;
1413

15-
$insertBulk = new \MongoDB\Driver\BulkWrite($ordered);
14+
$insertBulk = new \MongoDB\Driver\BulkWrite(['ordered' => true]);
1615
$hannes_id = $insertBulk->insert($hannes);
1716
$hayley_id = $insertBulk->insert($hayley);
1817

tests/cluster/rs/connection/read/primary-available.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ try {
9191
$wc = new MongoDB\Driver\WriteConcern(1);
9292
$result = $manager->executeInsert("databaseName.collectionName", array (
9393
'x' => 1,
94-
), $wc);
94+
), [], $wc);
9595
if ($result->getInsertedCount() == 1) {
9696
var_dump(array("ok" => 1));
9797
} else {

tests/cluster/rs/connection/read/primary-not-available.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ try {
9191
$wc = new MongoDB\Driver\WriteConcern(1);
9292
$result = $manager->executeInsert("databaseName.collectionName", array (
9393
'x' => 1,
94-
), $wc);
94+
), [], $wc);
9595
if ($result->getInsertedCount() == 1) {
9696
var_dump(array("ok" => 1));
9797
} else {

tests/cluster/rs/connection/write/primary-available.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ try {
9191
$wc = new MongoDB\Driver\WriteConcern(1);
9292
$result = $manager->executeInsert("databaseName.collectionName", array (
9393
'x' => 1,
94-
), $wc);
94+
), [], $wc);
9595
if ($result->getInsertedCount() == 1) {
9696
var_dump(array("ok" => 1));
9797
} else {

tests/cluster/rs/connection/write/primary-not-available.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ try {
9595
$wc = new MongoDB\Driver\WriteConcern(1);
9696
$result = $manager->executeInsert("databaseName.collectionName", array (
9797
'x' => 1,
98-
), $wc);
98+
), [], $wc);
9999
if ($result->getInsertedCount() == 1) {
100100
var_dump(array("ok" => 1));
101101
} else {

tests/cluster/rs/connection/write/primary-restarted.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ try {
9595
$wc = new MongoDB\Driver\WriteConcern(1);
9696
$result = $manager->executeInsert("databaseName.collectionName", array (
9797
'x' => 1,
98-
), $wc);
98+
), [], $wc);
9999
if ($result->getInsertedCount() == 1) {
100100
var_dump(array("ok" => 1));
101101
} else {
@@ -113,7 +113,7 @@ try {
113113
$wc = new MongoDB\Driver\WriteConcern(1);
114114
$result = $manager->executeInsert("databaseName.collectionName", array (
115115
'x' => 2,
116-
), $wc);
116+
), [], $wc);
117117
if ($result->getInsertedCount() == 1) {
118118
var_dump(array("ok" => 1));
119119
} else {

tests/cluster/rs/discovery/remove-all-add-members.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ try {
109109
$wc = new MongoDB\Driver\WriteConcern(1);
110110
$result = $manager->executeInsert("databaseName.collectionName", array (
111111
'x' => 1,
112-
), $wc);
112+
), [], $wc);
113113
if ($result->getInsertedCount() == 1) {
114114
var_dump(array("ok" => 1));
115115
} else {

0 commit comments

Comments
 (0)