MySQL Connector/C++
MySQL connector library for C and C++ applications
Loading...
Searching...
No Matches
collection_crud.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2023, 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 also distributed with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an
12 * additional permission to link the program and your derivative works
13 * with the separately licensed software that they have included with
14 * MySQL.
15 *
16 * Without limiting anything contained in the foregoing, this file,
17 * which is part of MySQL Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * http://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
31#ifndef MYSQLX_COLLECTION_CRUD_H
32#define MYSQLX_COLLECTION_CRUD_H
33
72#include "common.h"
73#include "result.h"
74#include "executable.h"
75#include "crud.h"
76
77
78namespace mysqlx {
79MYSQLX_ABI_BEGIN(2,0)
80
81class Session;
82class Collection;
83class Table;
84
85template<>
86common::Value Value::get<common::Value>() const;
87
88// ----------------------------------------------------------------------
89
90/*
91 Adding documents to a collection
92 ================================
93*/
94
95class CollectionAdd;
96
97namespace internal {
98
99/*
100 Note: using empty class instead of alias/typedef to help Doxygen correctly
101 expand templates.
102*/
103
104struct Collection_add_base
105 : public Executable<Result, CollectionAdd>
106{};
107
108}
109
110
137 : public internal::Collection_add_base
138 , internal::Collection_add_detail
139{
140public:
141
147 {
148 try {
149 reset(internal::Crud_factory::mk_add(coll));
150 }
151 CATCH_AND_WRAP
152 }
153
154 CollectionAdd(const internal::Collection_add_base &other)
155 {
156 internal::Collection_add_base::operator=(other);
157 }
158
159 CollectionAdd(internal::Collection_add_base &&other)
160 {
161 internal::Collection_add_base::operator=(std::move(other));
162 }
163
164 using internal::Collection_add_base::operator=;
165
166
171 template <typename It>
172 CollectionAdd& add(const It &begin, const It &end)
173 {
174 try {
175 do_add(get_impl(), begin, end);
176 return *this;
177 }
178 CATCH_AND_WRAP
179 }
180
188 template <class Container>
189 CollectionAdd& add(const Container &c)
190 {
191 try {
192 do_add(get_impl(), c);
193 return *this;
194 }
195 CATCH_AND_WRAP
196 }
197
204 template <typename... Types>
205 CollectionAdd& add(const Types&... docs)
206 {
207 try {
208 do_add(get_impl(), docs...);
209 return *this;
210 }
211 CATCH_AND_WRAP
212 }
213
214protected:
215
216 using Impl = common::Collection_add_if;
217
218 Impl* get_impl()
219 {
220 return static_cast<Impl*>(internal::Collection_add_base::get_impl());
221 }
222
223};
224
225
226// ----------------------------------------------------------------------
227
228
229/*
230 Removing documents from a collection
231 ====================================
232*/
233
234class CollectionRemove;
235
236namespace internal {
237
238struct Collection_remove_cmd
239 : public Executable<Result, CollectionRemove>
240{};
241
242struct Collection_remove_base
243 : public Sort< Limit< Bind_parameters< Collection_remove_cmd > > >
244{};
245
246} // internal namespace
247
248
260 : public internal::Collection_remove_base
261{
262
263public:
264
272 CollectionRemove(Collection &coll, const string &expr)
273 {
274 try {
275 reset(internal::Crud_factory::mk_remove(coll, expr));
276 }
277 CATCH_AND_WRAP
278 }
279
280
281 CollectionRemove(const internal::Collection_remove_cmd &other)
282 {
283 internal::Collection_remove_cmd::operator=(other);
284 }
285
286 CollectionRemove(internal::Collection_remove_cmd &&other)
287 {
288 internal::Collection_remove_cmd::operator=(std::move(other));
289 }
290
291 using internal::Collection_remove_cmd::operator=;
292
293};
294
295
296// ----------------------------------------------------------------------
297
298/*
299 Searching for documents in a collection
300 =======================================
301*/
302
303class CollectionFind;
304
305namespace internal {
306
307struct Collection_find_cmd
308 : public Executable<DocResult, CollectionFind>
309{};
310
311struct Collection_find_base
312 : public Group_by< Having< Sort< Limit< Offset< Bind_parameters<
313 Set_lock< Collection_find_cmd, common::Collection_find_if >
314 > > > > > >
315{};
316
317} // internal namespace
318
319
327 : public internal::Collection_find_base
328 , internal::Collection_find_detail
329{
330
331 using Operation = internal::Collection_find_base;
332
333public:
334
340 {
341 try {
342 reset(internal::Crud_factory::mk_find(coll));
343 }
344 CATCH_AND_WRAP
345 }
346
354 CollectionFind(Collection &coll, const string &expr)
355 {
356 try {
357 reset(internal::Crud_factory::mk_find(coll, expr));
358 }
359 CATCH_AND_WRAP
360 }
361
362
363 CollectionFind(const internal::Collection_find_cmd &other)
364 {
365 internal::Collection_find_cmd::operator=(other);
366 }
367
368 CollectionFind(internal::Collection_find_cmd &&other)
369 {
370 internal::Collection_find_cmd::operator=(std::move(other));
371 }
372
373 using internal::Collection_find_cmd::operator=;
374
375
376public:
377
389 template <typename... Expr>
390 Operation& fields(Expr... proj)
391 {
392 try {
393 get_impl()->clear_proj();
394 do_fields(get_impl(), proj...);
395 return *this;
396 }
397 CATCH_AND_WRAP
398 }
399
400protected:
401
402 using Impl = common::Collection_find_if;
403
404 Impl* get_impl()
405 {
406 return static_cast<Impl*>(internal::Collection_find_base::get_impl());
407 }
408
409};
410
411
412// ----------------------------------------------------------------------
413
414/*
415 Modifying documents in a collection
416 ===================================
417*/
418
419class CollectionModify;
420
421
422namespace internal {
423
424class CollectionReplace;
425
426struct Collection_modify_cmd
427 : public Executable<Result, CollectionModify>
428{};
429
430struct Collection_modify_base
431 : public Sort< Limit< Bind_parameters< Collection_modify_cmd > > >
432{};
433
434} // internal namespace
435
436
462 : public internal::Collection_modify_base
463{
464
465public:
466
474 CollectionModify(Collection &coll, const string &expr)
475 {
476 try {
477 reset(internal::Crud_factory::mk_modify(coll, expr));
478 }
479 CATCH_AND_WRAP
480 }
481
482
483 CollectionModify(const internal::Collection_modify_cmd &other)
484 {
485 internal::Collection_modify_cmd::operator=(other);
486 }
487
488 CollectionModify(internal::Collection_modify_cmd &&other)
489 {
490 internal::Collection_modify_cmd::operator=(std::move(other));
491 }
492
493 using internal::Collection_modify_cmd::operator=;
494
503 CollectionModify& set(const Field &field, const Value &val)
504 {
505 try {
506 get_impl()->add_operation(Impl::SET, field,
507 val.get<common::Value>());
508 return *this;
509 }
510 CATCH_AND_WRAP
511 }
512
519 CollectionModify& unset(const Field &field)
520 {
521 try {
522 get_impl()->add_operation(Impl::UNSET, field);
523 return *this;
524 }
525 CATCH_AND_WRAP
526 }
527
535 CollectionModify& arrayInsert(const Field &field, const Value &val)
536 {
537 try {
538 get_impl()->add_operation(Impl::ARRAY_INSERT, field,
539 val.get<common::Value>());
540 return *this;
541 }
542 CATCH_AND_WRAP
543 }
544
553 CollectionModify& arrayAppend(const Field &field, const Value &val)
554 {
555 try {
556 get_impl()->add_operation(Impl::ARRAY_APPEND, field,
557 val.get<common::Value>());
558 return *this;
559 }
560 CATCH_AND_WRAP
561 }
562
581 CollectionModify& patch(const string &val)
582 {
583 try {
584 get_impl()->add_operation(
585 Impl::MERGE_PATCH, "$", (const common::Value&)expr(val)
586 );
587 return *this;
588 }
589 CATCH_AND_WRAP
590 }
591
592protected:
593
594 using Impl = common::Collection_modify_if;
595
596 Impl* get_impl()
597 {
598 return static_cast<Impl*>(internal::Collection_modify_base::get_impl());
599 }
600
601};
602
603MYSQLX_ABI_END(2,0)
604} // mysqlx namespace
605
606#endif
An operation which adds documents to a collection.
Definition: collection_crud.h:139
CollectionAdd & add(const Types &... docs)
Add document(s) to a collection.
Definition: collection_crud.h:205
CollectionAdd & add(const Container &c)
Add all documents within given container.
Definition: collection_crud.h:189
CollectionAdd(Collection &coll)
Create an empty add operation for the given collection.
Definition: collection_crud.h:146
CollectionAdd & add(const It &begin, const It &end)
Add all documents from a range defined by two iterators.
Definition: collection_crud.h:172
An operation which returns all or selected documents from a collection.
Definition: collection_crud.h:329
CollectionFind(Collection &coll)
Create an operation which returns all documents from the given collection.
Definition: collection_crud.h:339
CollectionFind(Collection &coll, const string &expr)
Create an operation which returns selected documents from the given collection.
Definition: collection_crud.h:354
Operation & fields(Expr... proj)
Specify a projection for the documents returned by this operation.
Definition: collection_crud.h:390
Represents a collection of documents in a schema.
Definition: xdevapi.h:908
An operation which modifies all or selected documents in a collection.
Definition: collection_crud.h:463
CollectionModify & unset(const Field &field)
Remove the given field from a document.
Definition: collection_crud.h:519
CollectionModify & set(const Field &field, const Value &val)
Set the given field in a document to the given value.
Definition: collection_crud.h:503
CollectionModify & arrayAppend(const Field &field, const Value &val)
Append a value to an array field of a document.
Definition: collection_crud.h:553
CollectionModify(Collection &coll, const string &expr)
Create an operation which modifies selected documents in the given collection.
Definition: collection_crud.h:474
CollectionModify & arrayInsert(const Field &field, const Value &val)
Insert a value into an array field of a document.
Definition: collection_crud.h:535
CollectionModify & patch(const string &val)
Apply JSON Patch to a target JSON document.
Definition: collection_crud.h:581
An operation which removes documents from a collection.
Definition: collection_crud.h:261
CollectionRemove(Collection &coll, const string &expr)
Create an operation which removes selected documnets from the given collection.
Definition: collection_crud.h:272
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:228
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.
Classes used to access query and command execution results.