Skip to content

Commit d55b348

Browse files
committed
Update replaceOne() API method to current specs.
1 parent 13b27eb commit d55b348

File tree

5 files changed

+51
-61
lines changed

5 files changed

+51
-61
lines changed

devapi/collection_crud.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ class Op_collection_modify
647647
}
648648

649649
friend mysqlx::CollectionModify;
650-
friend mysqlx::CollectionReplace;
650+
friend mysqlx::internal::CollectionReplace;
651651
};
652652

653653

@@ -662,8 +662,9 @@ CollectionModify::CollectionModify(
662662
CATCH_AND_WRAP
663663
}
664664

665-
CollectionReplace::CollectionReplace(Collection &coll, const mysqlx::string &id
666-
, mysqlx::internal::ExprValue &&val)
665+
internal::CollectionReplace::CollectionReplace(Collection &coll,
666+
const mysqlx::string &id,
667+
mysqlx::internal::ExprValue &&val)
667668
{
668669
try
669670
{
@@ -686,3 +687,4 @@ CollectionReplace::CollectionReplace(Collection &coll, const mysqlx::string &id
686687
}
687688
CATCH_AND_WRAP
688689
}
690+

devapi/tests/crud-t.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,28 +2236,22 @@ TEST_F(Crud, single_document)
22362236
EXPECT_TRUE(coll.getOne("id1").isNull());
22372237

22382238
// Replace existing document
2239-
CollectionReplace replace = coll.replaceOne("id3", expr("{\"name\": :name }"))
2240-
.bind("name", "qux");
2241-
EXPECT_EQ(1, replace.execute().getAffectedItemsCount()
2242-
);
2239+
EXPECT_TRUE(coll.replaceOne("id3", expr("{\"name\": \"qux\" }")));
22432240
EXPECT_EQ(string("qux"), coll.getOne("id3")["name"].get<string>());
22442241

22452242
// Ignore _id field on document and replace existing docment
22462243
// Document passed as string
2247-
replace = coll.replaceOne("id3", "{\"_id\": \"id4\", \"name\": \"baz\" }");
2248-
EXPECT_EQ(1, replace.execute().getAffectedItemsCount());
2244+
EXPECT_EQ(1, coll.replaceOne("id3", "{\"_id\": \"id4\", \"name\": \"baz\" }"));
22492245
EXPECT_EQ(string("baz"), coll.getOne("id3")["name"].get<string>());
22502246
EXPECT_EQ(string("id3"), coll.getOne("id3")["_id"].get<string>());
22512247

22522248
// should not affect none
2253-
replace = coll.replaceOne("id4", expr("{\"_id\":\"id4\", \"name\": :name }"));
2254-
EXPECT_EQ(0, replace.bind("name", "baz").execute().getAffectedItemsCount());
2249+
EXPECT_FALSE(coll.replaceOne("id4", expr("{\"_id\":\"id4\", \"name\": \"baz\" }")));
22552250

22562251
// Using DbDoc
22572252
DbDoc doc("{\"_id\":\"id4\", \"name\": \"quux\" }");
22582253

2259-
replace = coll.replaceOne("id3", doc);
2260-
EXPECT_EQ(1, replace.execute().getAffectedItemsCount());
2254+
EXPECT_TRUE(coll.replaceOne("id3", doc));
22612255
EXPECT_EQ(string("quux"), coll.getOne("id3")["name"].get<string>());
22622256
}
22632257

devapi/tests/first-t.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,6 @@ TEST_F(First, api)
232232
CollectionModify modify2 = x;
233233
}
234234

235-
{
236-
CollectionReplace replace = c.replaceOne("...", "...");
237-
replace = replace.bind("...", 0);
238-
auto x = replace.bind("...", 0);
239-
CollectionReplace replace1 = replace;
240-
CollectionReplace replace2 = x;
241-
}
242-
243235
// Test copy semantics for table operations
244236

245237
{

include/devapi/collection_crud.h

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,11 @@ DLL_WARNINGS_POP
393393
*/
394394

395395
class CollectionModify;
396-
class CollectionReplace;
397396

398397
namespace internal {
399398

399+
class CollectionReplace;
400+
400401
struct Collection_modify_cmd
401402
: public Executable<Result, CollectionModify>
402403
{};
@@ -413,6 +414,43 @@ struct Collection_replace_base
413414
: public Bind_parameters< Collection_replace_cmd >
414415
{};
415416

417+
/*
418+
Operation which replaces a document, using the id by a new one
419+
*/
420+
421+
class PUBLIC_API CollectionReplace
422+
: public internal::Collection_replace_base
423+
{
424+
425+
public:
426+
427+
/*
428+
Create operation which replaces document with specified _id in a collection.
429+
*/
430+
CollectionReplace(Collection &base,
431+
const string &id,
432+
internal::ExprValue &&val);
433+
434+
CollectionReplace(const internal::Collection_replace_cmd &other)
435+
{
436+
internal::Collection_replace_cmd::operator=(other);
437+
}
438+
439+
CollectionReplace(internal::Collection_replace_cmd &&other)
440+
{
441+
internal::Collection_replace_cmd::operator=(std::move(other));
442+
}
443+
444+
protected:
445+
446+
using Impl = Collection_modify_impl;
447+
448+
Impl* get_impl()
449+
{
450+
return static_cast<Impl*>(internal::Collection_replace_base::get_impl());
451+
}
452+
};
453+
416454
} // internal namespace
417455

418456

@@ -537,44 +575,7 @@ class PUBLIC_API CollectionModify
537575
};
538576

539577

540-
/**
541-
Operation which replaces a document, using the id by a new one
542-
543-
@ingroup devapi_op
544-
*/
545578

546-
class PUBLIC_API CollectionReplace
547-
: public internal::Collection_replace_base
548-
{
549-
550-
public:
551-
552-
/**
553-
Create operation which replaces document with specified _id in a collection.
554-
*/
555-
CollectionReplace(Collection &base,
556-
const string &id,
557-
internal::ExprValue &&val);
558-
559-
CollectionReplace(const internal::Collection_replace_cmd &other)
560-
{
561-
internal::Collection_replace_cmd::operator=(other);
562-
}
563-
564-
CollectionReplace(internal::Collection_replace_cmd &&other)
565-
{
566-
internal::Collection_replace_cmd::operator=(std::move(other));
567-
}
568-
569-
protected:
570-
571-
using Impl = internal::Collection_modify_impl;
572-
573-
Impl* get_impl()
574-
{
575-
return static_cast<Impl*>(internal::Collection_replace_base::get_impl());
576-
}
577-
};
578579

579580
} // mysqlx namespace
580581

include/mysql_devapi.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,10 @@ class PUBLIC_API Collection
427427
parameters with .bind() before executing the statement.
428428
*/
429429

430-
CollectionReplace replaceOne(string id, internal::ExprValue &&document)
430+
bool replaceOne(string id, internal::ExprValue &&document)
431431
{
432-
return CollectionReplace(*this, id, std::move(document));
432+
return
433+
internal::CollectionReplace(*this, id, std::move(document)).execute().getAffectedItemsCount() == 1;
433434
}
434435

435436
};

0 commit comments

Comments
 (0)