42
42
#include "php_phongo.h"
43
43
#include "php_bson.h"
44
44
45
+ #define RETURN_LONG_FROM_BSON_INT32 (iter , bson , key ) \
46
+ if (bson_iter_init_find((iter), (bson), (key)) && BSON_ITER_HOLDS_INT32((iter))) { \
47
+ RETURN_LONG(bson_iter_int32((iter))); \
48
+ }
45
49
46
50
PHONGO_API zend_class_entry * php_phongo_writeresult_ce ;
47
51
@@ -62,9 +66,7 @@ PHP_METHOD(WriteResult, getInsertedCount)
62
66
return ;
63
67
}
64
68
65
- if (bson_iter_init_find (& iter , intern -> reply , "nInserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
66
- RETURN_LONG (bson_iter_int32 (& iter ));
67
- }
69
+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nInserted" );
68
70
}
69
71
/* }}} */
70
72
/* {{{ proto integer WriteResult::getMatchedCount()
@@ -82,9 +84,7 @@ PHP_METHOD(WriteResult, getMatchedCount)
82
84
return ;
83
85
}
84
86
85
- if (bson_iter_init_find (& iter , intern -> reply , "nMatched" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
86
- RETURN_LONG (bson_iter_int32 (& iter ));
87
- }
87
+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nMatched" );
88
88
}
89
89
/* }}} */
90
90
/* {{{ proto integer|null WriteResult::getModifiedCount()
@@ -102,9 +102,7 @@ PHP_METHOD(WriteResult, getModifiedCount)
102
102
return ;
103
103
}
104
104
105
- if (bson_iter_init_find (& iter , intern -> reply , "nModified" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
106
- RETURN_LONG (bson_iter_int32 (& iter ));
107
- }
105
+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nModified" );
108
106
}
109
107
/* }}} */
110
108
/* {{{ proto integer WriteResult::getDeletedCount()
@@ -122,9 +120,7 @@ PHP_METHOD(WriteResult, getDeletedCount)
122
120
return ;
123
121
}
124
122
125
- if (bson_iter_init_find (& iter , intern -> reply , "nRemoved" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
126
- RETURN_LONG (bson_iter_int32 (& iter ));
127
- }
123
+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nRemoved" );
128
124
}
129
125
/* }}} */
130
126
/* {{{ proto integer WriteResult::getUpsertedCount()
@@ -142,9 +138,7 @@ PHP_METHOD(WriteResult, getUpsertedCount)
142
138
return ;
143
139
}
144
140
145
- if (bson_iter_init_find (& iter , intern -> reply , "nUpserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
146
- RETURN_LONG (bson_iter_int32 (& iter ));
147
- }
141
+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nUpserted" );
148
142
}
149
143
/* }}} */
150
144
/* {{{ proto MongoDB\Driver\Server WriteResult::getServer()
@@ -471,35 +465,19 @@ HashTable *php_phongo_writeresult_get_debug_info(zval *object, int *is_temp TSRM
471
465
* is_temp = 1 ;
472
466
array_init_size (& retval , 9 );
473
467
474
- if (bson_iter_init_find (& iter , intern -> reply , "nInserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
475
- ADD_ASSOC_LONG_EX (& retval , "nInserted" , bson_iter_int32 (& iter ));
476
- } else {
477
- ADD_ASSOC_NULL_EX (& retval , "nInserted" );
478
- }
479
-
480
- if (bson_iter_init_find (& iter , intern -> reply , "nMatched" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
481
- ADD_ASSOC_LONG_EX (& retval , "nMatched" , bson_iter_int32 (& iter ));
482
- } else {
483
- ADD_ASSOC_NULL_EX (& retval , "nMatched" );
468
+ #define SCP (field ) \
469
+ if (bson_iter_init_find(&iter, intern->reply, (field)) && BSON_ITER_HOLDS_INT32(&iter)) { \
470
+ ADD_ASSOC_LONG_EX(&retval, (field), bson_iter_int32(&iter)); \
471
+ } else { \
472
+ ADD_ASSOC_NULL_EX(&retval, (field)); \
484
473
}
485
474
486
- if (bson_iter_init_find (& iter , intern -> reply , "nModified" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
487
- ADD_ASSOC_LONG_EX (& retval , "nModified" , bson_iter_int32 (& iter ));
488
- } else {
489
- ADD_ASSOC_NULL_EX (& retval , "nModified" );
490
- }
491
-
492
- if (bson_iter_init_find (& iter , intern -> reply , "nRemoved" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
493
- ADD_ASSOC_LONG_EX (& retval , "nRemoved" , bson_iter_int32 (& iter ));
494
- } else {
495
- ADD_ASSOC_NULL_EX (& retval , "nRemoved" );
496
- }
497
-
498
- if (bson_iter_init_find (& iter , intern -> reply , "nUpserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
499
- ADD_ASSOC_LONG_EX (& retval , "nUpserted" , bson_iter_int32 (& iter ));
500
- } else {
501
- ADD_ASSOC_NULL_EX (& retval , "nUpserted" );
502
- }
475
+ SCP ("nInserted" );
476
+ SCP ("nMatched" );
477
+ SCP ("nModified" );
478
+ SCP ("nRemoved" );
479
+ SCP ("nUpserted" );
480
+ #undef SCP
503
481
504
482
if (bson_iter_init_find (& iter , intern -> reply , "upserted" ) && BSON_ITER_HOLDS_ARRAY (& iter )) {
505
483
uint32_t len ;
0 commit comments