Skip to content

Commit ae8a5e9

Browse files
committed
Merge pull request #98
2 parents 6bafc95 + c224e6d commit ae8a5e9

File tree

6 files changed

+207
-9
lines changed

6 files changed

+207
-9
lines changed

php_phongo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t
105105
return php_phongo_connectionexception_ce;
106106
}
107107

108-
MONGOC_ERROR("Resolving unknown exception domain!!!");
109-
return spl_ce_RuntimeException;
108+
MONGOC_ERROR("Resolving unknown phongo error domain: %d", domain);
109+
return php_phongo_runtimeexception_ce;
110110
}
111111
zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_domain_t */ domain, uint32_t /* mongoc_error_code_t */ code)
112112
{
@@ -166,7 +166,7 @@ zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_d
166166
return phongo_ce_mongo_connection_exception;
167167
#endif
168168
default:
169-
return spl_ce_RuntimeException;
169+
return php_phongo_runtimeexception_ce;
170170
}
171171
}
172172
PHONGO_API zval* phongo_throw_exception(php_phongo_error_domain_t domain TSRMLS_DC, const char *format, ...)

src/MongoDB/Server.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,48 +58,50 @@ PHP_METHOD(Server, __construct)
5858
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Accessing private constructor");
5959
}
6060
/* }}} */
61-
/* {{{ proto MongoDB\Driver\Cursor Server::executeCommand(string $db, MongoDB\Driver\Command $command)
61+
/* {{{ proto MongoDB\Driver\Cursor Server::executeCommand(string $db, MongoDB\Driver\Command $command[, MongoDB\Driver\ReadPreference $readPreference = null]))
6262
Executes a command on this server */
6363
PHP_METHOD(Server, executeCommand)
6464
{
6565
php_phongo_server_t *intern;
6666
char *db;
6767
int db_len;
6868
zval *command;
69+
zval *readPreference = NULL;
6970
php_phongo_command_t *cmd;
7071
(void)return_value_ptr;
7172

7273

7374
intern = (php_phongo_server_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
7475

75-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO", &db, &db_len, &command, php_phongo_command_ce) == FAILURE) {
76+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO|O!", &db, &db_len, &command, php_phongo_command_ce, &readPreference, php_phongo_readpreference_ce) == FAILURE) {
7677
return;
7778
}
7879

7980

8081
cmd = (php_phongo_command_t *)zend_object_store_get_object(command TSRMLS_CC);
81-
phongo_execute_command(intern->client, db, cmd->bson, NULL, intern->server_id, return_value, return_value_used TSRMLS_CC);
82+
phongo_execute_command(intern->client, db, cmd->bson, phongo_read_preference_from_zval(readPreference TSRMLS_CC), intern->server_id, return_value, return_value_used TSRMLS_CC);
8283
}
8384
/* }}} */
84-
/* {{{ proto MongoDB\Driver\Cursor Server::executeQuery(string $namespace, MongoDB\Driver\Query $zquery)
85+
/* {{{ proto MongoDB\Driver\Cursor Server::executeQuery(string $namespace, MongoDB\Driver\Query $zquery[, MongoDB\Driver\ReadPreference $readPreference = null]))
8586
Executes a Query */
8687
PHP_METHOD(Server, executeQuery)
8788
{
8889
php_phongo_server_t *intern;
8990
char *namespace;
9091
int namespace_len;
9192
zval *zquery;
93+
zval *readPreference = NULL;
9294
(void)return_value_ptr;
9395

9496

9597
intern = (php_phongo_server_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
9698

97-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO", &namespace, &namespace_len, &zquery, php_phongo_query_ce) == FAILURE) {
99+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO|O!", &namespace, &namespace_len, &zquery, php_phongo_query_ce, &readPreference, php_phongo_readpreference_ce) == FAILURE) {
98100
return;
99101
}
100102

101103

102-
phongo_execute_query(intern->client, namespace, phongo_query_from_zval(zquery TSRMLS_CC), NULL, intern->server_id, return_value, return_value_used TSRMLS_CC);
104+
phongo_execute_query(intern->client, namespace, phongo_query_from_zval(zquery TSRMLS_CC), phongo_read_preference_from_zval(readPreference TSRMLS_CC), intern->server_id, return_value, return_value_used TSRMLS_CC);
103105
}
104106
/* }}} */
105107
/* {{{ proto MongoDB\Driver\WriteResult Server::executeBulkWrite(string $namespace, MongoDB\Driver\BulkWrite $zbulk[, MongoDB\Driver\WriteConcern $writeConcern = null])
@@ -391,11 +393,13 @@ PHP_METHOD(Server, isPassive)
391393
ZEND_BEGIN_ARG_INFO_EX(ai_Server_executeCommand, 0, 0, 2)
392394
ZEND_ARG_INFO(0, db)
393395
ZEND_ARG_OBJ_INFO(0, command, MongoDB\\Driver\\Command, 0)
396+
ZEND_ARG_OBJ_INFO(0, readPreference, MongoDB\\Driver\\ReadPreference, 1)
394397
ZEND_END_ARG_INFO();
395398

396399
ZEND_BEGIN_ARG_INFO_EX(ai_Server_executeQuery, 0, 0, 2)
397400
ZEND_ARG_INFO(0, namespace)
398401
ZEND_ARG_OBJ_INFO(0, zquery, MongoDB\\Driver\\Query, 0)
402+
ZEND_ARG_OBJ_INFO(0, readPreference, MongoDB\\Driver\\ReadPreference, 1)
399403
ZEND_END_ARG_INFO();
400404

401405
ZEND_BEGIN_ARG_INFO_EX(ai_Server_executeBulkWrite, 0, 0, 2)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--TEST--
2+
MongoDB\Driver\Server::executeCommand() takes a read preference
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(REPLICASET);
10+
11+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
12+
$secondary = $manager->selectServer($rp);
13+
14+
$command = new MongoDB\Driver\Command(array('profile' => 2));
15+
$cursor = $secondary->executeCommand(DATABASE_NAME, $command);
16+
$result = current($cursor->toArray());
17+
18+
printf("Set profile level to 2 successfully: %s\n", (empty($result->ok) ? 'no' : 'yes'));
19+
20+
$command = new MongoDB\Driver\Command(array(
21+
'aggregate' => COLLECTION_NAME,
22+
'pipeline' => array(array('$match' => array('x' => 1))),
23+
));
24+
$secondary->executeCommand(DATABASE_NAME, $command, $rp);
25+
26+
$query = new MongoDB\Driver\Query(
27+
array(
28+
'op' => 'command',
29+
'ns' => DATABASE_NAME . '.$cmd',
30+
),
31+
array(
32+
'sort' => array('ts' => -1),
33+
'limit' => 1,
34+
)
35+
);
36+
$cursor = $secondary->executeQuery(DATABASE_NAME . '.system.profile', $query, $rp);
37+
$profileEntry = current($cursor->toArray());
38+
39+
var_dump($profileEntry->command);
40+
41+
$command = new MongoDB\Driver\Command(array('profile' => 0));
42+
$cursor = $secondary->executeCommand(DATABASE_NAME, $command);
43+
$result = current($cursor->toArray());
44+
45+
printf("Set profile level to 0 successfully: %s\n", (empty($result->ok) ? 'no' : 'yes'));
46+
47+
?>
48+
===DONE===
49+
<?php exit(0); ?>
50+
--EXPECTF--
51+
Set profile level to 2 successfully: yes
52+
object(stdClass)#%d (%d) {
53+
["aggregate"]=>
54+
string(32) "server_server_executeCommand_002"
55+
["pipeline"]=>
56+
array(1) {
57+
[0]=>
58+
object(stdClass)#%d (%d) {
59+
["$match"]=>
60+
object(stdClass)#%d (%d) {
61+
["x"]=>
62+
int(1)
63+
}
64+
}
65+
}
66+
["$readPreference"]=>
67+
object(stdClass)#%d (%d) {
68+
["mode"]=>
69+
string(9) "secondary"
70+
["tags"]=>
71+
array(0) {
72+
}
73+
}
74+
}
75+
Set profile level to 0 successfully: yes
76+
===DONE===
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\Driver\Server::executeCommand() with conflicting read preference for secondary
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(REPLICASET);
10+
11+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
12+
$secondary = $manager->selectServer($rp);
13+
14+
echo throws(function() use ($secondary) {
15+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
16+
$secondary->executeCommand(NS, new MongoDB\Driver\Command(array('ping' => 1)), $rp);
17+
}, "MongoDB\Driver\Exception\RuntimeException"), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
OK: Got MongoDB\Driver\Exception\RuntimeException
24+
not master and slaveOk=false
25+
===DONE===
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
MongoDB\Driver\Server::executeQuery() takes a read preference
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(REPLICASET);
10+
11+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
12+
$secondary = $manager->selectServer($rp);
13+
14+
$command = new MongoDB\Driver\Command(array('profile' => 2));
15+
$cursor = $secondary->executeCommand(DATABASE_NAME, $command);
16+
$result = current($cursor->toArray());
17+
18+
printf("Set profile level to 2 successfully: %s\n", (empty($result->ok) ? 'no' : 'yes'));
19+
20+
if (empty($result->ok)) {
21+
exit("Could not set profile level\n");
22+
}
23+
24+
$secondary->executeQuery(NS, new MongoDB\Driver\Query(array("x" => 1)), $rp);
25+
26+
$query = new MongoDB\Driver\Query(
27+
array(
28+
'op' => 'query',
29+
'ns' => NS,
30+
),
31+
array(
32+
'sort' => array('ts' => -1),
33+
'limit' => 1,
34+
)
35+
);
36+
$cursor = $secondary->executeQuery(DATABASE_NAME . '.system.profile', $query, $rp);
37+
$profileEntry = current($cursor->toArray());
38+
39+
var_dump($profileEntry->query);
40+
41+
$command = new MongoDB\Driver\Command(array('profile' => 0));
42+
$cursor = $secondary->executeCommand(DATABASE_NAME, $command);
43+
$result = current($cursor->toArray());
44+
45+
printf("Set profile level to 0 successfully: %s\n", (empty($result->ok) ? 'no' : 'yes'));
46+
47+
?>
48+
===DONE===
49+
<?php exit(0); ?>
50+
--EXPECTF--
51+
Set profile level to 2 successfully: yes
52+
object(stdClass)#%d (%d) {
53+
["$query"]=>
54+
object(stdClass)#%d (%d) {
55+
["x"]=>
56+
int(1)
57+
}
58+
["$readPreference"]=>
59+
object(stdClass)#%d (%d) {
60+
["mode"]=>
61+
string(9) "secondary"
62+
["tags"]=>
63+
array(0) {
64+
}
65+
}
66+
}
67+
Set profile level to 0 successfully: yes
68+
===DONE===
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\Driver\Server::executeQuery() with conflicting read preference for secondary
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(REPLICASET);
10+
11+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
12+
$secondary = $manager->selectServer($rp);
13+
14+
echo throws(function() use ($secondary) {
15+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
16+
$secondary->executeQuery(NS, new MongoDB\Driver\Query(array("x" => 1)), $rp);
17+
}, "MongoDB\Driver\Exception\RuntimeException"), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
OK: Got MongoDB\Driver\Exception\RuntimeException
24+
not master and slaveOk=false
25+
===DONE===

0 commit comments

Comments
 (0)