Skip to content

Commit 9c0b848

Browse files
bjorijmikola
authored andcommitted
PHPC-373: Get rid of MAKE_STD_ZVAL() and use bson_t instead
1 parent c601054 commit 9c0b848

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

php_phongo.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,14 @@ void phongo_writeconcern_init(zval *return_value, const mongoc_write_concern_t *
318318

319319
bool phongo_query_init(php_phongo_query_t *query, zval *filter, zval *options TSRMLS_DC) /* {{{ */
320320
{
321-
zval *zquery = NULL;
321+
bson_t bfilter;
322322

323323
if (!(Z_TYPE_P(filter) == IS_ARRAY || Z_TYPE_P(filter) == IS_OBJECT)) {
324324
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected filter to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(filter)));
325325
return false;
326326
}
327327
convert_to_object(filter);
328328

329-
MAKE_STD_ZVAL(zquery);
330-
array_init(zquery);
331-
332329
if (options) {
333330
/* TODO: Ensure batchSize, limit, and skip are 32-bit */
334331
query->batch_size = php_array_fetchc_long(options, "batchSize");
@@ -350,19 +347,17 @@ bool phongo_query_init(php_phongo_query_t *query, zval *filter, zval *options TS
350347

351348
if (modifiers && !(Z_TYPE_P(modifiers) == IS_ARRAY || Z_TYPE_P(modifiers) == IS_OBJECT)) {
352349
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected modifiers to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(modifiers)));
353-
zval_ptr_dtor(&zquery);
354350
return false;
355351
}
356352

357-
zend_hash_merge(HASH_OF(zquery), HASH_OF(modifiers), (void (*)(void*))zval_add_ref, NULL, sizeof(zval *), 1);
353+
zval_to_bson(modifiers, PHONGO_BSON_NONE, query->query, NULL TSRMLS_CC);
358354
}
359355

360356
if (php_array_existsc(options, "projection")) {
361357
zval *projection = php_array_fetchc(options, "projection");
362358

363359
if (projection && !(Z_TYPE_P(projection) == IS_ARRAY || Z_TYPE_P(projection) == IS_OBJECT)) {
364360
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected projection to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(projection)));
365-
zval_ptr_dtor(&zquery);
366361
return false;
367362
}
368363

@@ -371,27 +366,25 @@ bool phongo_query_init(php_phongo_query_t *query, zval *filter, zval *options TS
371366
}
372367

373368
if (php_array_existsc(options, "sort")) {
369+
bson_t bsort = BSON_INITIALIZER;
374370
zval *sort = php_array_fetchc(options, "sort");
375371

376372
if (sort && !(Z_TYPE_P(sort) == IS_ARRAY || Z_TYPE_P(sort) == IS_OBJECT)) {
377373
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected sort to be array or object, %s given", zend_get_type_by_const(Z_TYPE_P(sort)));
378-
zval_ptr_dtor(&zquery);
379374
return false;
380375
}
381376

382377
convert_to_object_ex(&sort);
383-
Z_ADDREF_P(sort);
384-
add_assoc_zval_ex(zquery, ZEND_STRS("$orderby"), sort);
378+
zval_to_bson(sort, PHONGO_BSON_NONE, &bsort, NULL TSRMLS_CC);
379+
BSON_APPEND_DOCUMENT(query->query, "$orderby", &bsort);
380+
bson_destroy(&bsort);
385381
}
386382
}
387383

388-
Z_ADDREF_P(filter);
389-
add_assoc_zval_ex(zquery, ZEND_STRS("$query"), filter);
390-
391-
query->query = bson_new();
392-
zval_to_bson(zquery, PHONGO_BSON_NONE, query->query, NULL TSRMLS_CC);
393-
zval_ptr_dtor(&zquery);
394-
384+
bson_init(&bfilter);
385+
zval_to_bson(filter, PHONGO_BSON_NONE, &bfilter, NULL TSRMLS_CC);
386+
BSON_APPEND_DOCUMENT(query->query, "$query", &bfilter);
387+
bson_destroy(&bfilter);
395388
return true;
396389
} /* }}} */
397390

src/MongoDB/Query.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ PHP_METHOD(Query, __construct)
6767
zend_restore_error_handling(&error_handling TSRMLS_CC);
6868

6969

70-
phongo_query_init(intern, filter, options TSRMLS_CC);
70+
intern->query = bson_new();
71+
if (!phongo_query_init(intern, filter, options TSRMLS_CC)) {
72+
bson_clear(&intern->query);
73+
}
7174
}
7275
/* }}} */
7376

0 commit comments

Comments
 (0)