MySQL Connector/C++ 9.1.0
MySQL connector library for C and C++ applications
table_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
31#ifndef MYSQLX_TABLE_CRUD_H
32#define MYSQLX_TABLE_CRUD_H
33
62#include "common.h"
63#include "result.h"
64#include "executable.h"
65#include "crud.h"
66
67
68namespace mysqlx {
69MYSQLX_ABI_BEGIN(2,0)
70
71class Table;
72
73// ---------------------------------------------------------------------------
74
75class TableInsert;
76
77namespace internal {
78
79 struct Table_insert_base
80 : public Executable<Result, TableInsert>
81 {};
82
83}
84
100 : public internal::Table_insert_base
101 , internal::Table_insert_detail
102{
103
104protected:
105
106 template <class... Cols>
107 TableInsert(Table &table, const Cols&... cols)
108 : TableInsert(table)
109 {
110 add_columns(get_impl(), cols...);
111 }
112
113public:
114
115 // Create operation which inserts rows into given table.
116
117 TableInsert(Table &table)
118 {
119 try {
120 reset(internal::Crud_factory::mk_insert(table));
121 }
122 CATCH_AND_WRAP
123 }
124
125 TableInsert(const internal::Table_insert_base &other)
126 {
127 internal::Table_insert_base::operator=(other);
128 }
129
130 TableInsert(internal::Table_insert_base &&other)
131 {
132 internal::Table_insert_base::operator=(std::move(other));
133 }
134
135 using internal::Table_insert_base::operator=;
136
138
139 virtual TableInsert& values(const Row &row)
140 {
141 try {
142 add_rows(get_impl(), row);
143 return *this;
144 }
145 CATCH_AND_WRAP
146 }
147
153 template<typename... Types>
154 TableInsert& values(Types... rest)
155 {
156 try {
157 add_values(get_impl(), rest...);
158 return *this;
159 }
160 CATCH_AND_WRAP
161 }
162
167 template<typename Container>
168 TableInsert& rows(const Container &cont)
169 {
170 try {
171 add_rows(get_impl(), cont);
172 return *this;
173 }
174 CATCH_AND_WRAP
175 }
176
181 template<typename It>
182 TableInsert& rows(const It &begin, const It &end)
183 {
184 try {
185 add_rows(get_impl(), begin, end);
186 return *this;
187 }
188 CATCH_AND_WRAP
189 }
190
195 template<typename... Types>
196 TableInsert& rows(const Row &first, Types... rest)
197 {
198 try {
199 add_rows(get_impl(), first, rest...);
200 return *this;
201 }
202 CATCH_AND_WRAP
203 }
204
205protected:
206
207 using Table_insert_detail::Impl;
208
209 Impl* get_impl()
210 {
211 return static_cast<Impl*>(internal::Table_insert_base::get_impl());
212 }
213
215 friend Table;
217};
218
219
220// ---------------------------------------------------------------------------
221
222class TableSelect;
223
224namespace internal {
225
226 class Op_view_create_alter;
227
228 struct Table_select_cmd
229 : public Executable<RowResult, TableSelect>
230 {};
231
232 struct Table_select_base
233 : public Group_by < Having < Order_by < Limit < Offset< Bind_parameters<
234 Set_lock< Table_select_cmd, common::Table_select_if >
235 > > > > > >
236 {};
237
238}
239
240
255 : public internal::Table_select_base
256 , internal::Table_select_detail
257{
258
259 using Operation = Table_select_base;
260
261public:
262
263 TableSelect(Table &table)
264 {
265 try {
266 reset(internal::Crud_factory::mk_select(table));
267 }
268 CATCH_AND_WRAP
269 }
270
271 template <typename...PROJ>
272 TableSelect(Table &table, const PROJ&... proj)
273 : TableSelect(table)
274 {
275 try {
276 add_proj(get_impl(), proj...);
277 }
278 CATCH_AND_WRAP
279 }
280
281 TableSelect(const internal::Table_select_cmd &other)
282 {
283 internal::Table_select_cmd::operator=(other);
284 }
285
286 TableSelect(internal::Table_select_cmd &&other)
287 {
288 internal::Table_select_cmd::operator=(std::move(other));
289 }
290
291 using internal::Table_select_cmd::operator=;
292
299 Operation& where(const string& expr)
300 {
301 try {
302 get_impl()->set_where(expr);
303 return *this;
304 }
305 CATCH_AND_WRAP
306 }
307
308protected:
309
310 using Impl = common::Table_select_if;
311
312 Impl* get_impl()
313 {
314 return static_cast<Impl*>(internal::Table_select_base::get_impl());
315 }
316
318 friend Table;
319 friend internal::Op_view_create_alter;
321};
322
323
324// ---------------------------------------------------------------------------
325
326class TableUpdate;
327
328namespace internal {
329
330 struct Table_update_cmd
331 : public Executable<Result, TableUpdate>
332 {};
333
334 struct Table_update_base
335 : public Order_by< Limit< Bind_parameters< Table_update_cmd > > >
336 {};
337
338}
339
340
351 : public internal::Table_update_base
352{
353 using Operation = internal::Table_update_base;
354
355 TableUpdate(Table& table)
356 {
357 try {
358 reset(internal::Crud_factory::mk_update(table));
359 }
360 CATCH_AND_WRAP
361 }
362
363public:
364
365 TableUpdate(Table &table, const string &expr)
366 : TableUpdate(table)
367 {
368 where(expr);
369 }
370
371 TableUpdate(const internal::Table_update_cmd &other)
372 {
373 internal::Table_update_cmd::operator=(other);
374 }
375
376 TableUpdate(internal::Table_update_cmd &&other)
377 {
378 internal::Table_update_cmd::operator=(std::move(other));
379 }
380
381 using internal::Table_update_cmd::operator=;
382
390 TableUpdate& set(const string& field, const Value &val)
391 {
392 try {
393 get_impl()->add_set(field, (const common::Value&)val);
394 return *this;
395 }
396 CATCH_AND_WRAP
397 }
398
403 Operation& where(const string& expr)
404 {
405 try {
406 get_impl()->set_where(expr);
407 return *this;
408 }
409 CATCH_AND_WRAP
410 }
411
412protected:
413
414 using Impl = common::Table_update_if;
415
416 Impl* get_impl()
417 {
418 return static_cast<Impl*>(internal::Table_update_base::get_impl());
419 }
420
422 friend Table;
424};
425
426
427// ---------------------------------------------------------------------------
428
429class TableRemove;
430
431namespace internal {
432
433 struct Table_remove_cmd
434 : public Executable<Result, TableRemove>
435 {};
436
437 struct Table_remove_base
438 : Order_by< Limit< Bind_parameters< Table_remove_cmd > > >
439 {};
440
441}
442
443
453 : public internal::Table_remove_base
454{
455 using Operation = internal::Table_remove_base;
456
457 TableRemove(Table& table)
458 {
459 try {
460 reset(internal::Crud_factory::mk_remove(table));
461 }
462 CATCH_AND_WRAP
463 }
464
465public:
466
467 TableRemove(Table &table, const string &expr)
468 : TableRemove(table)
469 {
470 where(expr);
471 }
472
473 TableRemove(const internal::Table_remove_cmd &other)
474 {
475 internal::Table_remove_cmd::operator=(other);
476 }
477
478 TableRemove(internal::Table_remove_cmd &&other)
479 {
480 internal::Table_remove_cmd::operator=(std::move(other));
481 }
482
483 using internal::Table_remove_cmd::operator=;
484
489 Operation& where(const string &expr)
490 {
491 try {
492 get_impl()->set_where(expr);
493 return *this;
494 }
495 CATCH_AND_WRAP
496 }
497
498protected:
499
500 using Impl = common::Table_remove_if;
501
502 Impl* get_impl()
503 {
504 return static_cast<Impl*>(internal::Table_remove_base::get_impl());
505 }
506
508 friend Table;
510};
511
512
513MYSQLX_ABI_END(2,0)
514} // mysqlx
515
516#endif
Represents an operation that can be executed.
Definition: executable.h:68
Represents a single row from a result that contains rows.
Definition: row.h:72
An operation which inserts rows into a table.
Definition: table_crud.h:102
virtual TableInsert & values(const Row &row)
Add the given row to the list of rows to be inserted.
Definition: table_crud.h:139
TableInsert & values(Types... rest)
Add a single row consisting of the specified values to the list of rows to be inserted.
Definition: table_crud.h:154
TableInsert & rows(const Row &first, Types... rest)
Add the given list of rows.
Definition: table_crud.h:196
TableInsert & rows(const It &begin, const It &end)
Add rows from a range given by two iterators.
Definition: table_crud.h:182
TableInsert & rows(const Container &cont)
Add rows from a container such as vector or list.
Definition: table_crud.h:168
An operation which removes rows from a table.
Definition: table_crud.h:454
Operation & where(const string &expr)
Specify selection criteria for rows to be removed.
Definition: table_crud.h:489
An operation which selects rows from a table.
Definition: table_crud.h:257
Operation & where(const string &expr)
Specify row selection criteria.
Definition: table_crud.h:299
An operation which updates rows stored in a table.
Definition: table_crud.h:352
Operation & where(const string &expr)
Specify selection criteria for rows that should be updated.
Definition: table_crud.h:403
TableUpdate & set(const string &field, const Value &val)
Set the given field in a row to the given value.
Definition: table_crud.h:390
Represents a table in a schema.
Definition: xdevapi.h:1375
Value object can store value of scalar type, string, array or document.
Definition: document.h:230
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.