Skip to content

Commit 2f1f6b9

Browse files
committed
Merged pull request #694
2 parents ffe1fff + bb2adac commit 2f1f6b9

8 files changed

+37
-48
lines changed

php_phongo.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_ph
621621
php_phongo_writeresult_t *writeresult;
622622
zval *zwriteConcern = NULL;
623623
const mongoc_write_concern_t *write_concern;
624+
bson_t opts = BSON_INITIALIZER;
624625

625626
if (bulk_write->executed) {
626627
phongo_throw_exception(PHONGO_ERROR_WRITE_FAILED TSRMLS_CC, "BulkWrite objects may only be executed once and this instance has already been executed");
@@ -635,10 +636,13 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_ph
635636
/* FIXME: Legacy way of specifying the writeConcern option into this function */
636637
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_writeconcern_ce TSRMLS_CC)) {
637638
zwriteConcern = options;
638-
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_WRITE, NULL, NULL, &zwriteConcern TSRMLS_CC)) {
639+
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_WRITE, &opts, NULL, &zwriteConcern TSRMLS_CC)) {
640+
bson_destroy(&opts);
639641
return false;
640642
}
641643

644+
bson_destroy(&opts);
645+
642646
mongoc_bulk_operation_set_database(bulk, bulk_write->database);
643647
mongoc_bulk_operation_set_collection(bulk, bulk_write->collection);
644648
mongoc_bulk_operation_set_client(bulk, client);
@@ -724,6 +728,7 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
724728
char *collname;
725729
mongoc_collection_t *collection;
726730
zval *zreadPreference = NULL;
731+
bson_t opts = BSON_INITIALIZER;
727732

728733
if (!phongo_split_namespace(namespace, &dbname, &collname)) {
729734
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s: %s", "Invalid namespace provided", namespace);
@@ -742,10 +747,13 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
742747
/* FIXME: Legacy way of specifying the readPreference option into this function */
743748
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_readpreference_ce TSRMLS_CC)) {
744749
zreadPreference = options;
745-
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_READ, NULL, &zreadPreference, NULL TSRMLS_CC)) {
750+
} else if (!phongo_execute_parse_options(client, server_id, options, PHONGO_COMMAND_READ, &opts, &zreadPreference, NULL TSRMLS_CC)) {
751+
bson_destroy(&opts);
746752
return false;
747753
}
748754

755+
bson_destroy(&opts);
756+
749757
cursor = mongoc_collection_find_with_opts(collection, query->filter, query->opts, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC));
750758
mongoc_collection_destroy(collection);
751759

@@ -791,26 +799,24 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
791799
bson_iter_t iter;
792800
bson_t reply;
793801
bson_error_t error;
794-
bson_t *opts;
802+
bson_t opts = BSON_INITIALIZER;
795803
mongoc_cursor_t *cmd_cursor;
796804
uint32_t selected_server_id;
797805
zval *zreadPreference = NULL;
798806
int result;
799807

800808
command = Z_COMMAND_OBJ_P(zcommand);
801809

802-
opts = bson_new();
803-
804810
/* FIXME: Legacy way of specifying the readPreference option into this function */
805811
if (options && Z_TYPE_P(options) == IS_OBJECT && instanceof_function(Z_OBJCE_P(options), php_phongo_readpreference_ce TSRMLS_CC)) {
806812
zreadPreference = options;
807-
} else if (!phongo_execute_parse_options(client, server_id, options, type, opts, &zreadPreference, NULL TSRMLS_CC)) {
813+
} else if (!phongo_execute_parse_options(client, server_id, options, type, &opts, &zreadPreference, NULL TSRMLS_CC)) {
808814
return false;
809815
}
810816

811-
selected_server_id = phongo_do_select_server(client, opts, zreadPreference, server_id TSRMLS_CC);
817+
selected_server_id = phongo_do_select_server(client, &opts, zreadPreference, server_id TSRMLS_CC);
812818
if (!selected_server_id) {
813-
bson_free(opts);
819+
bson_destroy(&opts);
814820
return false;
815821
}
816822

@@ -819,31 +825,31 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
819825
* command construction. */
820826
switch (type) {
821827
case PHONGO_COMMAND_RAW:
822-
result = mongoc_client_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), opts, &reply, &error);
828+
result = mongoc_client_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), &opts, &reply, &error);
823829
break;
824830
case PHONGO_COMMAND_READ:
825-
result = mongoc_client_read_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), opts, &reply, &error);
831+
result = mongoc_client_read_command_with_opts(client, db, command->bson, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC), &opts, &reply, &error);
826832
break;
827833
case PHONGO_COMMAND_WRITE:
828-
result = mongoc_client_write_command_with_opts(client, db, command->bson, opts, &reply, &error);
834+
result = mongoc_client_write_command_with_opts(client, db, command->bson, &opts, &reply, &error);
829835
break;
830836
case PHONGO_COMMAND_READ_WRITE:
831837
/* We can pass NULL as readPreference, as this argument was added historically, but has no function */
832-
result = mongoc_client_read_write_command_with_opts(client, db, command->bson, NULL, opts, &reply, &error);
838+
result = mongoc_client_read_write_command_with_opts(client, db, command->bson, NULL, &opts, &reply, &error);
833839
break;
834840
default:
835841
/* Should never happen, but if it does: exception */
836842
phongo_throw_exception(PHONGO_ERROR_LOGIC TSRMLS_CC, "Type '%d' should never have been passed to phongo_execute_command, please file a bug report", type);
837-
bson_free(opts);
843+
bson_destroy(&opts);
838844
return false;
839845
}
840846
if (!result) {
841847
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
842-
bson_free(opts);
848+
bson_destroy(&opts);
843849
return false;
844850
}
845851

