Skip to content

Commit 8567a79

Browse files
committed
rebase with Result.c
1 parent ab1998f commit 8567a79

File tree

1 file changed

+110
-74
lines changed

1 file changed

+110
-74
lines changed

src/MongoDB/Result.c

Lines changed: 110 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,23 @@
4242
#include "php_bson.h"
4343

4444

45-
PHONGO_API zend_class_entry *php_phongo_commandresult_ce;
45+
PHONGO_API zend_class_entry *php_phongo_result_ce;
4646

47-
zend_object_handlers php_phongo_handler_commandresult;
47+
zend_object_handlers php_phongo_handler_result;
4848

49-
/* {{{ proto MongoDB\Driver\CommandResult CommandResult::__construct(MongoDB\Driver\Server $server, array|object $responseDocument)
50-
Constructs a new CommandResult */
51-
PHP_METHOD(CommandResult, __construct)
49+
/* {{{ proto MongoDB\Driver\Result Result::__construct(MongoDB\Driver\Server $server, array|object $responseDocument)
50+
Constructs a new Result */
51+
PHP_METHOD(Result, __construct)
5252
{
53-
php_phongo_commandresult_t *intern;
53+
php_phongo_result_t *intern;
5454
zend_error_handling error_handling;
5555
zval *server;
5656
zval *responseDocument;
57+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
5758

5859

5960
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
60-
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
61+
intern = (php_phongo_result_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
6162

6263
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OA", &server, php_phongo_server_ce, &responseDocument) == FAILURE) {
6364
zend_restore_error_handling(&error_handling TSRMLS_CC);
@@ -67,43 +68,70 @@ PHP_METHOD(CommandResult, __construct)
6768

6869
}
6970
/* }}} */
70-
/* {{{ proto MongoDB\Driver\Cursor CommandResult::getIterator()
71+
/* {{{ proto void Result::setTypemap(array $typemap)
72+
Sets a typemap to use for BSON unserialization */
73+
PHP_METHOD(Result, setTypemap)
74+
{
75+
php_phongo_result_t *intern;
76+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
77+
zend_error_handling error_handling;
78+
zval *typemap = NULL;
79+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
80+
81+
82+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
83+
intern = (php_phongo_result_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
84+
85+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!", &typemap) == FAILURE) {
86+
zend_restore_error_handling(&error_handling TSRMLS_CC);
87+
return;
88+
}
89+
zend_restore_error_handling(&error_handling TSRMLS_CC);
90+
91+
php_phongo_bson_typemap_to_state(typemap, &state.map TSRMLS_CC);
92+
93+
intern->visitor_data = state;
94+
}
95+
/* }}} */
96+
/* {{{ proto MongoDB\Driver\Cursor Result::getIterator()
7197
Returns the Cursor iterator */
72-
PHP_METHOD(CommandResult, getIterator)
98+
PHP_METHOD(Result, getIterator)
7399
{
74-
php_phongo_commandresult_t *intern;
100+
php_phongo_result_t *intern;
75101
zend_error_handling error_handling;
102+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
76103

77104

78105
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
79-
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
106+
intern = (php_phongo_result_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
80107

81108
if (zend_parse_parameters_none() == FAILURE) {
82109
zend_restore_error_handling(&error_handling TSRMLS_CC);
83110
return;
84111
}
85112
zend_restore_error_handling(&error_handling TSRMLS_CC);
86113

87-
php_phongo_cursor_new_from_result(return_value, &intern->result TSRMLS_CC);
88-
89-
if (intern->result.ce_get_iterator) {
90-
object_init_ex(return_value, intern->result.ce_get_iterator);
114+
if (intern->ce_get_iterator) {
115+
object_init_ex(return_value, intern->ce_get_iterator);
116+
} else {
117+
php_phongo_cursor_new_from_result(return_value, intern TSRMLS_CC);
91118
}
92119

93120
}
94121
/* }}} */
95-
/* {{{ proto self CommandResult::setIteratorClass(string $class)
122+
/* {{{ proto self Result::setIteratorClass(string $class)
96123
Sets the class name of the Cursor implementation to be used */
97-
PHP_METHOD(CommandResult, setIteratorClass)
124+
PHP_METHOD(Result, setIteratorClass)
98125
{
99-
php_phongo_commandresult_t *intern;
126+
php_phongo_result_t *intern;
100127
zend_error_handling error_handling;
101128
char *class;
102129
int class_len;
130+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
103131

104132

105133
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
106-
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
134+
intern = (php_phongo_result_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
107135

108136
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &class, &class_len) == FAILURE) {
109137
zend_restore_error_handling(&error_handling TSRMLS_CC);
@@ -113,17 +141,18 @@ PHP_METHOD(CommandResult, setIteratorClass)
113141

114142
}
115143
/* }}} */
116-
/* {{{ proto self CommandResult::setIteratorInitCallback(callable $callback)
144+
/* {{{ proto self Result::setIteratorInitCallback(callable $callback)
117145
Sets a callback to invoke for initializing a custom Cursor */
118-
PHP_METHOD(CommandResult, setIteratorInitCallback)
146+
PHP_METHOD(Result, setIteratorInitCallback)
119147
{
120-
php_phongo_commandresult_t *intern;
148+
php_phongo_result_t *intern;
121149
zend_error_handling error_handling;
122150
zval *callback;
151+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
123152

124153

125154
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
126-
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
155+
intern = (php_phongo_result_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
127156

128157
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &callback) == FAILURE) {
129158
zend_restore_error_handling(&error_handling TSRMLS_CC);
@@ -133,16 +162,17 @@ PHP_METHOD(CommandResult, setIteratorInitCallback)
133162

134163
}
135164
/* }}} */
136-
/* {{{ proto array CommandResult::getResponseDocument()
165+
/* {{{ proto array Result::toArray()
137166
Returns the original response document from the server */
138-
PHP_METHOD(CommandResult, getResponseDocument)
167+
PHP_METHOD(Result, toArray)
139168
{
140-
php_phongo_commandresult_t *intern;
169+
php_phongo_result_t *intern;
141170
zend_error_handling error_handling;
171+
(void)return_value_ptr; (void)return_value_used;
142172

143173

144174
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
145-
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
175+
intern = (php_phongo_result_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
146176

147177
if (zend_parse_parameters_none() == FAILURE) {
148178
zend_restore_error_handling(&error_handling TSRMLS_CC);
@@ -151,26 +181,27 @@ PHP_METHOD(CommandResult, getResponseDocument)
151181
zend_restore_error_handling(&error_handling TSRMLS_CC);
152182

153183

154-
if (intern->result.firstBatch) {
184+
if (intern->firstBatch) {
155185
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
156186

157187
MAKE_STD_ZVAL(state.zchild);
158-
bson_to_zval(bson_get_data(intern->result.firstBatch), intern->result.firstBatch->len, &state);
188+
bson_to_zval(bson_get_data(intern->firstBatch), intern->firstBatch->len, &state);
159189
RETURN_ZVAL(state.zchild, 0, 1);
160190
}
161191
}
162192
/* }}} */
163-
/* {{{ proto MongoDB\Driver\Server CommandResult::getServer()
193+
/* {{{ proto MongoDB\Driver\Server Result::getServer()
164194
Returns the Server object that this cursor is attached to */
165-
PHP_METHOD(CommandResult, getServer)
195+
PHP_METHOD(Result, getServer)
166196
{
167-
php_phongo_commandresult_t *intern;
197+
php_phongo_result_t *intern;
168198
zend_error_handling error_handling;
169199
mongoc_host_list_t host;
200+
(void)return_value_ptr; (void)return_value_used;
170201

171202

172203
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
173-
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
204+
intern = (php_phongo_result_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
174205

175206
if (zend_parse_parameters_none() == FAILURE) {
176207
zend_restore_error_handling(&error_handling TSRMLS_CC);
@@ -179,109 +210,114 @@ PHP_METHOD(CommandResult, getServer)
179210
zend_restore_error_handling(&error_handling TSRMLS_CC);
180211

181212

182-
mongoc_cursor_get_host(intern->result.cursor, &host);
183-
phongo_server_init(return_value, intern->result.hint, &host TSRMLS_CC);
213+
mongoc_cursor_get_host(intern->cursor, &host);
214+
phongo_server_init(return_value, intern->hint, &host TSRMLS_CC);
184215
}
185216
/* }}} */
186217

187-
/* {{{ MongoDB\Driver\CommandResult */
218+
/* {{{ MongoDB\Driver\Result */
188219

189-
ZEND_BEGIN_ARG_INFO_EX(ai_CommandResult___construct, 0, 0, 2)
220+
ZEND_BEGIN_ARG_INFO_EX(ai_Result___construct, 0, 0, 2)
190221
ZEND_ARG_OBJ_INFO(0, server, MongoDB\\Driver\\Server, 0)
191222
ZEND_ARG_INFO(0, responseDocument)
192223
ZEND_END_ARG_INFO();
193224

194-
ZEND_BEGIN_ARG_INFO_EX(ai_CommandResult_getIterator, 0, 0, 0)
225+
ZEND_BEGIN_ARG_INFO_EX(ai_Result_setTypemap, 0, 0, 1)
226+
ZEND_ARG_ARRAY_INFO(0, typemap, 0)
227+
ZEND_END_ARG_INFO();
228+
229+
ZEND_BEGIN_ARG_INFO_EX(ai_Result_getIterator, 0, 0, 0)
195230
ZEND_END_ARG_INFO();
196231

197-
ZEND_BEGIN_ARG_INFO_EX(ai_CommandResult_setIteratorClass, 0, 0, 1)
232+
ZEND_BEGIN_ARG_INFO_EX(ai_Result_setIteratorClass, 0, 0, 1)
198233
ZEND_ARG_INFO(0, class)
199234
ZEND_END_ARG_INFO();
200235

201-
ZEND_BEGIN_ARG_INFO_EX(ai_CommandResult_setIteratorInitCallback, 0, 0, 1)
236+
ZEND_BEGIN_ARG_INFO_EX(ai_Result_setIteratorInitCallback, 0, 0, 1)
202237
ZEND_ARG_INFO(0, callback) /* callable */
203238
ZEND_END_ARG_INFO();
204239

205-
ZEND_BEGIN_ARG_INFO_EX(ai_CommandResult_getResponseDocument, 0, 0, 0)
240+
ZEND_BEGIN_ARG_INFO_EX(ai_Result_toArray, 0, 0, 0)
206241
ZEND_END_ARG_INFO();
207242

208-
ZEND_BEGIN_ARG_INFO_EX(ai_CommandResult_getServer, 0, 0, 0)
243+
ZEND_BEGIN_ARG_INFO_EX(ai_Result_getServer, 0, 0, 0)
209244
ZEND_END_ARG_INFO();
210245

211246

212-
static zend_function_entry php_phongo_commandresult_me[] = {
213-
PHP_ME(CommandResult, __construct, ai_CommandResult___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
214-
PHP_ME(CommandResult, getIterator, ai_CommandResult_getIterator, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
215-
PHP_ME(CommandResult, setIteratorClass, ai_CommandResult_setIteratorClass, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
216-
PHP_ME(CommandResult, setIteratorInitCallback, ai_CommandResult_setIteratorInitCallback, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
217-
PHP_ME(CommandResult, getResponseDocument, ai_CommandResult_getResponseDocument, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
218-
PHP_ME(CommandResult, getServer, ai_CommandResult_getServer, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
247+
static zend_function_entry php_phongo_result_me[] = {
248+
PHP_ME(Result, __construct, ai_Result___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
249+
PHP_ME(Result, setTypemap, ai_Result_setTypemap, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
250+
PHP_ME(Result, getIterator, ai_Result_getIterator, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
251+
PHP_ME(Result, setIteratorClass, ai_Result_setIteratorClass, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
252+
PHP_ME(Result, setIteratorInitCallback, ai_Result_setIteratorInitCallback, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
253+
PHP_ME(Result, toArray, ai_Result_toArray, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
254+
PHP_ME(Result, getServer, ai_Result_getServer, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
219255
PHP_FE_END
220256
};
221257

222258
/* }}} */
223259

224260

225-
/* {{{ php_phongo_commandresult_t object handlers */
226-
static void php_phongo_commandresult_free_object(void *object TSRMLS_DC) /* {{{ */
261+
/* {{{ php_phongo_result_t object handlers */
262+
static void php_phongo_result_free_object(void *object TSRMLS_DC) /* {{{ */
227263
{
228-
php_phongo_commandresult_t *intern = (php_phongo_commandresult_t*)object;
264+
php_phongo_result_t *intern = (php_phongo_result_t*)object;
229265

230-
zend_object_std_dtor(&intern->result.std TSRMLS_CC);
266+
zend_object_std_dtor(&intern->std TSRMLS_CC);
231267

232-
php_phongo_result_free(&intern->result);
268+
php_phongo_result_free(intern);
233269

234270
efree(intern);
235271
} /* }}} */
236272

237-
zend_object_value php_phongo_commandresult_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
273+
zend_object_value php_phongo_result_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
238274
{
239275
zend_object_value retval;
240-
php_phongo_commandresult_t *intern = NULL;
276+
php_phongo_result_t *intern = NULL;
241277

242-
intern = (php_phongo_commandresult_t *)ecalloc(1, sizeof *intern);
278+
intern = (php_phongo_result_t *)ecalloc(1, sizeof *intern);
243279

244-
zend_object_std_init(&intern->result.std, class_type TSRMLS_CC);
245-
object_properties_init(&intern->result.std, class_type);
280+
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
281+
object_properties_init(&intern->std, class_type);
246282

247-
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_commandresult_free_object, NULL TSRMLS_CC);
248-
retval.handlers = &php_phongo_handler_commandresult;
283+
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_result_free_object, NULL TSRMLS_CC);
284+
retval.handlers = &php_phongo_handler_result;
249285

250286
return retval;
251287
} /* }}} */
252288

253-
HashTable *php_phongo_commandresult_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
289+
HashTable *php_phongo_result_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
254290
{
255-
php_phongo_commandresult_t *intern;
291+
php_phongo_result_t *intern;
256292
zval retval = zval_used_for_init;
257293

258294

259295
*is_temp = 1;
260-
intern = (php_phongo_commandresult_t *)zend_object_store_get_object(object TSRMLS_CC);
296+
intern = (php_phongo_result_t *)zend_object_store_get_object(object TSRMLS_CC);
261297

262-
php_phongo_result_to_zval(&retval, &intern->result);
298+
php_phongo_result_to_zval(&retval, intern);
263299

264300
return Z_ARRVAL(retval);
265301

266302
} /* }}} */
267303
/* }}} */
268304

269305
/* {{{ PHP_MINIT_FUNCTION */
270-
PHP_MINIT_FUNCTION(CommandResult)
306+
PHP_MINIT_FUNCTION(Result)
271307
{
272308
(void)type; (void)module_number;
273309
zend_class_entry ce;
274310

275-
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "CommandResult", php_phongo_commandresult_me);
276-
php_phongo_commandresult_ce = zend_register_internal_class(&ce TSRMLS_CC);
277-
php_phongo_commandresult_ce->create_object = php_phongo_commandresult_create_object;
278-
php_phongo_commandresult_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
279-
php_phongo_commandresult_ce->get_iterator = phongo_result_get_iterator;
311+
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "Result", php_phongo_result_me);
312+
php_phongo_result_ce = zend_register_internal_class(&ce TSRMLS_CC);
313+
php_phongo_result_ce->create_object = php_phongo_result_create_object;
314+
php_phongo_result_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
315+
php_phongo_result_ce->get_iterator = phongo_result_get_iterator;
280316

281-
memcpy(&php_phongo_handler_commandresult, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
282-
php_phongo_handler_commandresult.get_debug_info = php_phongo_commandresult_get_debug_info;
317+
memcpy(&php_phongo_handler_result, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
318+
php_phongo_handler_result.get_debug_info = php_phongo_result_get_debug_info;
283319

284-
zend_class_implements(php_phongo_commandresult_ce TSRMLS_CC, 1, zend_ce_aggregate);
320+
zend_class_implements(php_phongo_result_ce TSRMLS_CC, 1, zend_ce_aggregate);
285321

286322

287323
return SUCCESS;

0 commit comments

Comments
 (0)