28
28
#include <bson.h>
29
29
#include <mongoc.h>
30
30
#include <mongoc-cursor-private.h>
31
+ #include <mongoc-read-prefs-private.h>
31
32
32
33
/* PHP Core stuff */
33
34
#include <php.h>
45
46
46
47
PHONGO_API zend_class_entry * php_phongo_cursor_ce ;
47
48
49
+ zend_object_handlers php_phongo_handler_cursor ;
50
+
48
51
/* {{{ proto MongoDB\Driver\Cursor Cursor::__construct(MongoDB\Driver\Server $server, MongoDB\Driver\CursorId $cursorId, array $firstBatch)
49
52
Construct a new Cursor */
50
53
PHP_METHOD (Cursor , __construct )
@@ -54,6 +57,7 @@ PHP_METHOD(Cursor, __construct)
54
57
zval * server ;
55
58
zval * cursorId ;
56
59
zval * firstBatch ;
60
+ (void )return_value ; (void )return_value_ptr ; (void )return_value_used ;
57
61
58
62
59
63
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -73,6 +77,7 @@ PHP_METHOD(Cursor, getId)
73
77
{
74
78
php_phongo_cursor_t * intern ;
75
79
zend_error_handling error_handling ;
80
+ (void )return_value_ptr ; (void )return_value_used ;
76
81
77
82
78
83
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -93,6 +98,7 @@ PHP_METHOD(Cursor, getServer)
93
98
{
94
99
php_phongo_cursor_t * intern ;
95
100
zend_error_handling error_handling ;
101
+ (void )return_value_ptr ; (void )return_value_used ;
96
102
97
103
98
104
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -104,6 +110,8 @@ PHP_METHOD(Cursor, getServer)
104
110
}
105
111
zend_restore_error_handling (& error_handling TSRMLS_CC );
106
112
113
+ /* FIXME: NOT IMPLEMENTED YET */
114
+ RETURN_NULL ();
107
115
}
108
116
/* }}} */
109
117
/* {{{ proto boolean Cursor::isDead()
@@ -112,6 +120,7 @@ PHP_METHOD(Cursor, isDead)
112
120
{
113
121
php_phongo_cursor_t * intern ;
114
122
zend_error_handling error_handling ;
123
+ (void )return_value_ptr ; (void )return_value_used ;
115
124
116
125
117
126
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -133,6 +142,7 @@ PHP_METHOD(Cursor, kill)
133
142
php_phongo_cursor_t * intern ;
134
143
zend_error_handling error_handling ;
135
144
int hint ;
145
+ (void )return_value_ptr ; (void )return_value_used ;
136
146
137
147
138
148
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -146,15 +156,18 @@ PHP_METHOD(Cursor, kill)
146
156
147
157
hint = mongoc_cursor_get_hint (intern -> result -> cursor );
148
158
mongoc_client_kill_cursor (intern -> result -> cursor -> client , mongoc_cursor_get_id (intern -> result -> cursor ));
159
+
160
+ RETURN_NULL ();
149
161
}
150
162
/* }}} */
151
- /* {{{ proto boolean Cursor::setBatchSize(integer $batchSize)
152
- Sets a batch size for the cursor */
163
+ /* {{{ proto integer Cursor::setBatchSize(integer $batchSize)
164
+ Sets a batch size for the cursor, returning the previous size */
153
165
PHP_METHOD (Cursor , setBatchSize )
154
166
{
155
167
php_phongo_cursor_t * intern ;
156
168
zend_error_handling error_handling ;
157
169
long batchSize ;
170
+ (void )return_value_ptr ; (void )return_value_used ;
158
171
159
172
160
173
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -166,6 +179,7 @@ PHP_METHOD(Cursor, setBatchSize)
166
179
}
167
180
zend_restore_error_handling (& error_handling TSRMLS_CC );
168
181
182
+ RETVAL_LONG (mongoc_cursor_get_batch_size (intern -> result -> cursor ));
169
183
mongoc_cursor_set_batch_size (intern -> result -> cursor , batchSize );
170
184
}
171
185
/* }}} */
@@ -175,6 +189,7 @@ PHP_METHOD(Cursor, getBatchSize)
175
189
{
176
190
php_phongo_cursor_t * intern ;
177
191
zend_error_handling error_handling ;
192
+ (void )return_value_ptr ; (void )return_value_used ;
178
193
179
194
180
195
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -197,6 +212,7 @@ PHP_METHOD(Cursor, current)
197
212
php_phongo_cursor_t * intern ;
198
213
zend_error_handling error_handling ;
199
214
zval * * data = NULL ;
215
+ (void )return_value_ptr ; (void )return_value_used ;
200
216
201
217
202
218
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -227,6 +243,7 @@ PHP_METHOD(Cursor, key)
227
243
{
228
244
php_phongo_cursor_t * intern ;
229
245
zend_error_handling error_handling ;
246
+ (void )return_value_ptr ; (void )return_value_used ;
230
247
231
248
232
249
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -258,6 +275,7 @@ PHP_METHOD(Cursor, next)
258
275
{
259
276
php_phongo_cursor_t * intern ;
260
277
zend_error_handling error_handling ;
278
+ (void )return_value ; (void )return_value_ptr ; (void )return_value_used ;
261
279
262
280
263
281
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -285,6 +303,7 @@ PHP_METHOD(Cursor, rewind)
285
303
{
286
304
php_phongo_cursor_t * intern ;
287
305
zend_error_handling error_handling ;
306
+ (void )return_value ; (void )return_value_ptr ; (void )return_value_used ;
288
307
289
308
290
309
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -312,6 +331,7 @@ PHP_METHOD(Cursor, valid)
312
331
{
313
332
php_phongo_cursor_t * intern ;
314
333
zend_error_handling error_handling ;
334
+ (void )return_value_ptr ; (void )return_value_used ;
315
335
316
336
317
337
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
@@ -334,18 +354,6 @@ PHP_METHOD(Cursor, valid)
334
354
}
335
355
/* }}} */
336
356
337
- /**
338
- * Cursor used to iterate through results of an executed Query or Command.
339
- *
340
- * The iteration and internal logic of Query and Command cursors is very
341
- * similar. The cursor ID and first batch of results, originating from either
342
- * the OP_REPLY message or command result document), will be used to construct
343
- * this object.
344
- *
345
- * While this Cursor object must be initialized internally, the class itself may
346
- * be extended to provide custom Cursor behaviors (e.g. return documents as
347
- * BSON, hydrated classes, stdClass objects).
348
- */
349
357
/* {{{ MongoDB\Driver\Cursor */
350
358
351
359
ZEND_BEGIN_ARG_INFO_EX (ai_Cursor___construct , 0 , 0 , 3 )
@@ -425,32 +433,164 @@ static void php_phongo_cursor_free_object(void *object TSRMLS_DC) /* {{{ */
425
433
zend_object_value php_phongo_cursor_create_object (zend_class_entry * class_type TSRMLS_DC ) /* {{{ */
426
434
{
427
435
zend_object_value retval ;
428
- php_phongo_cursor_t * intern ;
436
+ php_phongo_cursor_t * intern = NULL ;
429
437
430
- intern = (php_phongo_cursor_t * )emalloc (sizeof (php_phongo_cursor_t ));
431
- memset (intern , 0 , sizeof (php_phongo_cursor_t ));
438
+ intern = (php_phongo_cursor_t * )ecalloc (1 , sizeof * intern );
432
439
433
440
zend_object_std_init (& intern -> std , class_type TSRMLS_CC );
434
441
object_properties_init (& intern -> std , class_type );
435
442
436
443
retval .handle = zend_objects_store_put (intern , (zend_objects_store_dtor_t ) zend_objects_destroy_object , php_phongo_cursor_free_object , NULL TSRMLS_CC );
437
- retval .handlers = phongo_get_std_object_handlers () ;
444
+ retval .handlers = & php_phongo_handler_cursor ;
438
445
439
446
return retval ;
440
447
} /* }}} */
448
+
449
+ void php_phongo_read_preference_to_zval (zval * retval , mongoc_read_prefs_t * read_prefs ) /* {{{ */
450
+ {
451
+
452
+ array_init_size (retval , 2 );
453
+
454
+ add_assoc_long_ex (retval , ZEND_STRS ("mode" ), read_prefs -> mode );
455
+ if (read_prefs -> tags .len ) {
456
+ php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
457
+
458
+ MAKE_STD_ZVAL (state .zchild );
459
+ bson_to_zval (bson_get_data (& read_prefs -> tags ), read_prefs -> tags .len , & state );
460
+ add_assoc_zval_ex (retval , ZEND_STRS ("tags" ), state .zchild );
461
+ } else {
462
+ add_assoc_null_ex (retval , ZEND_STRS ("tags" ));
463
+ }
464
+ } /* }}} */
465
+
466
+ void php_phongo_result_to_zval (zval * retval , php_phongo_result_t * result ) /* {{{ */
467
+ {
468
+
469
+ array_init_size (retval , 4 );
470
+
471
+ if (result -> cursor ) {
472
+ zval * cursor = NULL ;
473
+
474
+ MAKE_STD_ZVAL (cursor );
475
+ array_init_size (cursor , 16 );
476
+
477
+ add_assoc_long_ex (cursor , ZEND_STRS ("stamp" ), result -> cursor -> stamp );
478
+
479
+ #define _ADD_BOOL (z , field ) add_assoc_bool_ex(z, ZEND_STRS(#field), result->cursor->field)
480
+ _ADD_BOOL (cursor , is_command );
481
+ _ADD_BOOL (cursor , sent );
482
+ _ADD_BOOL (cursor , done );
483
+ _ADD_BOOL (cursor , failed );
484
+ _ADD_BOOL (cursor , end_of_event );
485
+ _ADD_BOOL (cursor , in_exhaust );
486
+ _ADD_BOOL (cursor , redir_primary );
487
+ _ADD_BOOL (cursor , has_fields );
488
+ #undef _ADD_BOOL
489
+
490
+ {
491
+ php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
492
+
493
+ MAKE_STD_ZVAL (state .zchild );
494
+ bson_to_zval (bson_get_data (& result -> cursor -> query ), result -> cursor -> query .len , & state );
495
+ add_assoc_zval_ex (cursor , ZEND_STRS ("query" ), state .zchild );
496
+ }
497
+ {
498
+ php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
499
+
500
+ MAKE_STD_ZVAL (state .zchild );
501
+ bson_to_zval (bson_get_data (& result -> cursor -> fields ), result -> cursor -> fields .len , & state );
502
+ add_assoc_zval_ex (cursor , ZEND_STRS ("fields" ), state .zchild );
503
+ }
504
+ {
505
+ zval * read_preference = NULL ;
506
+
507
+ MAKE_STD_ZVAL (read_preference );
508
+ php_phongo_read_preference_to_zval (read_preference , result -> cursor -> read_prefs );
509
+ add_assoc_zval_ex (cursor , ZEND_STRS ("read_preference" ), read_preference );
510
+ }
511
+
512
+ #define _ADD_INT (z , field ) add_assoc_long_ex(z, ZEND_STRS(#field), result->cursor->field)
513
+ _ADD_INT (cursor , flags );
514
+ _ADD_INT (cursor , skip );
515
+ _ADD_INT (cursor , limit );
516
+ _ADD_INT (cursor , count );
517
+ _ADD_INT (cursor , batch_size );
518
+ #undef _ADD_INT
519
+
520
+ add_assoc_string_ex (cursor , ZEND_STRS ("ns" ), result -> cursor -> ns , 1 );
521
+ {
522
+ php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
523
+
524
+ MAKE_STD_ZVAL (state .zchild );
525
+ bson_to_zval (bson_get_data (result -> cursor -> current ), result -> cursor -> current -> len , & state );
526
+ add_assoc_zval_ex (cursor , ZEND_STRS ("current_doc" ), state .zchild );
527
+ }
528
+ add_assoc_zval_ex (retval , ZEND_STRS ("cursor" ), cursor );
529
+ } else {
530
+ add_assoc_null_ex (retval , ZEND_STRS ("cursor" ));
531
+ }
532
+
533
+ if (result -> firstBatch ) {
534
+ php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER ;
535
+
536
+ MAKE_STD_ZVAL (state .zchild );
537
+ bson_to_zval (bson_get_data (result -> firstBatch ), result -> firstBatch -> len , & state );
538
+ add_assoc_zval_ex (retval , ZEND_STRS ("firstBatch" ), state .zchild );
539
+ } else {
540
+ add_assoc_null_ex (retval , ZEND_STRS ("firstBatch" ));
541
+ }
542
+ add_assoc_long_ex (retval , ZEND_STRS ("hint" ), result -> hint );
543
+ add_assoc_bool_ex (retval , ZEND_STRS ("is_command_cursor" ), result -> is_command_cursor );
544
+
545
+ } /* }}} */
546
+
547
+ HashTable * php_phongo_cursor_get_debug_info (zval * object , int * is_temp TSRMLS_DC ) /* {{{ */
548
+ {
549
+ php_phongo_cursor_t * intern ;
550
+ zval retval = zval_used_for_init ;
551
+
552
+
553
+ * is_temp = 1 ;
554
+ intern = (php_phongo_cursor_t * )zend_object_store_get_object (object TSRMLS_CC );
555
+
556
+ array_init_size (& retval , 2 );
557
+
558
+ if (!intern -> it ) {
559
+ zend_class_entry * ce ;
560
+
561
+ ce = Z_OBJCE_P (object );
562
+ intern -> it = (phongo_cursor_it * )ce -> get_iterator (ce , object , 0 TSRMLS_CC );
563
+ }
564
+ add_assoc_long_ex (& retval , ZEND_STRS ("current_index" ), intern -> it -> current );
565
+
566
+
567
+ if (intern -> result ) {
568
+ zval * result = NULL ;
569
+
570
+ MAKE_STD_ZVAL (result );
571
+ php_phongo_result_to_zval (result , intern -> result );
572
+ add_assoc_zval_ex (& retval , ZEND_STRS ("result" ), result );
573
+ }
574
+
575
+ return Z_ARRVAL (retval );
576
+
577
+ } /* }}} */
441
578
/* }}} */
442
579
443
580
/* {{{ PHP_MINIT_FUNCTION */
444
581
PHP_MINIT_FUNCTION (Cursor )
445
582
{
446
- (void )type ; /* We don't care if we are loaded via dl() or extension= */
583
+ (void )type ; ( void ) module_number ;
447
584
zend_class_entry ce ;
448
585
449
586
INIT_NS_CLASS_ENTRY (ce , "MongoDB\\Driver" , "Cursor" , php_phongo_cursor_me );
450
- ce .create_object = php_phongo_cursor_create_object ;
451
587
php_phongo_cursor_ce = zend_register_internal_class (& ce TSRMLS_CC );
588
+ php_phongo_cursor_ce -> create_object = php_phongo_cursor_create_object ;
452
589
php_phongo_cursor_ce -> get_iterator = phongo_cursor_get_iterator ;
453
590
591
+ memcpy (& php_phongo_handler_cursor , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
592
+ php_phongo_handler_cursor .get_debug_info = php_phongo_cursor_get_debug_info ;
593
+
454
594
zend_class_implements (php_phongo_cursor_ce TSRMLS_CC , 1 , spl_ce_Iterator );
455
595
456
596
0 commit comments