Skip to content

Commit 887f067

Browse files
committed
PHPC-734: BulkWrite update and delete "collation" option
1 parent 943a00e commit 887f067

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/MongoDB/BulkWrite.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ static inline bool php_phongo_bulkwrite_update_has_operators(bson_t *bupdate) /*
6565
return false;
6666
} /* }}} */
6767

68+
/* Appends a document field for the given opts document and key. Returns true on
69+
* success; otherwise, false is returned and an exception is thrown. */
70+
static bool php_phongo_bulkwrite_opts_append_document(bson_t *opts, const char *opts_key, zval *zarr, const char *zarr_key TSRMLS_DC)
71+
{
72+
zval *value = php_array_fetch(zarr, zarr_key);
73+
bson_t b = BSON_INITIALIZER;
74+
75+
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
76+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" option to be array or object, %s given", zarr_key, zend_get_type_by_const(Z_TYPE_P(value)));
77+
return false;
78+
}
79+
80+
phongo_zval_to_bson(value, PHONGO_BSON_NONE, &b, NULL TSRMLS_CC);
81+
82+
if (EG(exception)) {
83+
bson_destroy(&b);
84+
return false;
85+
}
86+
87+
if (!BSON_APPEND_DOCUMENT(opts, opts_key, &b)) {
88+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error appending \"%s\" option", opts_key);
89+
bson_destroy(&b);
90+
return false;
91+
}
92+
93+
bson_destroy(&b);
94+
return true;
95+
}
96+
6897
#define PHONGO_BULKWRITE_APPEND_BOOL(opt, value) \
6998
if (!BSON_APPEND_BOOL(boptions, (opt), (value))) { \
7099
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error appending \"%s\" option", (opt)); \
@@ -77,6 +106,13 @@ static inline bool php_phongo_bulkwrite_update_has_operators(bson_t *bupdate) /*
77106
return false; \
78107
}
79108

109+
#define PHONGO_BULKWRITE_OPT_DOCUMENT(opt) \
110+
if (zoptions && php_array_existsc(zoptions, (opt))) { \
111+
if (!php_phongo_bulkwrite_opts_append_document(boptions, (opt), zoptions, (opt) TSRMLS_CC)) { \
112+
return false; \
113+
} \
114+
}
115+
80116
/* Applies options (including defaults) for an update operation. */
81117
static bool php_phongo_bulkwrite_update_apply_options(bson_t *boptions, zval *zoptions TSRMLS_DC)/* {{{ */
82118
{
@@ -93,6 +129,7 @@ static bool php_phongo_bulkwrite_update_apply_options(bson_t *boptions, zval *zo
93129

94130
PHONGO_BULKWRITE_APPEND_BOOL("multi", multi);
95131
PHONGO_BULKWRITE_APPEND_BOOL("upsert", upsert);
132+
PHONGO_BULKWRITE_OPT_DOCUMENT("collation");
96133

97134
return true;
98135
} /* }}} */
@@ -109,6 +146,7 @@ static bool php_phongo_bulkwrite_delete_apply_options(bson_t *boptions, zval *zo
109146
}
110147

111148
PHONGO_BULKWRITE_APPEND_INT32("limit", limit);
149+
PHONGO_BULKWRITE_OPT_DOCUMENT("collation");
112150

113151
return true;
114152
} /* }}} */

tests/bulk/bulkwrite-update_error-001.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ echo throws(function() use ($bulk) {
1313

1414
echo throws(function() use ($bulk) {
1515
$bulk->update(['x' => 1], ['y' => 1], ['multi' => true]);
16-
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n\n";
1717

18+
echo throws(function() use ($bulk) {
19+
$bulk->update([], [], ['collation' => 1]);
20+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
1821
?>
1922
===DONE===
2023
<?php exit(0); ?>
@@ -24,4 +27,7 @@ Replacement document may not contain "$" or "." in keys
2427

2528
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2629
Replacement document conflicts with true "multi" option
30+
31+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
32+
Expected "collation" option to be array or object, integer given
2733
===DONE===

0 commit comments

Comments
 (0)