Skip to content

Commit 6283fe2

Browse files
committed
Improve API consistency between WriteConcern and ReadPreference
Also fixes a segfault when no WriteConcern is provided
1 parent a0f2e15 commit 6283fe2

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

php_phongo.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,19 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
812812

813813
/* }}} */
814814

815+
mongoc_write_concern_t* phongo_write_concern_from_zval(zval *zwrite_concern TSRMLS_DC) /* {{{ */
816+
{
817+
if (zwrite_concern) {
818+
php_phongo_writeconcern_t *intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(zwrite_concern TSRMLS_CC);
819+
820+
if (intern) {
821+
return intern->write_concern;
822+
}
823+
}
824+
825+
return NULL;
826+
} /* }}} */
827+
815828
mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preference TSRMLS_DC) /* {{{ */
816829
{
817830
if (zread_preference) {

php_phongo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ int phongo_execute_single_delete(mongoc_client_t *client, c
105105
mongoc_stream_t* phongo_stream_initiator (const mongoc_uri_t *uri, const mongoc_host_list_t *host, void *user_data, bson_error_t *error);
106106
zend_object_iterator* phongo_result_get_iterator (zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC);
107107
zend_object_iterator* phongo_cursor_get_iterator (zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC);
108-
mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *object TSRMLS_DC);
109-
php_phongo_query_t* phongo_query_from_zval(zval *zquery TSRMLS_DC);
108+
mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preference TSRMLS_DC);
109+
mongoc_write_concern_t* phongo_write_concern_from_zval (zval *zwrite_concern TSRMLS_DC);
110+
php_phongo_query_t* phongo_query_from_zval (zval *zquery TSRMLS_DC);
110111

111112

112113
void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS_DC);

src/MongoDB/Manager.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ PHP_METHOD(Manager, executeWriteBatch)
140140
zval *zbatch;
141141
zval *zwrite_concern = NULL;
142142
php_phongo_writebatch_t *batch;
143-
php_phongo_writeconcern_t *write_concern;
144143

145144

146145
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -154,8 +153,7 @@ PHP_METHOD(Manager, executeWriteBatch)
154153

155154

156155
batch = (php_phongo_writebatch_t *)zend_object_store_get_object(zbatch TSRMLS_CC);
157-
write_concern = (php_phongo_writeconcern_t *)zend_object_store_get_object(zwrite_concern TSRMLS_CC);
158-
phongo_execute_write(intern->client, namespace, batch->batch, write_concern->write_concern, 0, return_value, return_value_used TSRMLS_CC);
156+
phongo_execute_write(intern->client, namespace, batch->batch, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), 0, return_value, return_value_used TSRMLS_CC);
159157
}
160158
/* }}} */
161159
/* {{{ proto MongoDB\WriteResult Manager::executeInsert(string $namespace, array|object $document[, MongoDB\WriteConcern $writeConcern = null])
@@ -169,7 +167,6 @@ PHP_METHOD(Manager, executeInsert)
169167
zval *document;
170168
zval *zwrite_concern = NULL;
171169
bson_t *bson;
172-
php_phongo_writeconcern_t *write_concern;
173170

174171

175172
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -182,10 +179,9 @@ PHP_METHOD(Manager, executeInsert)
182179
zend_restore_error_handling(&error_handling TSRMLS_CC);
183180

184181

185-
write_concern = (php_phongo_writeconcern_t *)zend_object_store_get_object(zwrite_concern TSRMLS_CC);
186182
bson = bson_new();
187183
zval_to_bson(document, PHONGO_BSON_NONE, bson, NULL TSRMLS_CC);
188-
phongo_execute_single_insert(intern->client, namespace, bson, write_concern->write_concern, return_value, return_value_used TSRMLS_CC);
184+
phongo_execute_single_insert(intern->client, namespace, bson, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), return_value, return_value_used TSRMLS_CC);
189185
bson_clear(&bson);
190186
}
191187
/* }}} */
@@ -204,7 +200,6 @@ PHP_METHOD(Manager, executeUpdate)
204200
bson_t *query;
205201
bson_t *update;
206202
mongoc_update_flags_t flags = MONGOC_UPDATE_NONE;
207-
php_phongo_writeconcern_t *write_concern;
208203

209204

210205
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -217,7 +212,6 @@ PHP_METHOD(Manager, executeUpdate)
217212
zend_restore_error_handling(&error_handling TSRMLS_CC);
218213

219214

220-
write_concern = (php_phongo_writeconcern_t *)zend_object_store_get_object(zwrite_concern TSRMLS_CC);
221215
query = bson_new();
222216
update = bson_new();
223217
zval_to_bson(zquery, PHONGO_BSON_NONE, query, NULL TSRMLS_CC);
@@ -230,7 +224,7 @@ PHP_METHOD(Manager, executeUpdate)
230224
flags |= MONGOC_UPDATE_MULTI_UPDATE;
231225
}
232226

233-
phongo_execute_single_update(intern->client, namespace, query, update, write_concern->write_concern, flags, return_value, return_value_used TSRMLS_CC);
227+
phongo_execute_single_update(intern->client, namespace, query, update, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), flags, return_value, return_value_used TSRMLS_CC);
234228
bson_clear(&query);
235229
bson_clear(&update);
236230
}
@@ -248,7 +242,6 @@ PHP_METHOD(Manager, executeDelete)
248242
zval *zwrite_concern = NULL;
249243
bson_t *bson;
250244
mongoc_delete_flags_t flags = MONGOC_DELETE_NONE;
251-
php_phongo_writeconcern_t *write_concern;
252245

253246

254247
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -261,13 +254,12 @@ PHP_METHOD(Manager, executeDelete)
261254
zend_restore_error_handling(&error_handling TSRMLS_CC);
262255

263256

264-
write_concern = (php_phongo_writeconcern_t *)zend_object_store_get_object(zwrite_concern TSRMLS_CC);
265257
if (deleteOptions && php_array_fetch_bool(deleteOptions, "limit")) {
266258
flags |= MONGOC_DELETE_SINGLE_REMOVE;
267259
}
268260
bson = bson_new();
269261
zval_to_bson(query, PHONGO_BSON_NONE, bson, NULL TSRMLS_CC);
270-
phongo_execute_single_delete(intern->client, namespace, bson, write_concern->write_concern, flags, return_value, return_value_used TSRMLS_CC);
262+
phongo_execute_single_delete(intern->client, namespace, bson, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), flags, return_value, return_value_used TSRMLS_CC);
271263
bson_clear(&bson);
272264
}
273265
/* }}} */

0 commit comments

Comments
 (0)