Skip to content

Commit 4e01486

Browse files
committed
PHPC-418: Getters for WriteConcern properties
1 parent 106ee84 commit 4e01486

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

src/MongoDB/WriteConcern.c

Lines changed: 103 additions & 0 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>
@@ -98,6 +99,93 @@ PHP_METHOD(WriteConcern, __construct)
9899
}
99100
/* }}} */
100101

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+
101189
/**
102190
* Value object for write concern used in issuing write operations.
103191
*/
@@ -110,9 +198,24 @@ ZEND_BEGIN_ARG_INFO_EX(ai_WriteConcern___construct, 0, 0, 1)
110198
ZEND_ARG_INFO(0, fsync)
111199
ZEND_END_ARG_INFO();
112200

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();
113212

114213
static zend_function_entry php_phongo_writeconcern_me[] = {
115214
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)
116219
PHP_FE_END
117220
};
118221

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern::getFsync()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
true,
11+
false,
12+
1,
13+
0,
14+
null,
15+
);
16+
17+
foreach ($tests as $test) {
18+
$wc = new MongoDB\Driver\WriteConcern(1, 0, true, $test);
19+
var_dump($wc->getFsync());
20+
}
21+
22+
// Test with default value
23+
$wc = new MongoDB\Driver\WriteConcern(1, 0, true);
24+
var_dump($wc->getFsync());
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECT--
30+
bool(true)
31+
bool(false)
32+
bool(true)
33+
bool(false)
34+
bool(false)
35+
NULL
36+
===DONE===
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern::getJournal()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
true,
11+
false,
12+
1,
13+
0,
14+
null,
15+
);
16+
17+
foreach ($tests as $test) {
18+
$wc = new MongoDB\Driver\WriteConcern(1, 0, $test);
19+
var_dump($wc->getJournal());
20+
}
21+
22+
// Test with default value
23+
$wc = new MongoDB\Driver\WriteConcern(1, 0);
24+
var_dump($wc->getJournal());
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECT--
30+
bool(true)
31+
bool(false)
32+
bool(true)
33+
bool(false)
34+
bool(false)
35+
NULL
36+
===DONE===
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern::getW()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
MongoDB\Driver\WriteConcern::MAJORITY,
11+
-3,
12+
-2,
13+
-1,
14+
0,
15+
1,
16+
2,
17+
'tag',
18+
);
19+
20+
foreach ($tests as $test) {
21+
$wc = new MongoDB\Driver\WriteConcern($test);
22+
var_dump($wc->getW());
23+
}
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECT--
29+
string(8) "majority"
30+
string(8) "majority"
31+
int(-2)
32+
int(-1)
33+
int(0)
34+
int(1)
35+
int(2)
36+
string(3) "tag"
37+
===DONE===
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern::getWtimeout()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
-1,
11+
0,
12+
1,
13+
);
14+
15+
foreach ($tests as $test) {
16+
$wc = new MongoDB\Driver\WriteConcern(1, $test);
17+
var_dump($wc->getWtimeout());
18+
}
19+
20+
// Test with default value
21+
$wc = new MongoDB\Driver\WriteConcern(1);
22+
var_dump($wc->getWtimeout());
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
int(0)
29+
int(0)
30+
int(1)
31+
int(0)
32+
===DONE===

0 commit comments

Comments
 (0)