Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t
if (wtag) {
add_assoc_string_ex(retval, ZEND_STRS("w"), (char *)wtag, 1);
} else if (mongoc_write_concern_get_wmajority(write_concern)) {
add_assoc_string_ex(retval, ZEND_STRS("w"), (char *)"majority", 1);
add_assoc_string_ex(retval, ZEND_STRS("w"), (char *)PHONGO_WRITE_CONCERN_W_MAJORITY, 1);
} else if (w != MONGOC_WRITE_CONCERN_W_DEFAULT) {
add_assoc_long_ex(retval, ZEND_STRS("w"), w);
}
Expand Down Expand Up @@ -1778,7 +1778,7 @@ bool php_phongo_apply_wc_options_to_client(mongoc_client_t *client, bson_t *opti
} else if (BSON_ITER_HOLDS_UTF8(&iter)) {
const char *str = bson_iter_utf8(&iter, NULL);

if (0 == strcasecmp("majority", str)) {
if (0 == strcasecmp(PHONGO_WRITE_CONCERN_W_MAJORITY, str)) {
mongoc_write_concern_set_wmajority(new_wc, wtimeoutms);
} else {
mongoc_write_concern_set_wtag(new_wc, str);
Expand Down
3 changes: 2 additions & 1 deletion php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ ZEND_END_MODULE_GLOBALS(mongodb)
# define mglo mongodb_globals
#endif

#include "php_phongo_classes.h"
#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"

#include "php_phongo_classes.h"

typedef enum {
PHONGO_ERROR_INVALID_ARGUMENT = 1,
Expand Down
66 changes: 59 additions & 7 deletions src/MongoDB/ReadPreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ PHP_METHOD(ReadPreference, __construct)
{
php_phongo_readpreference_t *intern;
zend_error_handling error_handling;
long readPreference;
long mode;
zval *tagSets = NULL;
(void)return_value_ptr; (void)return_value; (void)return_value_used;


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

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|a!", &readPreference, &tagSets) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|a!", &mode, &tagSets) == FAILURE) {
zend_restore_error_handling(&error_handling TSRMLS_CC);
return;
}
zend_restore_error_handling(&error_handling TSRMLS_CC);


switch(readPreference) {
switch(mode) {
case MONGOC_READ_PRIMARY:
case MONGOC_READ_SECONDARY:
case MONGOC_READ_PRIMARY_PREFERRED:
case MONGOC_READ_SECONDARY_PREFERRED:
case MONGOC_READ_NEAREST:
intern->read_preference = mongoc_read_prefs_new(readPreference);
intern->read_preference = mongoc_read_prefs_new(mode);

if (tagSets) {
bson_t *tags = bson_new();
Expand All @@ -83,31 +83,83 @@ PHP_METHOD(ReadPreference, __construct)
mongoc_read_prefs_set_tags(intern->read_preference, tags);
bson_destroy(tags);
if (!mongoc_read_prefs_is_valid(intern->read_preference)) {
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid tagSet");
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Invalid tagSets");
return;
}
}
break;
default:
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid ReadPreference");
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Invalid mode: %ld", mode);
return;
}
}
/* }}} */

/* {{{ proto string ReadPreference::getMode()
Returns the ReadPreference mode */
PHP_METHOD(ReadPreference, getMode)
{
php_phongo_readpreference_t *intern;
(void)return_value_ptr; (void)return_value_used;

intern = (php_phongo_readpreference_t *)zend_object_store_get_object(getThis() TSRMLS_CC);

if (zend_parse_parameters_none() == FAILURE) {
return;
}

RETURN_LONG(mongoc_read_prefs_get_mode(intern->read_preference));
}
/* }}} */

/* {{{ proto array ReadPreference::getTagSets()
Returns the ReadPreference tag sets */
PHP_METHOD(ReadPreference, getTagSets)
{
php_phongo_readpreference_t *intern;
(void)return_value_ptr; (void)return_value_used;

intern = (php_phongo_readpreference_t *)zend_object_store_get_object(getThis() TSRMLS_CC);

if (zend_parse_parameters_none() == FAILURE) {
return;
}

if (intern->read_preference->tags.len) {
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
/* Use native arrays for debugging output */
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY;

MAKE_STD_ZVAL(state.zchild);
bson_to_zval(bson_get_data(&intern->read_preference->tags), intern->read_preference->tags.len, &state);
RETURN_ZVAL(state.zchild, 0, 1);
} else {
RETURN_NULL();
}
}
/* }}} */

/**
* Value object for read preferences used in issuing commands and queries.
*/
/* {{{ MongoDB\Driver\ReadPreference */

ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference___construct, 0, 0, 1)
ZEND_ARG_INFO(0, readPreference)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_ARRAY_INFO(0, tagSets, 1)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference_getMode, 0, 0, 0)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(ai_ReadPreference_getTagSets, 0, 0, 0)
ZEND_END_ARG_INFO();

static zend_function_entry php_phongo_readpreference_me[] = {
PHP_ME(ReadPreference, __construct, ai_ReadPreference___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(ReadPreference, getMode, ai_ReadPreference_getMode, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(ReadPreference, getTagSets, ai_ReadPreference_getTagSets, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_FE_END
};

Expand Down
105 changes: 103 additions & 2 deletions src/MongoDB/WriteConcern.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/* External libs */
#include <bson.h>
#include <mongoc.h>
#include "mongoc-write-concern-private.h"

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

zend_object_handlers php_phongo_handler_writeconcern;

#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"

/* {{{ proto MongoDB\Driver\WriteConcern WriteConcern::__construct(integer|string $w[, integer $wtimeout[, boolean $journal[, boolean $fsync]]])
Constructs a new WriteConcern */
PHP_METHOD(WriteConcern, __construct)
Expand Down Expand Up @@ -100,6 +99,93 @@ PHP_METHOD(WriteConcern, __construct)
}
/* }}} */

/* {{{ proto string|integer WriteConcern::getW()
Returns the WriteConcern "w" option */
PHP_METHOD(WriteConcern, getW)
{
php_phongo_writeconcern_t *intern;
const char *wtag;
(void)return_value_ptr; (void)return_value_used;

intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);

if (zend_parse_parameters_none() == FAILURE) {
return;
}

wtag = mongoc_write_concern_get_wtag(intern->write_concern);

if (wtag) {
RETURN_STRING(wtag, 1);
}

if (mongoc_write_concern_get_wmajority(intern->write_concern)) {
RETURN_STRING(PHONGO_WRITE_CONCERN_W_MAJORITY, 1);
}

RETURN_LONG(intern->write_concern->w);
}
/* }}} */

/* {{{ proto string|integer WriteConcern::getWtimeout()
Returns the WriteConcern "wtimeout" option */
PHP_METHOD(WriteConcern, getWtimeout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't that be getWTimeout ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://docs.mongodb.org/master/core/write-concern/ referred to it simply as wtimeout, and our constructor argument is also just $wtimeout, so I wasn't inclined to capitalize it.

For what it's worth, I also think getW() looks really weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

{
php_phongo_writeconcern_t *intern;
(void)return_value_ptr; (void)return_value_used;

intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);

if (zend_parse_parameters_none() == FAILURE) {
return;
}

RETURN_LONG(mongoc_write_concern_get_wtimeout(intern->write_concern));
}
/* }}} */

/* {{{ proto null|boolean WriteConcern::getJournal()
Returns the WriteConcern "journal" option */
PHP_METHOD(WriteConcern, getJournal)
{
php_phongo_writeconcern_t *intern;
(void)return_value_ptr; (void)return_value_used;

intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);

if (zend_parse_parameters_none() == FAILURE) {
return;
}

if (intern->write_concern->journal != MONGOC_WRITE_CONCERN_JOURNAL_DEFAULT) {
RETURN_BOOL(mongoc_write_concern_get_journal(intern->write_concern));
}

RETURN_NULL();
}
/* }}} */

/* {{{ proto null|boolean WriteConcern::getFsync()
Returns the WriteConcern "fsync" option */
PHP_METHOD(WriteConcern, getFsync)
{
php_phongo_writeconcern_t *intern;
(void)return_value_ptr; (void)return_value_used;

intern = (php_phongo_writeconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);

if (zend_parse_parameters_none() == FAILURE) {
return;
}

if (intern->write_concern->fsync_ != MONGOC_WRITE_CONCERN_FSYNC_DEFAULT) {
RETURN_BOOL(mongoc_write_concern_get_fsync(intern->write_concern));
}

RETURN_NULL();
}
/* }}} */

/**
* Value object for write concern used in issuing write operations.
*/
Expand All @@ -112,9 +198,24 @@ ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern___construct, 0, 0, 1)
ZEND_ARG_INFO(0, fsync)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getW, 0, 0, 0)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getWtimeout, 0, 0, 0)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getJournal, 0, 0, 0)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern_getFsync, 0, 0, 0)
ZEND_END_ARG_INFO();

static zend_function_entry php_phongo_writeconcern_me[] = {
PHP_ME(WriteConcern, __construct, ai_WriteConcern___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(WriteConcern, getW, ai_WriteConcern_getW, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(WriteConcern, getWtimeout, ai_WriteConcern_getWtimeout, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(WriteConcern, getJournal, ai_WriteConcern_getJournal, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(WriteConcern, getFsync, ai_WriteConcern_getFsync, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_FE_END
};

Expand Down
58 changes: 0 additions & 58 deletions tests/readPreference/001.phpt

This file was deleted.

Loading