Skip to content

Commit 292deec

Browse files
committed
Bug #29402358: CRUD METHODS SUCH AS .FIELDS()/.SORT() ARE WRONGLY
ACCUMULATED
1 parent 1f54c14 commit 292deec

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

devapi/tests/bugs-t.cc

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program is free software; you can redistribute it and/or modify
@@ -490,3 +490,92 @@ TEST_F(Bugs, crud_move)
490490

491491

492492
}
493+
494+
TEST_F(Bugs, not_accumulate)
495+
{
496+
SKIP_IF_NO_XPLUGIN;
497+
498+
auto sch = get_sess().createSchema("test",true);
499+
auto coll = sch.createCollection("c1",true);
500+
auto tbl = sch.getCollectionAsTable("c1");
501+
502+
coll.remove("true").execute();
503+
504+
coll.add("{ \"_id\": \"myuuid-1\", \"name\": \"foo\", \"age\": 7 }",
505+
"{ \"name\": \"buz\", \"age\": 17 }",
506+
"{ \"name\": \"bar\", \"age\": 3 }",
507+
"{ \"name\": \"baz\", \"age\": 3 }"
508+
).execute();
509+
510+
511+
//FIND
512+
513+
auto find = coll.find();
514+
find.fields("notfound");
515+
find.fields("name as name", "age as age");
516+
find.groupBy("notfound");
517+
find.groupBy("age","name");
518+
find.sort("notfound");
519+
find.sort("age ASC");
520+
521+
auto doc = find.execute().fetchOne();
522+
EXPECT_EQ(3, doc["age"].get<int>());
523+
EXPECT_EQ(string("bar"), doc["name"].get<string>());
524+
525+
526+
// MODIFY
527+
528+
auto modify = coll.modify("true");
529+
modify.set("food", expr("[]"));
530+
modify.arrayAppend("food", "milk");
531+
modify.arrayAppend("food", "soup");
532+
modify.arrayAppend("food", "potatoes");
533+
modify.sort("notfound");
534+
modify.sort("age ASC");
535+
modify.limit(2);
536+
//only age=3 will be modified
537+
modify.execute();
538+
539+
auto check_changes = coll.find().sort("age ASC").execute();
540+
EXPECT_TRUE(check_changes.fetchOne().hasField("food"));
541+
EXPECT_TRUE(check_changes.fetchOne().hasField("food"));
542+
EXPECT_FALSE(check_changes.fetchOne().hasField("food"));
543+
EXPECT_FALSE(check_changes.fetchOne().hasField("food"));
544+
545+
//REMOVE
546+
547+
auto remove = coll.remove("true");
548+
remove.sort("name DESC");
549+
remove.sort("age ASC");
550+
remove.limit(2);
551+
//only age=3 will be removed
552+
remove.execute();
553+
554+
check_changes = coll.find().execute();
555+
for(auto doc : check_changes)
556+
{
557+
EXPECT_NE(3, doc["age"].get<int>());
558+
}
559+
560+
// TABLE
561+
auto select = tbl.select("doc->$.age");
562+
select.orderBy("notfound ASC");
563+
select.orderBy("doc->$.age ASC");
564+
select.groupBy("notfound");
565+
select.groupBy("doc->$.age");
566+
567+
select.lockExclusive();
568+
EXPECT_EQ(2, select.execute().count());
569+
570+
auto update = tbl.update();
571+
update.set("doc->$.age",1);
572+
update.where("doc->$.age > 7");
573+
update.orderBy("notfound ASC");
574+
update.orderBy("doc->$.age ASC");
575+
EXPECT_EQ(1, update.execute().getAffectedItemsCount());
576+
577+
auto tbl_remove = tbl.remove();
578+
tbl_remove.orderBy("notfound ASC");
579+
tbl_remove.orderBy("doc->$.age ASC");
580+
EXPECT_EQ(2, tbl_remove.execute().getAffectedItemsCount());
581+
}

include/mysqlx/devapi/collection_crud.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ class CollectionFind
387387
Operation& fields(Expr... proj)
388388
{
389389
try {
390+
get_impl()->clear_proj();
390391
do_fields(get_impl(), proj...);
391392
return *this;
392393
}

include/mysqlx/devapi/crud.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class Sort
216216
Operation& sort(Type... spec)
217217
{
218218
try {
219+
get_impl()->clear_sort();
219220
add_sort(get_impl(), spec...);
220221
return *this;
221222
}
@@ -256,6 +257,7 @@ class Order_by
256257
Operation& orderBy(Type... spec)
257258
{
258259
try {
260+
get_impl()->clear_sort();
259261
add_sort(get_impl(), spec...);
260262
return *this;
261263
}
@@ -331,6 +333,7 @@ class Group_by
331333
Operation& groupBy(Expr... group_by_spec)
332334
{
333335
try {
336+
get_impl()->clear_group_by();
334337
do_group_by(get_impl(), group_by_spec...);
335338
return *this;
336339
}

0 commit comments

Comments
 (0)