1 | // |
2 | // InsertRequest.h |
3 | // |
4 | // Library: MongoDB |
5 | // Package: MongoDB |
6 | // Module: InsertRequest |
7 | // |
8 | // Definition of the InsertRequest class. |
9 | // |
10 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef MongoDB_InsertRequest_INCLUDED |
18 | #define MongoDB_InsertRequest_INCLUDED |
19 | |
20 | |
21 | #include "Poco/MongoDB/MongoDB.h" |
22 | #include "Poco/MongoDB/RequestMessage.h" |
23 | #include "Poco/MongoDB/Document.h" |
24 | |
25 | |
26 | namespace Poco { |
27 | namespace MongoDB { |
28 | |
29 | |
30 | class MongoDB_API InsertRequest: public RequestMessage |
31 | /// A request for inserting one or more documents to the database |
32 | /// (OP_INSERT). |
33 | { |
34 | public: |
35 | enum Flags |
36 | { |
37 | INSERT_DEFAULT = 0, |
38 | /// If specified, perform a normal insert operation. |
39 | |
40 | INSERT_CONTINUE_ON_ERROR = 1 |
41 | /// If set, the database will not stop processing a bulk insert if one |
42 | /// fails (e.g. due to duplicate IDs). This makes bulk insert behave similarly |
43 | /// to a series of single inserts, except lastError will be set if any insert |
44 | /// fails, not just the last one. If multiple errors occur, only the most |
45 | /// recent will be reported. |
46 | }; |
47 | |
48 | InsertRequest(const std::string& collectionName, Flags flags = INSERT_DEFAULT); |
49 | /// Creates an InsertRequest. |
50 | /// |
51 | /// The full collection name is the concatenation of the database |
52 | /// name with the collection name, using a "." for the concatenation. For example, |
53 | /// for the database "foo" and the collection "bar", the full collection name is |
54 | /// "foo.bar". |
55 | |
56 | virtual ~InsertRequest(); |
57 | /// Destroys the InsertRequest. |
58 | |
59 | Document& addNewDocument(); |
60 | /// Adds a new document for insertion. A reference to the empty document is |
61 | /// returned. InsertRequest is the owner of the Document and will free it |
62 | /// on destruction. |
63 | |
64 | Document::Vector& documents(); |
65 | /// Returns the documents to insert into the database. |
66 | |
67 | protected: |
68 | void buildRequest(BinaryWriter& writer); |
69 | |
70 | private: |
71 | Int32 _flags; |
72 | std::string _fullCollectionName; |
73 | Document::Vector _documents; |
74 | }; |
75 | |
76 | |
77 | // |
78 | // inlines |
79 | // |
80 | inline Document& InsertRequest::addNewDocument() |
81 | { |
82 | Document::Ptr doc = new Document(); |
83 | _documents.push_back(doc); |
84 | return *doc; |
85 | } |
86 | |
87 | |
88 | inline Document::Vector& InsertRequest::documents() |
89 | { |
90 | return _documents; |
91 | } |
92 | |
93 | |
94 | } } // namespace Poco::MongoDB |
95 | |
96 | |
97 | #endif // MongoDB_InsertRequest_INCLUDED |
98 | |