MySQL Connector/C++ 9.1.0
MySQL connector library for C and C++ applications
collection_crud.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2024, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0, as
6 * published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms, as
10 * designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * Without limiting anything contained in the foregoing, this file,
17 * which is part of Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * https://oss.oracle.com/licenses/universal-foss-exception.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License, version 2.0, for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30#ifndef MYSQLX_COLLECTION_CRUD_H
31#define MYSQLX_COLLECTION_CRUD_H
32
71#include "common.h"
72#include "result.h"
73#include "executable.h"
74#include "crud.h"
75
76
77namespace mysqlx {
78MYSQLX_ABI_BEGIN(2,0)
79
80class Session;
81class Collection;
82class Table;
83
84template<>
85PUBLIC_API common::Value Value::get<common::Value>() const;
86
87// ----------------------------------------------------------------------
88
89/*
90 Adding documents to a collection
91 ================================
92*/
93
94class CollectionAdd;
95
96namespace internal {
97
98/*
99 Note: using empty class instead of alias/typedef to help Doxygen correctly
100 expand templates.
101*/
102
103struct Collection_add_base
104 : public Executable<Result, CollectionAdd>
105{};
106
107}
108
109
136 : public internal::Collection_add_base
137 , internal::Collection_add_detail
138{
139public:
140
146 {
147 try {
148 reset(internal::Crud_factory::mk_add(coll));
149 }
150 CATCH_AND_WRAP
151 }
152
153 CollectionAdd(const internal::Collection_add_base &other)
154 {
155 internal::Collection_add_base::operator=(other);
156 }
157
158 CollectionAdd(internal::Collection_add_base &&other)
159 {
160 internal::Collection_add_base::operator=(std::move(other));
161 }
162
163 using internal::Collection_add_base::operator=;
164
165
170 template <typename It>
171 CollectionAdd& add(const It &begin, const It &end)
172 {
173 try {
174 do_add(get_impl(), begin, end);
175 return *this;
176 }
177 CATCH_AND_WRAP
178 }
179
187 template <class Container>
188 CollectionAdd& add(const Container &c)
189 {
190 try {
191 do_add(get_impl(), c);
192 return *this;
193 }
194 CATCH_AND_WRAP
195 }
196
203 template <typename... Types>
204 CollectionAdd& add(const Types&... docs)
205 {
206 try {
207 do_add(get_impl(), docs...);
208 return *this;
209 }
210 CATCH_AND_WRAP
211 }
212
213protected:
214
215 using Impl = common::Collection_add_if;
216
217 Impl* get_impl()
218 {
219 return static_cast<Impl*>(internal::Collection_add_base::get_impl());
220 }
221
222};
223
224
225// ----------------------------------------------------------------------
226
227
228/*
229 Removing documents from a collection
230 ====================================
231*/
232
233class CollectionRemove;
234
235namespace internal {
236
237struct Collection_remove_cmd
238 : public Executable<Result, CollectionRemove>
239{};
240
241struct Collection_remove_base
242 : public Sort< Limit< Bind_parameters< Collection_remove_cmd > > >
243{};
244
245} // internal namespace
246
247
259 : public internal::Collection_remove_base
260{
261
262public:
263
271 CollectionRemove(Collection &coll, const string &expr)
272 {
273 try {
274 reset(internal::Crud_factory::mk_remove(coll, expr));
275 }
276 CATCH_AND_WRAP
277 }
278
279
280 CollectionRemove(const internal::Collection_remove_cmd &other)
281 {
282 internal::Collection_remove_cmd::operator=(other);
283 }
284
285 CollectionRemove(internal::Collection_remove_cmd &&other)
286 {
287 internal::Collection_remove_cmd::operator=(std::move(other));
288 }
289
290 using internal::Collection_remove_cmd::operator=;
291
292};
293
294
295// ----------------------------------------------------------------------
296
297/*
298 Searching for documents in a collection
299 =======================================
300*/
301
302class CollectionFind;
303
304namespace internal {
305
306struct Collection_find_cmd
307 : public Executable<DocResult, CollectionFind>
308{};
309
310struct Collection_find_base
311 : public Group_by< Having< Sort< Limit< Offset< Bind_parameters<
312 Set_lock< Collection_find_cmd, common::Collection_find_if >
313 > > > > > >
314{};
315
316} // internal namespace
317
318
326 : public internal::Collection_find_base
327 , internal::Collection_find_detail
328{
329
330 using Operation = internal::Collection_find_base;
331
332public:
333
339 {
340 try {
341 reset(internal::Crud_factory::mk_find(coll));
342 }
343 CATCH_AND_WRAP
344 }
345
353 CollectionFind(Collection &coll, const string &expr)
354 {
355 try {
356 reset(internal::Crud_factory::mk_find(coll, expr));
357 }
358 CATCH_AND_WRAP
359 }
360
361
362 CollectionFind(const internal::Collection_find_cmd &other)
363 {
364 internal::Collection_find_cmd::operator=(other);
365 }
366
367 CollectionFind(internal::Collection_find_cmd &&other)
368 {
369 internal::Collection_find_cmd::operator=(std::move(other));
370 }
371
372 using internal::Collection_find_cmd::operator=;
373
374
375public:
376
388 template <typename... Expr>
389 Operation& fields(Expr... proj)
390 {
391 try {
392 get_impl()->clear_proj();
393 do_fields(get_impl(), proj...);
394 return *this;
395 }
396 CATCH_AND_WRAP
397 }
398
399protected:
400
401 using Impl = common::Collection_find_if;
402
403 Impl* get_impl()
404 {
405 return static_cast<Impl*>(internal::Collection_find_base::get_impl());
406 }
407
408};
409
410
411// ----------------------------------------------------------------------
412
413/*
414 Modifying documents in a collection
415 ===================================
416*/
417
418class CollectionModify;
419
420
421namespace internal {
422
423class CollectionReplace;
424
425struct Collection_modify_cmd
426 : public Executable<Result, CollectionModify>
427{};
428
429struct Collection_modify_base
430 : public Sort< Limit< Bind_parameters< Collection_modify_cmd > > >
431{};
432
433} // internal namespace
434
435
461 : public internal::Collection_modify_base
462{
463
464public:
465
473 CollectionModify(Collection &coll, const string &expr)
474 {
475 try {
476 reset(internal::Crud_factory::mk_modify(coll, expr));
477 }
478 CATCH_AND_WRAP
479 }
480
481
482 CollectionModify(const internal::Collection_modify_cmd &other)
483 {
484 internal::Collection_modify_cmd::operator=(other);
485 }
486
487 CollectionModify(internal::Collection_modify_cmd &&other)
488 {
489 internal::Collection_modify_cmd::operator=(std::move(other));
490 }
491
492 using internal::Collection_modify_cmd::operator=;
493
502 CollectionModify& set(const Field &field, const Value &val)
503 {
504 try {
505 get_impl()->add_operation(Impl::SET, field,
506 val.get<common::Value>());
507 return *this;
508 }
509 CATCH_AND_WRAP
510 }
511
518 CollectionModify& unset(const Field &field)
519 {
520 try {
521 get_impl()->add_operation(Impl::UNSET, field);
522 return *this;
523 }
524 CATCH_AND_WRAP
525 }
526
534 CollectionModify& arrayInsert(const Field &field, const Value &val)
535 {
536 try {
537 get_impl()->add_operation(Impl::ARRAY_INSERT, field,
538 val.get<common::Value>());
539 return *this;
540 }
541 CATCH_AND_WRAP
542 }
543
552 CollectionModify& arrayAppend(const Field &field, const Value &val)
553 {
554 try {
555 get_impl()->add_operation(Impl::ARRAY_APPEND, field,
556 val.get<common::Value>());
557 return *this;
558 }
559 CATCH_AND_WRAP
560 }
561
580 CollectionModify& patch(const string &val)
581 {
582 try {
583 get_impl()->add_operation(
584 Impl::MERGE_PATCH, "$", (const common::Value&)expr(val)
585 );
586 return *this;
587 }
588 CATCH_AND_WRAP
589 }
590
591protected:
592
593 using Impl = common::Collection_modify_if;
594
595 Impl* get_impl()
596 {
597 return static_cast<Impl*>(internal::Collection_modify_base::get_impl());
598 }
599
600};
601
602MYSQLX_ABI_END(2,0)
603} // mysqlx namespace
604
605#endif
An operation which adds documents to a collection.
Definition: collection_crud.h:138
CollectionAdd & add(const Types &... docs)
Add document(s) to a collection.
Definition: collection_crud.h:204
CollectionAdd & add(const Container &c)
Add all documents within given container.
Definition: collection_crud.h:188
CollectionAdd(Collection &coll)
Create an empty add operation for the given collection.
Definition: collection_crud.h:145
CollectionAdd & add(const It &begin, const It &end)
Add all documents from a range defined by two iterators.
Definition: collection_crud.h:171
An operation which returns all or selected documents from a collection.
Definition: collection_crud.h:328
CollectionFind(Collection &coll)
Create an operation which returns all documents from the given collection.
Definition: collection_crud.h:338
CollectionFind(Collection &coll, const string &expr)
Create an operation which returns selected documents from the given collection.
Definition: collection_crud.h:353
Operation & fields(Expr... proj)
Specify a projection for the documents returned by this operation.
Definition: collection_crud.h:389
An operation which modifies all or selected documents in a collection.
Definition: collection_crud.h:462
CollectionModify & unset(const Field &field)
Remove the given field from a document.
Definition: collection_crud.h:518
CollectionModify & set(const Field &field, const Value &val)
Set the given field in a document to the given value.
Definition: collection_crud.h:502
CollectionModify & arrayAppend(const Field &field, const Value &val)
Append a value to an array field of a document.
Definition: collection_crud.h:552
CollectionModify(Collection &coll, const string &expr)
Create an operation which modifies selected documents in the given collection.
Definition: collection_crud.h:473
CollectionModify & arrayInsert(const Field &field, const Value &val)
Insert a value into an array field of a document.
Definition: collection_crud.h:534
CollectionModify & patch(const string &val)
Apply JSON Patch to a target JSON document.
Definition: collection_crud.h:580
An operation which removes documents from a collection.
Definition: collection_crud.h:260
CollectionRemove(Collection &coll, const string &expr)
Create an operation which removes selected documnets from the given collection.
Definition: collection_crud.h:271
Represents a collection of documents in a schema.
Definition: xdevapi.h:912
Represents an operation that can be executed.
Definition: executable.h:68
Value object can store value of scalar type, string, array or document.
Definition: document.h:230
T get() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Details for public API classes representing CRUD operations.
Class representing executable statements.
internal::Expression expr(std::string &&e)
Function which indicates that a given string should be treated as expression.
Definition: document.h:638
Classes used to access query and command execution results.