Skip to content

Commit 92348ca

Browse files
committed
PHPC-448: Implement ReadConcern class
1 parent 1b2791c commit 92348ca

11 files changed

+383
-1
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: 39 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;
@@ -1208,6 +1220,19 @@ const mongoc_write_concern_t* phongo_write_concern_from_zval(zval *zwrite_concer
12081220
return NULL;
12091221
} /* }}} */
12101222

1223+
const mongoc_read_concern_t* phongo_read_concern_from_zval(zval *zread_concern TSRMLS_DC) /* {{{ */
1224+
{
1225+
if (zread_concern) {
1226+
php_phongo_readconcern_t *intern = (php_phongo_readconcern_t *)zend_object_store_get_object(zread_concern TSRMLS_CC);
1227+
1228+
if (intern) {
1229+
return intern->read_concern;
1230+
}
1231+
}
1232+
1233+
return NULL;
1234+
} /* }}} */
1235+
12111236
const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_preference TSRMLS_DC) /* {{{ */
12121237
{
12131238
if (zread_preference) {
@@ -1296,6 +1321,19 @@ void php_phongo_server_to_zval(zval *retval, const mongoc_server_description_t *
12961321

12971322
} /* }}} */
12981323

1324+
void php_phongo_read_concern_to_zval(zval *retval, const mongoc_read_concern_t *read_concern) /* {{{ */
1325+
{
1326+
const char *level = mongoc_read_concern_get_level(read_concern);
1327+
1328+
array_init_size(retval, 1);
1329+
1330+
if (level) {
1331+
add_assoc_string_ex(retval, ZEND_STRS("level"), (char *)level, 1);
1332+
} else {
1333+
add_assoc_null_ex(retval, ZEND_STRS("level"));
1334+
}
1335+
} /* }}} */
1336+
12991337
void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t *read_prefs) /* {{{ */
13001338
{
13011339

@@ -2189,6 +2227,7 @@ PHP_MINIT_FUNCTION(mongodb)
21892227
PHP_MINIT(CursorId)(INIT_FUNC_ARGS_PASSTHRU);
21902228
PHP_MINIT(Manager)(INIT_FUNC_ARGS_PASSTHRU);
21912229
PHP_MINIT(Query)(INIT_FUNC_ARGS_PASSTHRU);
2230+
PHP_MINIT(ReadConcern)(INIT_FUNC_ARGS_PASSTHRU);
21922231
PHP_MINIT(ReadPreference)(INIT_FUNC_ARGS_PASSTHRU);
21932232
PHP_MINIT(Server)(INIT_FUNC_ARGS_PASSTHRU);
21942233
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ typedef struct {
6969
uint32_t batch_size;
7070
} php_phongo_query_t;
7171

72+
typedef struct {
73+
zend_object std;
74+
mongoc_read_concern_t *read_concern;
75+
} php_phongo_readconcern_t;
76+
7277
typedef struct {
7378
zend_object std;
7479
mongoc_read_prefs_t *read_preference;
@@ -166,6 +171,7 @@ extern PHONGO_API zend_class_entry *php_phongo_cursor_ce;
166171
extern PHONGO_API zend_class_entry *php_phongo_cursorid_ce;
167172
extern PHONGO_API zend_class_entry *php_phongo_manager_ce;
168173
extern PHONGO_API zend_class_entry *php_phongo_query_ce;
174+
extern PHONGO_API zend_class_entry *php_phongo_readconcern_ce;
169175
extern PHONGO_API zend_class_entry *php_phongo_readpreference_ce;
170176
extern PHONGO_API zend_class_entry *php_phongo_result_ce;
171177
extern PHONGO_API zend_class_entry *php_phongo_server_ce;
@@ -214,6 +220,7 @@ PHP_MINIT_FUNCTION(Cursor);
214220
PHP_MINIT_FUNCTION(CursorId);
215221
PHP_MINIT_FUNCTION(Manager);
216222
PHP_MINIT_FUNCTION(Query);
223+
PHP_MINIT_FUNCTION(ReadConcern);
217224
PHP_MINIT_FUNCTION(ReadPreference);
218225
PHP_MINIT_FUNCTION(Result);
219226
PHP_MINIT_FUNCTION(Server);

src/MongoDB/ReadConcern.c

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
+---------------------------------------------------------------------------+
3+
| PHP Driver for MongoDB |
4+
+---------------------------------------------------------------------------+
5+
| Copyright 2013-2015 MongoDB, Inc. |
6+
| |
7+
| Licensed under the Apache License, Version 2.0 (the "License"); |
8+
| you may not use this file except in compliance with the License. |
9+
| You may obtain a copy of the License at |
10+
| |
11+
| http://www.apache.org/licenses/LICENSE-2.0 |
12+
| |
13+
| Unless required by applicable law or agreed to in writing, software |
14+
| distributed under the License is distributed on an "AS IS" BASIS, |
15+
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16+
| See the License for the specific language governing permissions and |
17+
| limitations under the License. |
18+
+---------------------------------------------------------------------------+
19+
| Copyright (c) 2014-2015 MongoDB, Inc. |
20+
+---------------------------------------------------------------------------+
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
# include "config.h"
25+
#endif
26+
27+
/* External libs */
28+
#include <bson.h>
29+
#include <mongoc.h>
30+
#include <mongoc-read-concern-private.h>
31+
32+
/* PHP Core stuff */
33+
#include <php.h>
34+
#include <php_ini.h>
35+
#include <ext/standard/info.h>
36+
#include <Zend/zend_interfaces.h>
37+
#include <ext/spl/spl_iterators.h>
38+
/* Our Compatability header */
39+
#include "phongo_compat.h"
40+
41+
/* Our stuffz */
42+
#include "php_phongo.h"
43+
#include "php_bson.h"
44+
45+
46+
PHONGO_API zend_class_entry *php_phongo_readconcern_ce;
47+
48+
zend_object_handlers php_phongo_handler_readconcern;
49+
50+
/* {{{ proto MongoDB\Driver\ReadConcern ReadConcern::__construct([string $level])
51+
Constructs a new ReadConcern */
52+
PHP_METHOD(ReadConcern, __construct)
53+
{
54+
php_phongo_readconcern_t *intern;
55+
zend_error_handling error_handling;
56+
char *level = NULL;
57+
int level_len = 0;
58+
59+
60+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
61+
62+
63+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
64+
intern = (php_phongo_readconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
65+
66+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &level, &level_len) == FAILURE) {
67+
zend_restore_error_handling(&error_handling TSRMLS_CC);
68+
return;
69+
}
70+
zend_restore_error_handling(&error_handling TSRMLS_CC);
71+
72+
73+
intern->read_concern = mongoc_read_concern_new();
74+
75+
if (level) {
76+
mongoc_read_concern_set_level(intern->read_concern, level);
77+
}
78+
}
79+
/* }}} */
80+
81+
/* {{{ proto string|null ReadConcern::getLevel()
82+
Returns the ReadConcern "level" option */
83+
PHP_METHOD(ReadConcern, getLevel)
84+
{
85+
php_phongo_readconcern_t *intern;
86+
const char *level;
87+
(void)return_value_ptr; (void)return_value_used;
88+
89+
intern = (php_phongo_readconcern_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
90+
91+
if (zend_parse_parameters_none() == FAILURE) {
92+
return;
93+
}
94+
95+
level = mongoc_read_concern_get_level(intern->read_concern);
96+
97+
if (level) {
98+
RETURN_STRING(level, 1);
99+
}
100+
101+
RETURN_NULL();
102+
}
103+
/* }}} */
104+
105+
/**
106+
* Value object for read concern used in issuing read operations.
107+
*/
108+
/* {{{ MongoDB\Driver\ReadConcern */
109+
110+
ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern___construct, 0, 0, 1)
111+
ZEND_ARG_INFO(0, level)
112+
ZEND_END_ARG_INFO();
113+
114+
ZEND_BEGIN_ARG_INFO_EX(ai_ReadConcern_getLevel, 0, 0, 0)
115+
ZEND_END_ARG_INFO();
116+
117+
static zend_function_entry php_phongo_readconcern_me[] = {
118+
PHP_ME(ReadConcern, __construct, ai_ReadConcern___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
119+
PHP_ME(ReadConcern, getLevel, ai_ReadConcern_getLevel, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
120+
PHP_FE_END
121+
};
122+
123+
/* }}} */
124+
125+
126+
/* {{{ php_phongo_readconcern_t object handlers */
127+
static void php_phongo_readconcern_free_object(void *object TSRMLS_DC) /* {{{ */
128+
{
129+
php_phongo_readconcern_t *intern = (php_phongo_readconcern_t*)object;
130+
131+
zend_object_std_dtor(&intern->std TSRMLS_CC);
132+
133+
if (intern->read_concern) {
134+
mongoc_read_concern_destroy(intern->read_concern);
135+
}
136+
efree(intern);
137+
} /* }}} */
138+
139+
zend_object_value php_phongo_readconcern_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
140+
{
141+
zend_object_value retval;
142+
php_phongo_readconcern_t *intern = NULL;
143+
144+
intern = (php_phongo_readconcern_t *)ecalloc(1, sizeof *intern);
145+
146+
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
147+
object_properties_init(&intern->std, class_type);
148+
149+
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_readconcern_free_object, NULL TSRMLS_CC);
150+
retval.handlers = &php_phongo_handler_readconcern;
151+
152+
return retval;
153+
} /* }}} */
154+
155+
HashTable *php_phongo_readconcern_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
156+
{
157+
zval retval = zval_used_for_init;
158+
const mongoc_read_concern_t *read_concern = phongo_read_concern_from_zval(object TSRMLS_CC);
159+
160+
161+
*is_temp = 1;
162+
php_phongo_read_concern_to_zval(&retval, read_concern);
163+
164+
return Z_ARRVAL(retval);
165+
} /* }}} */
166+
/* }}} */
167+
168+
/* {{{ PHP_MINIT_FUNCTION */
169+
PHP_MINIT_FUNCTION(ReadConcern)
170+
{
171+
zend_class_entry ce;
172+
(void)type;(void)module_number;
173+
174+
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "ReadConcern", php_phongo_readconcern_me);
175+
php_phongo_readconcern_ce = zend_register_internal_class(&ce TSRMLS_CC);
176+
php_phongo_readconcern_ce->create_object = php_phongo_readconcern_create_object;
177+
PHONGO_CE_INIT(php_phongo_readconcern_ce);
178+
179+
memcpy(&php_phongo_handler_readconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
180+
php_phongo_handler_readconcern.get_debug_info = php_phongo_readconcern_get_debug_info;
181+
182+
zend_declare_class_constant_stringl(php_phongo_readconcern_ce, ZEND_STRL("LOCAL"), ZEND_STRL(MONGOC_READ_CONCERN_LEVEL_LOCAL) TSRMLS_CC);
183+
zend_declare_class_constant_stringl(php_phongo_readconcern_ce, ZEND_STRL("MAJORITY"), ZEND_STRL(MONGOC_READ_CONCERN_LEVEL_MAJORITY) TSRMLS_CC);
184+
185+
return SUCCESS;
186+
}
187+
/* }}} */
188+
189+
190+
191+
/*
192+
* Local variables:
193+
* tab-width: 4
194+
* c-basic-offset: 4
195+
* End:
196+
* vim600: noet sw=4 ts=4 fdm=marker
197+
* vim<600: noet sw=4 ts=4
198+
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\Driver\ReadConcern constants
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
var_dump(MongoDB\Driver\ReadConcern::LOCAL);
10+
var_dump(MongoDB\Driver\ReadConcern::MAJORITY);
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECTF--
16+
string(5) "local"
17+
string(8) "majority"
18+
===DONE===
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
MongoDB\Driver\ReadConcern construction
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
var_dump(new MongoDB\Driver\ReadConcern());
10+
var_dump(new MongoDB\Driver\ReadConcern(null));
11+
var_dump(new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LOCAL));
12+
var_dump(new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::MAJORITY));
13+
var_dump(new MongoDB\Driver\ReadConcern('not-yet-supported'));
14+
15+
?>
16+
===DONE===
17+
<?php exit(0); ?>
18+
--EXPECTF--
19+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
20+
["level"]=>
21+
NULL
22+
}
23+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
24+
["level"]=>
25+
NULL
26+
}
27+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
28+
["level"]=>
29+
string(5) "local"
30+
}
31+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
32+
["level"]=>
33+
string(8) "majority"
34+
}
35+
object(MongoDB\Driver\ReadConcern)#%d (%d) {
36+
["level"]=>
37+
string(17) "not-yet-supported"
38+
}
39+
===DONE===

0 commit comments

Comments
 (0)