|
21 | 21 | #include <php.h>
|
22 | 22 | #include <Zend/zend_interfaces.h>
|
23 | 23 |
|
| 24 | +#include "php_array_api.h" |
24 | 25 | #include "phongo_compat.h"
|
25 | 26 | #include "php_phongo.h"
|
26 | 27 | #include "php_bson.h"
|
27 | 28 |
|
28 | 29 | zend_class_entry *php_phongo_command_ce;
|
29 | 30 |
|
30 |
| -/* {{{ proto void MongoDB\Driver\Command::__construct(array|object $document) |
| 31 | +/* Initialize the "maxAwaitTimeMS" option. Returns true on success; otherwise, |
| 32 | + * false is returned and an exception is thrown. |
| 33 | + * |
| 34 | + * The "maxAwaitTimeMS" option is assigned to the cursor after query execution |
| 35 | + * via mongoc_cursor_set_max_await_time_ms(). */ |
| 36 | +static bool php_phongo_command_init_max_await_time_ms(php_phongo_command_t *intern, zval *options TSRMLS_DC) /* {{{ */ |
| 37 | +{ |
| 38 | + if (php_array_existsc(options, "maxAwaitTimeMS")) { |
| 39 | + int64_t max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS"); |
| 40 | + |
| 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 | + } |
| 45 | + |
| 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 | + } |
| 50 | + |
| 51 | + intern->max_await_time_ms = (uint32_t) max_await_time_ms; |
| 52 | + } |
| 53 | + |
| 54 | + return true; |
| 55 | +} /* }}} */ |
| 56 | + |
| 57 | +/* Initializes the php_phongo_command_init from options argument. This |
| 58 | + * function will fall back to a modifier in the absence of a top-level option |
| 59 | + * (where applicable). */ |
| 60 | +static bool php_phongo_command_init(php_phongo_command_t *intern, zval *filter, zval *options TSRMLS_DC) /* {{{ */ |
| 61 | +{ |
| 62 | + intern->bson = bson_new(); |
| 63 | + |
| 64 | + php_phongo_zval_to_bson(filter, PHONGO_BSON_NONE, intern->bson, NULL TSRMLS_CC); |
| 65 | + |
| 66 | + /* Note: if any exceptions are thrown, we can simply return as PHP will |
| 67 | + * invoke php_phongo_query_free_object to destruct the object. */ |
| 68 | + if (EG(exception)) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + if (!options) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + if (!php_phongo_command_init_max_await_time_ms(intern, options TSRMLS_CC)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | +} /* }}} */ |
| 82 | + |
| 83 | +/* {{{ proto void MongoDB\Driver\Command::__construct(array|object $document[, array $options = array()]) |
31 | 84 | Constructs a new Command */
|
32 | 85 | static PHP_METHOD(Command, __construct)
|
33 | 86 | {
|
34 | 87 | php_phongo_command_t *intern;
|
35 | 88 | zend_error_handling error_handling;
|
36 | 89 | zval *document;
|
37 |
| - bson_t *bson = bson_new(); |
| 90 | + zval *options = NULL; |
38 | 91 | SUPPRESS_UNUSED_WARNING(return_value) SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used)
|
39 | 92 |
|
40 | 93 |
|
41 | 94 | zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
|
42 | 95 | intern = Z_COMMAND_OBJ_P(getThis());
|
43 | 96 |
|
44 |
| - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "A", &document) == FAILURE) { |
| 97 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "A|a!", &document, &options) == FAILURE) { |
45 | 98 | zend_restore_error_handling(&error_handling TSRMLS_CC);
|
46 | 99 | return;
|
47 | 100 | }
|
48 | 101 | zend_restore_error_handling(&error_handling TSRMLS_CC);
|
49 | 102 |
|
50 |
| - |
51 |
| - php_phongo_zval_to_bson(document, PHONGO_BSON_NONE, bson, NULL TSRMLS_CC); |
52 |
| - intern->bson = bson; |
| 103 | + php_phongo_command_init(intern, document, options TSRMLS_CC); |
53 | 104 | } /* }}} */
|
54 | 105 |
|
55 | 106 | /* {{{ MongoDB\Driver\Command function entries */
|
|
0 commit comments