Skip to content

Commit f4de8a4

Browse files
committed
PHPC-243: Manager->executeUpdate() option is supposed to be 'multi'
1 parent 4da1efd commit f4de8a4

File tree

4 files changed

+292
-16
lines changed

4 files changed

+292
-16
lines changed

php_phongo.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,24 @@ int phongo_execute_single_update(mongoc_client_t *client, const char *namespace,
434434

435435
bulk = phongo_bulkwrite_init(true);
436436
if (flags & MONGOC_UPDATE_MULTI_UPDATE) {
437-
mongoc_bulk_operation_update_one(bulk, query, update, !!(flags & MONGOC_UPDATE_UPSERT));
438-
} else {
439437
mongoc_bulk_operation_update(bulk, query, update, !!(flags & MONGOC_UPDATE_UPSERT));
438+
} else {
439+
bson_iter_t iter;
440+
zend_bool replaced = 0;
441+
442+
if (bson_iter_init(&iter, update)) {
443+
while (bson_iter_next (&iter)) {
444+
if (!strchr (bson_iter_key (&iter), '$')) {
445+
mongoc_bulk_operation_replace_one(bulk, query, update, !!(flags & MONGOC_UPDATE_UPSERT));
446+
replaced = 1;
447+
break;
448+
}
449+
}
450+
}
451+
452+
if (!replaced) {
453+
mongoc_bulk_operation_update_one(bulk, query, update, !!(flags & MONGOC_UPDATE_UPSERT));
454+
}
440455
}
441456
retval = phongo_execute_write(client, namespace, bulk, write_concern, server_id, return_value, return_value_used TSRMLS_CC);
442457
mongoc_bulk_operation_destroy(bulk);

src/MongoDB/BulkWrite.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ PHP_METHOD(BulkWrite, update)
124124
zval *query;
125125
zval *newObj;
126126
zval *updateOptions = NULL;
127-
mongoc_update_flags_t limit = 0;
128-
zend_bool upsert = 0;
127+
mongoc_update_flags_t flags = MONGOC_UPDATE_NONE;
129128
bson_t *bquery;
130129
bson_t *bupdate;
131130
(void)return_value_ptr; (void)return_value; (void)return_value_used;
@@ -145,29 +144,29 @@ PHP_METHOD(BulkWrite, update)
145144
zval_to_bson(newObj, PHONGO_BSON_NONE, bupdate, NULL TSRMLS_CC);
146145

147146
if (updateOptions) {
148-
limit = php_array_fetch_bool(updateOptions, "limit");
149-
upsert = php_array_fetch_bool(updateOptions, "upsert");
147+
flags |= php_array_fetch_bool(updateOptions, "multi") ? MONGOC_UPDATE_MULTI_UPDATE : 0;
148+
flags |= php_array_fetch_bool(updateOptions, "upsert") ? MONGOC_UPDATE_UPSERT : 0;
150149
}
151150

152-
if (limit) {
151+
if (flags & MONGOC_UPDATE_MULTI_UPDATE) {
152+
mongoc_bulk_operation_update(intern->bulk, bquery, bupdate, !!(flags & MONGOC_UPDATE_UPSERT));
153+
} else {
153154
bson_iter_t iter;
154155
zend_bool replaced = 0;
155156

156157
if (bson_iter_init(&iter, bupdate)) {
157158
while (bson_iter_next (&iter)) {
158159
if (!strchr (bson_iter_key (&iter), '$')) {
159-
mongoc_bulk_operation_replace_one(intern->bulk, bquery, bupdate, upsert);
160+
mongoc_bulk_operation_replace_one(intern->bulk, bquery, bupdate, !!(flags & MONGOC_UPDATE_UPSERT));
160161
replaced = 1;
161162
break;
162163
}
163164
}
164165
}
165166

166167
if (!replaced) {
167-
mongoc_bulk_operation_update_one(intern->bulk, bquery, bupdate, upsert);
168+
mongoc_bulk_operation_update_one(intern->bulk, bquery, bupdate, !!(flags & MONGOC_UPDATE_UPSERT));
168169
}
169-
} else {
170-
mongoc_bulk_operation_update(intern->bulk, bquery, bupdate, upsert);
171170
}
172171

173172
bson_clear(&bquery);

src/MongoDB/Manager.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,9 @@ PHP_METHOD(Manager, executeUpdate)
206206
zval_to_bson(zquery, PHONGO_BSON_NONE, query, NULL TSRMLS_CC);
207207
zval_to_bson(newObj, PHONGO_BSON_NONE, update, NULL TSRMLS_CC);
208208

209-
if (updateOptions && php_array_fetch_bool(updateOptions, "upsert")) {
210-
flags |= MONGOC_UPDATE_UPSERT;
211-
}
212-
if (updateOptions && php_array_fetch_bool(updateOptions, "limit")) {
213-
flags |= MONGOC_UPDATE_MULTI_UPDATE;
209+
if (updateOptions) {
210+
flags |= php_array_fetch_bool(updateOptions, "multi") ? MONGOC_UPDATE_MULTI_UPDATE : 0 ;
211+
flags |= php_array_fetch_bool(updateOptions, "upsert") ? MONGOC_UPDATE_UPSERT : 0;
214212
}
215213

216214
phongo_execute_single_update(intern->client, namespace, query, update, phongo_write_concern_from_zval(zwrite_concern TSRMLS_CC), -1, flags, return_value, return_value_used TSRMLS_CC);
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
--TEST--
2+
PHPC-243: Manager::executeUpdate() & Bulk->update() w/o multi
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE) ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(STANDALONE);
10+
11+
// load fixtures for test
12+
$manager->executeInsert(NS, array('_id' => 1, 'x' => 1));
13+
$manager->executeInsert(NS, array('_id' => 2, 'x' => 2));
14+
$manager->executeInsert(NS, array('_id' => 3, 'x' => 2));
15+
$manager->executeInsert(NS, array('_id' => 4, 'x' => 2));
16+
$manager->executeInsert(NS, array('_id' => 5, 'x' => 1));
17+
$manager->executeInsert(NS, array('_id' => 6, 'x' => 1));
18+
19+
$result = $manager->executeUpdate(
20+
NS,
21+
array('x' => 1),
22+
array('$set' => array('x' => 3)),
23+
array('multi' => false, 'upsert' => false)
24+
);
25+
26+
printf("Changed %d out of expected 1 (_id=1)\n", $result->getModifiedCount());
27+
28+
29+
$result = $manager->executeUpdate(
30+
NS,
31+
array('x' => 1),
32+
array('$set' => array('x' => 2)),
33+
array('multi' => true, 'upsert' => false)
34+
);
35+
36+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array()));
37+
var_dump(iterator_to_array($cursor));
38+
printf("Changed %d out of expected 2, (_id=5, _id=6)\n", $result->getModifiedCount());
39+
40+
$bulk = new MongoDB\Driver\BulkWrite;
41+
$bulk->update(
42+
array('x' => 2),
43+
array('$set' => array('x' => 4)),
44+
array('multi' => false, 'upsert' => false)
45+
);
46+
47+
$result = $manager->executeBulkWrite(NS, $bulk);
48+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array()));
49+
var_dump(iterator_to_array($cursor));
50+
printf("Changed %d out of expected 1, (_id=2)\n", $result->getModifiedCount());
51+
52+
53+
54+
$bulk = new MongoDB\Driver\BulkWrite;
55+
$bulk->update(
56+
array('x' => 2),
57+
array('$set' => array('x' => 41)),
58+
array('multi' => false, 'upsert' => false)
59+
);
60+
61+
$result = $manager->executeBulkWrite(NS, $bulk);
62+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array()));
63+
var_dump(iterator_to_array($cursor));
64+
printf("Changed %d out of expected 1 (id_=3)\n", $result->getModifiedCount());
65+
66+
67+
68+
$bulk = new MongoDB\Driver\BulkWrite;
69+
$bulk->update(
70+
array('x' => 2),
71+
array('$set' => array('x' => 42)),
72+
array('multi' => true, 'upsert' => false)
73+
);
74+
75+
$result = $manager->executeBulkWrite(NS, $bulk);
76+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array()));
77+
var_dump(iterator_to_array($cursor));
78+
printf("Changed %d out of expected 3 (_id=4, _id=5, _id=6)\n", $result->getModifiedCount());
79+
?>
80+
===DONE===
81+
<?php exit(0); ?>
82+
--EXPECT--
83+
Changed 1 out of expected 1 (_id=1)
84+
array(6) {
85+
[0]=>
86+
array(2) {
87+
["_id"]=>
88+
int(1)
89+
["x"]=>
90+
int(3)
91+
}
92+
[1]=>
93+
array(2) {
94+
["_id"]=>
95+
int(2)
96+
["x"]=>
97+
int(2)
98+
}
99+
[2]=>
100+
array(2) {
101+
["_id"]=>
102+
int(3)
103+
["x"]=>
104+
int(2)
105+
}
106+
[3]=>
107+
array(2) {
108+
["_id"]=>
109+
int(4)
110+
["x"]=>
111+
int(2)
112+
}
113+
[4]=>
114+
array(2) {
115+
["_id"]=>
116+
int(5)
117+
["x"]=>
118+
int(2)
119+
}
120+
[5]=>
121+
array(2) {
122+
["_id"]=>
123+
int(6)
124+
["x"]=>
125+
int(2)
126+
}
127+
}
128+
Changed 2 out of expected 2, (_id=5, _id=6)
129+
array(6) {
130+
[0]=>
131+
array(2) {
132+
["_id"]=>
133+
int(1)
134+
["x"]=>
135+
int(3)
136+
}
137+
[1]=>
138+
array(2) {
139+
["_id"]=>
140+
int(2)
141+
["x"]=>
142+
int(4)
143+
}
144+
[2]=>
145+
array(2) {
146+
["_id"]=>
147+
int(3)
148+
["x"]=>
149+
int(2)
150+
}
151+
[3]=>
152+
array(2) {
153+
["_id"]=>
154+
int(4)
155+
["x"]=>
156+
int(2)
157+
}
158+
[4]=>
159+
array(2) {
160+
["_id"]=>
161+
int(5)
162+
["x"]=>
163+
int(2)
164+
}
165+
[5]=>
166+
array(2) {
167+
["_id"]=>
168+
int(6)
169+
["x"]=>
170+
int(2)
171+
}
172+
}
173+
Changed 1 out of expected 1, (_id=2)
174+
array(6) {
175+
[0]=>
176+
array(2) {
177+
["_id"]=>
178+
int(1)
179+
["x"]=>
180+
int(3)
181+
}
182+
[1]=>
183+
array(2) {
184+
["_id"]=>
185+
int(2)
186+
["x"]=>
187+
int(4)
188+
}
189+
[2]=>
190+
array(2) {
191+
["_id"]=>
192+
int(3)
193+
["x"]=>
194+
int(41)
195+
}
196+
[3]=>
197+
array(2) {
198+
["_id"]=>
199+
int(4)
200+
["x"]=>
201+
int(2)
202+
}
203+
[4]=>
204+
array(2) {
205+
["_id"]=>
206+
int(5)
207+
["x"]=>
208+
int(2)
209+
}
210+
[5]=>
211+
array(2) {
212+
["_id"]=>
213+
int(6)
214+
["x"]=>
215+
int(2)
216+
}
217+
}
218+
Changed 1 out of expected 1 (id_=3)
219+
array(6) {
220+
[0]=>
221+
array(2) {
222+
["_id"]=>
223+
int(1)
224+
["x"]=>
225+
int(3)
226+
}
227+
[1]=>
228+
array(2) {
229+
["_id"]=>
230+
int(2)
231+
["x"]=>
232+
int(4)
233+
}
234+
[2]=>
235+
array(2) {
236+
["_id"]=>
237+
int(3)
238+
["x"]=>
239+
int(41)
240+
}
241+
[3]=>
242+
array(2) {
243+
["_id"]=>
244+
int(4)
245+
["x"]=>
246+
int(42)
247+
}
248+
[4]=>
249+
array(2) {
250+
["_id"]=>
251+
int(5)
252+
["x"]=>
253+
int(42)
254+
}
255+
[5]=>
256+
array(2) {
257+
["_id"]=>
258+
int(6)
259+
["x"]=>
260+
int(42)
261+
}
262+
}
263+
Changed 3 out of expected 3 (_id=4, _id=5, _id=6)
264+
===DONE===

0 commit comments

Comments
 (0)