846-
bson_free(opts);
852+
bson_destroy(&opts);
847853

848854
if (!return_value_used) {
849855
bson_destroy(&reply);

tests/manager/manager-executeCommand_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object g
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3737
Unknown option 'unknown'
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
39-
Unknown option 'writeConcern'
39+
Expected 'writeConcern' option to be 'MongoDB\Driver\WriteConcern', string given
4040
===DONE===

tests/server/server-executeBulkWrite-004.phpt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ require_once __DIR__ . "/../utils/basic.inc";
1010
$manager = new MongoDB\Driver\Manager(REPLICASET);
1111
$server = $manager->selectServer(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY));
1212

13-
$bulk = new MongoDB\Driver\BulkWrite();
14-
$bulk->insert(array('wc' => 0));
15-
16-
$result = $server->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(0));
17-
var_dump($result->isAcknowledged());
18-
var_dump($result->getInsertedCount());
19-
2013
$writeConcerns = array(1, 2, MongoDB\Driver\WriteConcern::MAJORITY);
2114

2215
foreach ($writeConcerns as $wc) {
@@ -32,8 +25,6 @@ foreach ($writeConcerns as $wc) {
3225
===DONE===
3326
<?php exit(0); ?>
3427
--EXPECT--
35-
bool(false)
36-
NULL
3728
OK: Got MongoDB\Driver\Exception\RuntimeException
3829
not master
3930
OK: Got MongoDB\Driver\Exception\RuntimeException

tests/server/server-executeBulkWrite-007.phpt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ require_once __DIR__ . "/../utils/basic.inc";
1010
$manager = new MongoDB\Driver\Manager(REPLICASET);
1111
$server = $manager->selectServer(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY));
1212

13-
$bulk = new MongoDB\Driver\BulkWrite();
14-
$bulk->insert(['wc' => 0]);
15-
16-
$result = $server->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(0));
17-
var_dump($result->isAcknowledged());
18-
var_dump($result->getInsertedCount());
19-
2013
$writeConcerns = [1, 2, MongoDB\Driver\WriteConcern::MAJORITY];
2114

2215
foreach ($writeConcerns as $wc) {
@@ -36,8 +29,6 @@ foreach ($writeConcerns as $wc) {
3629
===DONE===
3730
<?php exit(0); ?>
3831
--EXPECT--
39-
bool(false)
40-
NULL
4132
OK: Got MongoDB\Driver\Exception\RuntimeException
4233
not master
4334
OK: Got MongoDB\Driver\Exception\RuntimeException

tests/server/server-executeCommand-002.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ $result = current($cursor->toArray());
1818

1919
printf("Set profile level to 2 successfully: %s\n", (empty($result->ok) ? 'no' : 'yes'));
2020

21-
$command = new MongoDB\Driver\Command(array(
21+
$command = new MongoDB\Driver\Command([
2222
'aggregate' => COLLECTION_NAME,
23-
'pipeline' => array(array('$match' => array('x' => 1))),
24-
));
23+
'pipeline' => [ [ '$match' => [ 'x' => 1 ] ] ],
24+
'cursor' => (object) [],
25+
]);
2526
$secondary->executeCommand(DATABASE_NAME, $command, $rp);
2627

2728
$query = new MongoDB\Driver\Query(
@@ -64,6 +65,9 @@ object(stdClass)#%d (%d) {
6465
}
6566
}
6667
}
68+
["cursor"]=>
69+
object(stdClass)#16 (0) {
70+
}%A
6771
}
6872
Set profile level to 0 successfully: yes
6973
===DONE===

tests/server/server-executeCommand-003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ array(1) {
2828
[0]=>
2929
object(stdClass)#%d (%d) {
3030
["ok"]=>
31-
float(1)
31+
float(1)%A
3232
}
3333
}
3434
===DONE===

tests/server/server-executeCommand_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object g
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3939
Unknown option 'unknown'
4040
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
41-
Unknown option 'writeConcern'
41+
Expected 'writeConcern' option to be 'MongoDB\Driver\WriteConcern', string given
4242
===DONE===

tests/server/server-executeQuery-006.phpt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ $query = new MongoDB\Driver\Query(
3737
$cursor = $secondary->executeQuery(DATABASE_NAME . '.system.profile', $query, $rp);
3838
$profileEntry = current($cursor->toArray());
3939

40-
var_dump($profileEntry->query);
40+
var_dump($profileEntry->command->find);
41+
var_dump($profileEntry->command->filter);
4142

4243
$command = new MongoDB\Driver\Command(array('profile' => 0));
4344
$cursor = $secondary->executeCommand(DATABASE_NAME, $command);
@@ -50,14 +51,10 @@ printf("Set profile level to 0 successfully: %s\n", (empty($result->ok) ? 'no' :
5051
<?php exit(0); ?>
5152
--EXPECTF--
5253
Set profile level to 2 successfully: yes
53-
object(stdClass)#%d (%d) {
54-
["find"]=>
55-
string(%d) "%s"
56-
["filter"]=>
57-
object(stdClass)#%d (1) {
58-
["x"]=>
59-
int(1)
60-
}
54+
string(%d) "%s"
55+
object(stdClass)#%d (1) {
56+
["x"]=>
57+
int(1)
6158
}
6259
Set profile level to 0 successfully: yes
6360
===DONE===

0 commit comments

Comments
 (0)