Skip to content

Commit 0fb7e6d

Browse files
committed
Merge pull request #1029
2 parents f40d2d2 + 0d767a7 commit 0fb7e6d

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

src/MongoDB/BulkWrite.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,8 @@ static bool php_phongo_bulkwrite_update_apply_options(bson_t* boptions, zval* zo
242242
bool multi = false, upsert = false;
243243

244244
if (zoptions) {
245-
if (php_array_existsc(zoptions, "multi")) {
246-
multi = php_array_fetchc_bool(zoptions, "multi");
247-
}
248-
if (php_array_existsc(zoptions, "upsert")) {
249-
upsert = php_array_fetchc_bool(zoptions, "upsert");
250-
}
245+
multi = php_array_fetchc_bool(zoptions, "multi");
246+
upsert = php_array_fetchc_bool(zoptions, "upsert");
251247
}
252248

253249
PHONGO_BULKWRITE_APPEND_BOOL("multi", multi);
@@ -264,9 +260,7 @@ static bool php_phongo_bulkwrite_delete_apply_options(bson_t* boptions, zval* zo
264260
int32_t limit = 0;
265261

266262
if (zoptions) {
267-
if (php_array_existsc(zoptions, "limit")) {
268-
limit = php_array_fetchc_bool(zoptions, "limit") ? 1 : 0;
269-
}
263+
limit = php_array_fetchc_bool(zoptions, "limit") ? 1 : 0;
270264
}
271265

272266
PHONGO_BULKWRITE_APPEND_INT32("limit", limit);
@@ -391,7 +385,7 @@ static PHP_METHOD(BulkWrite, update)
391385
}
392386

393387
if (php_phongo_bulkwrite_update_has_operators(&bupdate) || php_phongo_bulkwrite_update_is_pipeline(&bupdate)) {
394-
if (zoptions && php_array_existsc(zoptions, "multi") && php_array_fetchc_bool(zoptions, "multi")) {
388+
if (zoptions && php_array_fetchc_bool(zoptions, "multi")) {
395389
if (!mongoc_bulk_operation_update_many_with_opts(intern->bulk, &bquery, &bupdate, &boptions, &error)) {
396390
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
397391
goto cleanup;
@@ -403,7 +397,7 @@ static PHP_METHOD(BulkWrite, update)
403397
}
404398
}
405399
} else {
406-
if (zoptions && php_array_existsc(zoptions, "multi") && php_array_fetchc_bool(zoptions, "multi")) {
400+
if (zoptions && php_array_fetchc_bool(zoptions, "multi")) {
407401
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Replacement document conflicts with true \"multi\" option");
408402
goto cleanup;
409403
}
@@ -447,7 +441,7 @@ static PHP_METHOD(BulkWrite, delete)
447441
goto cleanup;
448442
}
449443

450-
if (zoptions && php_array_existsc(zoptions, "limit") && php_array_fetchc_bool(zoptions, "limit")) {
444+
if (zoptions && php_array_fetchc_bool(zoptions, "limit")) {
451445
if (!mongoc_bulk_operation_remove_one_with_opts(intern->bulk, &bquery, &boptions, &error)) {
452446
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
453447
goto cleanup;

src/MongoDB/Command.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,26 @@ zend_class_entry* php_phongo_command_ce;
3535
* via mongoc_cursor_set_max_await_time_ms(). */
3636
static bool php_phongo_command_init_max_await_time_ms(php_phongo_command_t* intern, zval* options TSRMLS_DC) /* {{{ */
3737
{
38-
if (php_array_existsc(options, "maxAwaitTimeMS")) {
39-
int64_t max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
38+
int64_t max_await_time_ms;
4039

41-
if (max_await_time_ms < 0) {
42-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
43-
return false;
44-
}
40+
if (!php_array_existsc(options, "maxAwaitTimeMS")) {
41+
return true;
42+
}
4543

46-
if (max_await_time_ms > UINT32_MAX) {
47-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
48-
return false;
49-
}
44+
max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
5045

51-
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
46+
if (max_await_time_ms < 0) {
47+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
48+
return false;
5249
}
5350

51+
if (max_await_time_ms > UINT32_MAX) {
52+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
53+
return false;
54+
}
55+
56+
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
57+
5458
return true;
5559
} /* }}} */
5660

src/MongoDB/Query.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static bool php_phongo_query_init_hint(php_phongo_query_t* intern, zval* options
171171
* and default singleBatch to true. */
172172
static bool php_phongo_query_init_limit_and_singlebatch(php_phongo_query_t* intern, zval* options TSRMLS_DC) /* {{{ */
173173
{
174-
if (php_array_existsc(options, "limit") && php_array_fetchc_long(options, "limit") < 0) {
174+
if (php_array_fetchc_long(options, "limit") < 0) {
175175
phongo_long limit = php_array_fetchc_long(options, "limit");
176176

177177
if (!BSON_APPEND_INT64(intern->opts, "limit", -limit)) {
@@ -203,17 +203,21 @@ static bool php_phongo_query_init_limit_and_singlebatch(php_phongo_query_t* inte
203203
* which must be converted to a mongoc_read_concern_t. */
204204
static bool php_phongo_query_init_readconcern(php_phongo_query_t* intern, zval* options TSRMLS_DC) /* {{{ */
205205
{
206-
if (php_array_existsc(options, "readConcern")) {
207-
zval* read_concern = php_array_fetchc(options, "readConcern");
206+
zval* read_concern;
208207

209-
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
210-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
211-
return false;
212-
}
208+
if (!php_array_existsc(options, "readConcern")) {
209+
return true;
210+
}
211+
212+
read_concern = php_array_fetchc(options, "readConcern");
213213

214-
intern->read_concern = mongoc_read_concern_copy(phongo_read_concern_from_zval(read_concern TSRMLS_CC));
214+
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
215+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
216+
return false;
215217
}
216218

219+
intern->read_concern = mongoc_read_concern_copy(phongo_read_concern_from_zval(read_concern TSRMLS_CC));
220+
217221
return true;
218222
} /* }}} */
219223

@@ -224,22 +228,26 @@ static bool php_phongo_query_init_readconcern(php_phongo_query_t* intern, zval*
224228
* via mongoc_cursor_set_max_await_time_ms(). */
225229
static bool php_phongo_query_init_max_await_time_ms(php_phongo_query_t* intern, zval* options TSRMLS_DC) /* {{{ */
226230
{
227-
if (php_array_existsc(options, "maxAwaitTimeMS")) {
228-
int64_t max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
231+
int64_t max_await_time_ms;
229232

230-
if (max_await_time_ms < 0) {
231-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
232-
return false;
233-
}
233+
if (!php_array_existsc(options, "maxAwaitTimeMS")) {
234+
return true;
235+
}
234236

235-
if (max_await_time_ms > UINT32_MAX) {
236-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
237-
return false;
238-
}
237+
max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
239238

240-
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
239+
if (max_await_time_ms < 0) {
240+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
241+
return false;
241242
}
242243

244+
if (max_await_time_ms > UINT32_MAX) {
245+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
246+
return false;
247+
}
248+
249+
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
250+
243251
return true;
244252
} /* }}} */
245253

0 commit comments

Comments
 (0)