Skip to content

Commit a062f2d

Browse files
committed
Merge pull request #158
2 parents f131403 + 90d438e commit a062f2d

18 files changed

+612
-2
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ if test "$MONGODB" != "no"; then
161161
src/MongoDB/CursorId.c \
162162
src/MongoDB/Manager.c \
163163
src/MongoDB/Query.c \
164+
src/MongoDB/ReadConcern.c \
164165
src/MongoDB/ReadPreference.c \
165166
src/MongoDB/Server.c \
166167
src/MongoDB/BulkWrite.c \

config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (PHP_MONGODB != "no") {
1010
EXTENSION("mongodb", "php_phongo.c");
1111
ADD_SOURCES(configure_module_dirname + "/src", "bson.c", "mongodb");
1212
ADD_SOURCES(configure_module_dirname + "/src/BSON", "Type.c Unserializable.c Serializable.c Persistable.c Binary.c Javascript.c MaxKey.c MinKey.c ObjectID.c Regex.c Timestamp.c UTCDateTime.c", "mongodb");
13-
ADD_SOURCES(configure_module_dirname + "/src/MongoDB", "Command.c Cursor.c CursorId.c Manager.c Query.c ReadPreference.c Server.c BulkWrite.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c", "mongodb");
13+
ADD_SOURCES(configure_module_dirname + "/src/MongoDB", "Command.c Cursor.c CursorId.c Manager.c Query.c ReadConcern.c ReadPreference.c Server.c BulkWrite.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c", "mongodb");
1414
ADD_SOURCES(configure_module_dirname + "/src/MongoDB/Exception", "Exception.c LogicException.c RuntimeException.c UnexpectedValueException.c InvalidArgumentException.c ConnectionException.c AuthenticationException.c SSLConnectionException.c ExecutionTimeoutException.c ConnectionTimeoutException.c WriteException.c BulkWriteException.c", "mongodb");
1515
ADD_SOURCES(configure_module_dirname + "/src/contrib/", "php-ssl.c", "mongodb");
1616
ADD_SOURCES(configure_module_dirname + "/src/libbson/src/yajl", "yajl_version.c yajl.c yajl_encode.c yajl_lex.c yajl_parser.c yajl_buf.c yajl_tree.c yajl_alloc.c yajl_gen.c", "mongodb");

php_phongo.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "mongoc-cursor-cursorid-private.h"
3131
#include "mongoc-read-prefs-private.h"
3232
#include "mongoc-bulk-operation-private.h"
33+
#include "mongoc-read-concern-private.h"
3334
#include "mongoc-write-concern-private.h"
3435
#include "mongoc-uri-private.h"
3536
#include "mongoc-trace.h"
@@ -243,6 +244,17 @@ void phongo_server_init(zval *return_value, mongoc_client_t *client, int server_
243244
}
244245
/* }}} */
245246

247+
void phongo_readconcern_init(zval *return_value, const mongoc_read_concern_t *read_concern TSRMLS_DC) /* {{{ */
248+
{
249+
php_phongo_readconcern_t *intern;
250+
251+
object_init_ex(return_value, php_phongo_readconcern_ce);
252+
253+
intern = (php_phongo_readconcern_t *)zend_object_store_get_object(return_value TSRMLS_CC);
254+
intern->read_concern = mongoc_read_concern_copy(read_concern);
255+
}
256+
/* }}} */
257+
246258
void phongo_readpreference_init(zval *return_value, const mongoc_read_prefs_t *read_prefs TSRMLS_DC) /* {{{ */
247259
{
248260
php_phongo_readpreference_t *intern;
@@ -585,6 +597,10 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
585597
efree(dbname);
586598
efree(collname);
587599

600+
if (query->read_concern) {
601+
mongoc_collection_set_read_concern(collection, query->read_concern);
602+
}
603+
588604
cursor = mongoc_collection_find(collection, query->flags, query->skip, query->limit, query->batch_size, query->query, query->selector, read_preference);
589605
mongoc_collection_destroy(collection);
590606

@@ -1208,6 +1224,19 @@ const mongoc_write_concern_t* phongo_write_concern_from_zval(zval *zwrite_concer
12081224
return NULL;
12091225
} /* }}} */
12101226

1227+
const mongoc_read_concern_t* phongo_read_concern_from_zval(zval *zread_concern TSRMLS_DC) /* {{{ */
1228+
{
1229+
if (zread_concern) {
1230+
php_phongo_readconcern_t *intern = (php_phongo_readconcern_t *)zend_object_store_get_object(zread_concern TSRMLS_CC);
1231+
1232+
if (intern) {
1233+
return intern->read_concern;
1234+
}
1235+
}
1236+
1237+
return NULL;
1238+
} /* }}} */
1239+
12111240
const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preference TSRMLS_DC) /* {{{ */
12121241
{
12131242
if (zread_preference) {
@@ -1296,6 +1325,19 @@ void php_phongo_server_to_zval(zval *retval, const mongoc_server_description_t *
12961325

12971326
} /* }}} */
12981327

1328+
void php_phongo_read_concern_to_zval(zval *retval, const mongoc_read_concern_t *read_concern) /* {{{ */
1329+
{
1330+
const char *level = mongoc_read_concern_get_level(read_concern);
1331+
1332+
array_init_size(retval, 1);
1333+
1334+
if (level) {
1335+
add_assoc_string_ex(retval, ZEND_STRS("level"), (char *)level, 1);
1336+
} else {
1337+
add_assoc_null_ex(retval, ZEND_STRS("level"));
1338+
}
1339+
} /* }}} */
1340+
12991341
void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t *read_prefs) /* {{{ */
13001342
{
13011343

@@ -2189,6 +2231,7 @@ PHP_MINIT_FUNCTION(mongodb)
21892231
PHP_MINIT(CursorId)(INIT_FUNC_ARGS_PASSTHRU);
21902232
PHP_MINIT(Manager)(INIT_FUNC_ARGS_PASSTHRU);
21912233
PHP_MINIT(Query)(INIT_FUNC_ARGS_PASSTHRU);
2234+
PHP_MINIT(ReadConcern)(INIT_FUNC_ARGS_PASSTHRU);
21922235
PHP_MINIT(ReadPreference)(INIT_FUNC_ARGS_PASSTHRU);
21932236
PHP_MINIT(Server)(INIT_FUNC_ARGS_PASSTHRU);
21942237
PHP_MINIT(BulkWrite)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ PHONGO_API zval* phongo_throw_exception(php_phongo_error_domain_t domain TSRMLS_
110110
PHONGO_API zend_object_handlers *phongo_get_std_object_handlers(void);
111111

112112
void phongo_server_init (zval *return_value, mongoc_client_t *client, int server_id TSRMLS_DC);
113+
void phongo_readconcern_init (zval *return_value, const mongoc_read_concern_t *read_concern TSRMLS_DC);
113114
void phongo_readpreference_init (zval *return_value, const mongoc_read_prefs_t *read_prefs TSRMLS_DC);
114115
void phongo_writeconcern_init (zval *return_value, const mongoc_write_concern_t *write_concern TSRMLS_DC);
115116
bool phongo_query_init (php_phongo_query_t *query, bson_t *filter, bson_t *options TSRMLS_DC);
@@ -119,11 +120,13 @@ int phongo_execute_command (mongoc_client_t *client, c
119120
int phongo_execute_query (mongoc_client_t *client, const char *namespace, const php_phongo_query_t *query, const mongoc_read_prefs_t *read_preference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
120121

121122
mongoc_stream_t* phongo_stream_initiator (const mongoc_uri_t *uri, const mongoc_host_list_t *host, void *user_data, bson_error_t *error);
123+
const mongoc_read_concern_t* phongo_read_concern_from_zval (zval *zread_concern TSRMLS_DC);
122124
const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preference TSRMLS_DC);
123125
const mongoc_write_concern_t* phongo_write_concern_from_zval (zval *zwrite_concern TSRMLS_DC);
124126
const php_phongo_query_t* phongo_query_from_zval (zval *zquery TSRMLS_DC);
125127

126128
void php_phongo_server_to_zval(zval *retval, const mongoc_server_description_t *sd);
129+
void php_phongo_read_concern_to_zval(zval *retval, const mongoc_read_concern_t *read_concern);
127130
void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t *read_prefs);
128131
void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t *write_concern);
129132
void php_phongo_cursor_to_zval(zval *retval, php_phongo_cursor_t *cursor);

php_phongo_classes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,14 @@ typedef struct {
6767
uint32_t skip;
6868
uint32_t limit;
6969
uint32_t batch_size;
70+
mongoc_read_concern_t *read_concern;
7071
} php_phongo_query_t;
7172

73+
typedef struct {
74+
zend_object std;
75+
mongoc_read_concern_t *read_concern;
76+
} php_phongo_readconcern_t;
77+
7278
typedef struct {
7379
zend_object std;
7480
mongoc_read_prefs_t *read_preference;
@@ -166,6 +172,7 @@ extern PHONGO_API zend_class_entry *php_phongo_cursor_ce;
166172
extern PHONGO_API zend_class_entry *php_phongo_cursorid_ce;
167173
extern PHONGO_API zend_class_entry *php_phongo_manager_ce;
168174
extern PHONGO_API zend_class_entry *php_phongo_query_ce;
175+
extern PHONGO_API zend_class_entry *php_phongo_readconcern_ce;
169176
extern PHONGO_API zend_class_entry *php_phongo_readpreference_ce;
170177
extern PHONGO_API zend_class_entry *php_phongo_result_ce;
171178
extern PHONGO_API zend_class_entry *php_phongo_server_ce;
@@ -214,6 +221,7 @@ PHP_MINIT_FUNCTION(Cursor);
214221
PHP_MINIT_FUNCTION(CursorId);
215222
PHP_MINIT_FUNCTION(Manager);
216223
PHP_MINIT_FUNCTION(Query);
224+
PHP_MINIT_FUNCTION(ReadConcern);
217225
PHP_MINIT_FUNCTION(ReadPreference);
218226
PHP_MINIT_FUNCTION(Result);
219227
PHP_MINIT_FUNCTION(Server);

src/MongoDB/Manager.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ PHP_METHOD(Manager, executeBulkWrite)
180180
phongo_execute_write(intern->client, namespace, bulk->bulk, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), -1, return_value, return_value_used TSRMLS_CC);
181181
}
182182
/* }}} */
183+
/* {{{ proto MongoDB\Driver\ReadConcern Manager::getReadConcern()
184+
Returns the ReadConcern associated with this Manager */
185+
PHP_METHOD(Manager, getReadConcern)
186+
{
187+
php_phongo_manager_t *intern;
188+
(void)return_value_ptr;
189+
190+
intern = (php_phongo_manager_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
191+
192+
if (zend_parse_parameters_none() == FAILURE) {
193+
return;
194+
}
195+
196+
if (return_value_used) {
197+
phongo_readconcern_init(return_value, mongoc_client_get_read_concern(intern->client) TSRMLS_CC);
198+
}
199+
}
200+
/* }}} */
183201
/* {{{ proto MongoDB\Driver\ReadPreference Manager::getReadPreference()
184202
Returns the ReadPreference associated with this Manager */
185203
PHP_METHOD(Manager, getReadPreference)
@@ -329,6 +347,9 @@ ZEND_BEGIN_ARG_INFO_EX(ai_Manager_executeBulkWrite, 0, 0, 2)
329347
ZEND_ARG_OBJ_INFO(0, writeConcern, MongoDB\\Driver\\WriteConcern, 1)
330348
ZEND_END_ARG_INFO();
331349

350+
ZEND_BEGIN_ARG_INFO_EX(ai_Manager_getReadConcern, 0, 0, 0)
351+
ZEND_END_ARG_INFO();
352+
332353
ZEND_BEGIN_ARG_INFO_EX(ai_Manager_getReadPreference, 0, 0, 0)
333354
ZEND_END_ARG_INFO();
334355

@@ -347,6 +368,7 @@ static zend_function_entry php_phongo_manager_me[] = {
347368
PHP_ME(Manager, executeCommand, ai_Manager_executeCommand, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
348369
PHP_ME(Manager, executeQuery, ai_Manager_executeQuery, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
349370
PHP_ME(Manager, executeBulkWrite, ai_Manager_executeBulkWrite, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
371+
PHP_ME(Manager, getReadConcern, ai_Manager_getReadConcern, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
350372
PHP_ME(Manager, getReadPreference, ai_Manager_getReadPreference, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
351373
PHP_ME(Manager, getServers, ai_Manager_getServers, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
352374
PHP_ME(Manager, getWriteConcern, ai_Manager_getWriteConcern, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)

src/MongoDB/Query.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include <ext/standard/info.h>
3535
#include <Zend/zend_interfaces.h>
3636
#include <ext/spl/spl_iterators.h>
37+
38+
/* PHP array helpers */
39+
#include "php_array_api.h"
40+
3741
/* Our Compatability header */
3842
#include "phongo_compat.h"
3943

@@ -75,8 +79,22 @@ PHP_METHOD(Query, __construct)
7579
zval_to_bson(zfilter, PHONGO_BSON_NONE, &bfilter, NULL TSRMLS_CC);
7680

7781
if (zoptions) {
82+
if (php_array_exists(zoptions, "readConcern")) {
83+
zval *zread_concern = php_array_fetchc(zoptions, "readConcern");
84+
85+
if (Z_TYPE_P(zread_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(zread_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
86+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", php_phongo_readconcern_ce->name, zend_get_type_by_const(Z_TYPE_P(zread_concern)));
87+
bson_clear(&intern->query);
88+
return;
89+
}
90+
91+
intern->read_concern = mongoc_read_concern_copy(phongo_read_concern_from_zval(zread_concern TSRMLS_CC));
92+
php_array_unsetc(zoptions, "readConcern");
93+
}
94+
7895
zval_to_bson(zoptions, PHONGO_BSON_NONE, &boptions, NULL TSRMLS_CC);
7996
}
97+
8098
if (!phongo_query_init(intern, &bfilter, &boptions TSRMLS_CC)) {
8199
bson_clear(&intern->query);
82100
}
@@ -117,6 +135,10 @@ static void php_phongo_query_free_object(void *object TSRMLS_DC) /* {{{ */
117135
bson_clear(&intern->query);
118136
}
119137

138+
if (intern->read_concern) {
139+
mongoc_read_concern_destroy(intern->read_concern);
140+
}
141+
120142
efree(intern);
121143
} /* }}} */
122144

0 commit comments

Comments
 (0)