Skip to content

Commit 98a0ba0

Browse files
committed
Merge pull request #108
2 parents 0c059c6 + 4e01486 commit 98a0ba0

18 files changed

+470
-77
lines changed

php_phongo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t
14231423
if (wtag) {
14241424
add_assoc_string_ex(retval, ZEND_STRS("w"), (char *)wtag, 1);
14251425
} else if (mongoc_write_concern_get_wmajority(write_concern)) {
1426-
add_assoc_string_ex(retval, ZEND_STRS("w"), (char *)"majority", 1);
1426+
add_assoc_string_ex(retval, ZEND_STRS("w"), (char *)PHONGO_WRITE_CONCERN_W_MAJORITY, 1);
14271427
} else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) {
14281428
add_assoc_long_ex(retval, ZEND_STRS("w"), w);
14291429
}
@@ -1778,7 +1778,7 @@ bool php_phongo_apply_wc_options_to_client(mongoc_client_t *client, bson_t *opti
17781778
} else if (BSON_ITER_HOLDS_UTF8(&iter)) {
17791779
const char *str = bson_iter_utf8(&iter, NULL);
17801780

1781-
if (0 == strcasecmp("majority", str)) {
1781+
if (0 == strcasecmp(PHONGO_WRITE_CONCERN_W_MAJORITY, str)) {
17821782
mongoc_write_concern_set_wmajority(new_wc, wtimeoutms);
17831783
} else {
17841784
mongoc_write_concern_set_wtag(new_wc, str);

php_phongo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ ZEND_END_MODULE_GLOBALS(mongodb)
6464
# define mglo mongodb_globals
6565
#endif
6666

67-
#include "php_phongo_classes.h"
67+
#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"
6868

69+
#include "php_phongo_classes.h"
6970

7071
typedef enum {
7172
PHONGO_ERROR_INVALID_ARGUMENT = 1,

src/MongoDB/ReadPreference.c

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,28 @@ PHP_METHOD(ReadPreference, __construct)
5353
{
5454
php_phongo_readpreference_t *intern;
5555
zend_error_handling error_handling;
56-
long readPreference;
56+
long mode;
5757
zval *tagSets = NULL;
5858
(void)return_value_ptr; (void)return_value; (void)return_value_used;
5959

6060

6161
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
6262
intern = (php_phongo_readpreference_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
6363

64-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|a!", &readPreference, &tagSets) == FAILURE) {
64+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|a!", &mode, &tagSets) == FAILURE) {
6565
zend_restore_error_handling(&error_handling TSRMLS_CC);
6666
return;
6767
}
6868
zend_restore_error_handling(&error_handling TSRMLS_CC);
6969

7070

71-
switch(readPreference) {
71+
switch(mode) {
7272
case MONGOC_READ_PRIMARY:
7373
case MONGOC_READ_SECONDARY:
7474
case MONGOC_READ_PRIMARY_PREFERRED:
7575
case MONGOC_READ_SECONDARY_PREFERRED:
7676
case MONGOC_READ_NEAREST:
77-
intern->read_preference = mongoc_read_prefs_new(readPreference);
77+
intern->read_preference = mongoc_read_prefs_new(mode);
7878

7979
if (tagSets) {
8080
bson_t *tags = bson_new();
@@ -83,31 +83,83 @@ PHP_METHOD(ReadPreference, __construct)
8383
mongoc_read_prefs_set_tags(intern->read_preference, tags);
8484
bson_destroy(tags);
8585
if (!mongoc_read_prefs_is_valid(intern->read_preference)) {
86-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid tagSet");
86+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Invalid tagSets");
8787
return;
8888
}
8989
}
9090
break;
9191
default:
92-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid ReadPreference");
92+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Invalid mode: %ld", mode);
9393
return;
9494
}
9595
}
9696
/* }}} */
9797

98+
/* {{{ proto string ReadPreference::getMode()
99+
Returns the ReadPreference mode */
100+
PHP_METHOD(ReadPreference, getMode)
101+
{
102+
php_phongo_readpreference_t *intern;
103+
(void)return_value_ptr; (void)return_value_used;
104+
105+
intern = (php_phongo_readpreference_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
106+
107+
if (zend_parse_parameters_none() == FAILURE) {
108+
return;
109+
}
110+
111+
RETURN_LONG(mongoc_read_prefs_get_mode(intern->read_preference));
112+
}
113+
/* }}} */
114+
115+
/* {{{ proto array ReadPreference::getTagSets()
116+
Returns the ReadPreference tag sets */
117+
PHP_METHOD(ReadPreference, getTagSets)
118+
{
119+
php_phongo_readpreference_t *intern;
120+
(void)return_value_ptr; (void)return_value_used;
121+
122+
intern = (php_phongo_readpreference_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
123+
124+
if (zend_parse_parameters_none() == FAILURE) {
125+
return;
126+
}
127+
128+
if (intern->read_preference->tags.len) {
129+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
130+
/* Use native arrays for debugging output */
131+
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
132+
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
133+
134+
MAKE_STD_ZVAL(state.zchild);
135+
bson_to_zval(bson_get_data(&intern->read_preference->tags), intern->read_preference->tags.len, &state);
136+
RETURN_ZVAL(state.zchild, 0, 1);
137+
} else {
138+
RETURN_NULL();
139+
}
140+
}
141+
/* }}} */
142+
98143
/**
99144
* Value object for read preferences used in issuing commands and queries.
100145
*/
101146
/* {{{ MongoDB\Driver\ReadPreference */
102147

103148
ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference___construct, 0, 0, 1)
104-
ZEND_ARG_INFO(0, readPreference)
149+
ZEND_ARG_INFO(0, mode)
105150
ZEND_ARG_ARRAY_INFO(0, tagSets, 1)
106151
ZEND_END_ARG_INFO();
107152

153+
ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference_getMode, 0, 0, 0)
154+
ZEND_END_ARG_INFO();
155+
156+
ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference_getTagSets, 0, 0, 0)
157+
ZEND_END_ARG_INFO();
108158

109159
static zend_function_entry php_phongo_readpreference_me[] = {
110160
PHP_ME(ReadPreference, __construct, ai_ReadPreference___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
161+
PHP_ME(ReadPreference, getMode, ai_ReadPreference_getMode, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
162+
PHP_ME(ReadPreference, getTagSets, ai_ReadPreference_getTagSets, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
111163
PHP_FE_END
112164
};
113165

src/MongoDB/WriteConcern.c

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/* External libs */
2828
#include <bson.h>
2929
#include <mongoc.h>
30+
#include "mongoc-write-concern-private.h"
3031

3132
/* PHP Core stuff */
3233
#include <php.h>
@@ -46,8 +47,6 @@ PHONGO_API zend_class_entry *php_phongo_writeconcern_ce;
4647

4748
zend_object_handlers php_phongo_handler_writeconcern;
4849

49-
#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"
50-
5150
/* {{{ proto MongoDB\Driver\WriteConcern WriteConcern::__construct(integer|string $w[, integer $wtimeout[, boolean $journal[, boolean $fsync]]])
5251
Constructs a new WriteConcern */
5352
PHP_METHOD(WriteConcern, __construct)
@@ -100,6 +99,93 @@ PHP_METHOD(WriteConcern, __construct)
10099
}
101100
/* }}} */
102101

102+
/* {{{ proto string|integer WriteConcern::getW()
103+
Returns the WriteConcern "w" option */
104+
PHP_METHOD(WriteConcern, getW)
105+
{
106+
php_phongo_writeconcern_t *intern;
107+
const char *wtag;
108+
(void)return_value_ptr; (void)return_value_used;
109+
110+
intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
111+
112+
if (zend_parse_parameters_none() == FAILURE) {
113+
return;
114+
}
115+
116+
wtag = mongoc_write_concern_get_wtag(intern->write_concern);
117+
118+
if (wtag) {
119+
RETURN_STRING(wtag, 1);
120+
}
121+
122+
if (mongoc_write_concern_get_wmajority(intern->write_concern)) {
123+
RETURN_STRING(PHONGO_WRITE_CONCERN_W_MAJORITY, 1);
124+
}
125+
126+
RETURN_LONG(intern->write_concern->w);
127+
}
128+
/* }}} */
129+
130+
/* {{{ proto string|integer WriteConcern::getWtimeout()
131+
Returns the WriteConcern "wtimeout" option */
132+
PHP_METHOD(WriteConcern, getWtimeout)
133+
{
134+
php_phongo_writeconcern_t *intern;
135+
(void)return_value_ptr; (void)return_value_used;
136+
137+
intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
138+
139+
if (zend_parse_parameters_none() == FAILURE) {
140+
return;
141+
}
142+
143+
RETURN_LONG(mongoc_write_concern_get_wtimeout(intern->write_concern));
144+
}
145+
/* }}} */
146+
147+
/* {{{ proto null|boolean WriteConcern::getJournal()
148+
Returns the WriteConcern "journal" option */
149+
PHP_METHOD(WriteConcern, getJournal)
150+
{
151+
php_phongo_writeconcern_t *intern;
152+
(void)return_value_ptr; (void)return_value_used;
153+
154+
intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
155+
156+
if (zend_parse_parameters_none() == FAILURE) {
157+
return;
158+
}
159+
160+
if (intern->write_concern->journal != MONGOC_WRITE_CONCERN_JOURNAL_DEFAULT) {
161+
RETURN_BOOL(mongoc_write_concern_get_journal(intern->write_concern));
162+
}
163+
164+
RETURN_NULL();
165+
}
166+
/* }}} */
167+
168+
/* {{{ proto null|boolean WriteConcern::getFsync()
169+
Returns the WriteConcern "fsync" option */
170+
PHP_METHOD(WriteConcern, getFsync)
171+
{
172+
php_phongo_writeconcern_t *intern;
173+
(void)return_value_ptr; (void)return_value_used;
174+
175+
intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
176+
177+
if (zend_parse_parameters_none() == FAILURE) {
178+
return;
179+
}
180+
181+
if (intern->write_concern->fsync_ != MONGOC_WRITE_CONCERN_FSYNC_DEFAULT) {
182+
RETURN_BOOL(mongoc_write_concern_get_fsync(intern->write_concern));
183+
}
184+
185+
RETURN_NULL();
186+
}
187+
/* }}} */
188+
103189
/**
104190
* Value object for write concern used in issuing write operations.
105191
*/
@@ -112,9 +198,24 @@ ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern___construct, 0, 0, 1)
112198
ZEND_ARG_INFO(0, fsync)
113199
ZEND_END_ARG_INFO();
114200

201+
ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getW, 0, 0, 0)
202+
ZEND_END_ARG_INFO();
203+
204+
ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getWtimeout, 0, 0, 0)
205+
ZEND_END_ARG_INFO();
206+
207+
ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getJournal, 0, 0, 0)
208+
ZEND_END_ARG_INFO();
209+
210+
ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getFsync, 0, 0, 0)
211+
ZEND_END_ARG_INFO();
115212

116213
static zend_function_entry php_phongo_writeconcern_me[] = {
117214
PHP_ME(WriteConcern, __construct, ai_WriteConcern___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
215+
PHP_ME(WriteConcern, getW, ai_WriteConcern_getW, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
216+
PHP_ME(WriteConcern, getWtimeout, ai_WriteConcern_getWtimeout, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
217+
PHP_ME(WriteConcern, getJournal, ai_WriteConcern_getJournal, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
218+
PHP_ME(WriteConcern, getFsync, ai_WriteConcern_getFsync, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
118219
PHP_FE_END
119220
};
120221

tests/readPreference/001.phpt

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)