- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathtable_crud.h
516 lines (391 loc) · 10.5 KB
/
table_crud.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0, as
* published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of Connector/C++, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* https://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef MYSQLX_TABLE_CRUD_H
#defineMYSQLX_TABLE_CRUD_H
/**
@file
Crud operations on tables.
Classes declared here represent CRUD operations on a table. They are
analogous to collection CRUD operation classes defined in collection_crud.h.
The following classes for table CRUD operations are defined:
- TableInsert
- TableRemove
- TableSelect
- TableUpdate
CRUD operation objects can be created directly, or assigned from
result of DevAPI methods that create such operations:
~~~~~~
TableInsert insert_op(table);
TableSelect select_op = table.select(...).orderBy(...);
~~~~~~
CRUD operation objects have methods which can modify the operation
before it gets executed. For example `TableInsert::values()`
appends a row to the list of rows that should be inserted into a table
by the given TableInsert operation. These methods can be chained
as allowed by the fluent API grammar.
*/
#include"common.h"
#include"result.h"
#include"executable.h"
#include"crud.h"
namespacemysqlx {
MYSQLX_ABI_BEGIN(2,0)
classTable;
// ---------------------------------------------------------------------------
classTableInsert;
namespaceinternal {
structTable_insert_base
: public Executable<Result, TableInsert>
{};
}
/**
An operation which inserts rows into a table.
This class defines methods that specify the rows to be inserted into
the table.
@todo Check that every row passed to .values() call has
the same number of values. The column count should match
the one in insert(c1,...) call. For insert() without column
list, it should match the number of columns in the table.
@ingroup devapi_op
*/
classTableInsert
: public internal::Table_insert_base
, internal::Table_insert_detail
{
protected:
template <class... Cols>
TableInsert(Table &table, const Cols&... cols)
: TableInsert(table)
{
add_columns(get_impl(), cols...);
}
public:
// Create operation which inserts rows into given table.
TableInsert(Table &table)
{
try {
reset(internal::Crud_factory::mk_insert(table));
}
CATCH_AND_WRAP
}
TableInsert(const internal::Table_insert_base &other)
{
internal::Table_insert_base::operator=(other);
}
TableInsert(internal::Table_insert_base &&other)
{
internal::Table_insert_base::operator=(std::move(other));
}
using internal::Table_insert_base::operator=;
/// Add the given row to the list of rows to be inserted.
virtual TableInsert& values(const Row &row)
{
try {
add_rows(get_impl(), row);
return *this;
}
CATCH_AND_WRAP
}
/**
Add a single row consisting of the specified values to the list of
rows to be inserted.
*/
template<typename... Types>
TableInsert& values(Types... rest)
{
try {
add_values(get_impl(), rest...);
return *this;
}
CATCH_AND_WRAP
}
/**
Add rows from a container such as vector or list.
*/
template<typename Container>
TableInsert& rows(const Container &cont)
{
try {
add_rows(get_impl(), cont);
return *this;
}
CATCH_AND_WRAP
}
/**
Add rows from a range given by two iterators.
*/
template<typename It>
TableInsert& rows(const It &begin, const It &end)
{
try {
add_rows(get_impl(), begin, end);
return *this;
}
CATCH_AND_WRAP
}
/**
Add the given list of rows.
*/
template<typename... Types>
TableInsert& rows(const Row &first, Types... rest)
{
try {
add_rows(get_impl(), first, rest...);
return *this;
}
CATCH_AND_WRAP
}
protected:
using Table_insert_detail::Impl;
Impl* get_impl()
{
returnstatic_cast<Impl*>(internal::Table_insert_base::get_impl());
}
///@cond IGNORED
friend Table;
///@endcond
};
// ---------------------------------------------------------------------------
classTableSelect;
namespaceinternal {
classOp_view_create_alter;
structTable_select_cmd
: public Executable<RowResult, TableSelect>
{};
structTable_select_base
: public Group_by < Having < Order_by < Limit < Offset< Bind_parameters<
Set_lock< Table_select_cmd, common::Table_select_if >
> > > > > >
{};
}
/**
An operation which selects rows from a table.
The class defines various methods, such as `where()`, to specify which rows
should be returned and in which order.
For each row the operation can return all fields from the
row or a set of values defined by projection expressions
specified when the operation was created.
@ingroup devapi_op
*/
classTableSelect
: public internal::Table_select_base
, internal::Table_select_detail
{
using Operation = Table_select_base;
public:
TableSelect(Table &table)
{
try {
reset(internal::Crud_factory::mk_select(table));
}
CATCH_AND_WRAP
}
template <typename...PROJ>
TableSelect(Table &table, const PROJ&... proj)
: TableSelect(table)
{
try {
add_proj(get_impl(), proj...);
}
CATCH_AND_WRAP
}
TableSelect(const internal::Table_select_cmd &other)
{
internal::Table_select_cmd::operator=(other);
}
TableSelect(internal::Table_select_cmd &&other)
{
internal::Table_select_cmd::operator=(std::move(other));
}
using internal::Table_select_cmd::operator=;
/**
Specify row selection criteria.
The criteria is specified as a Boolean expression string.
*/
Operation& where(const string& expr)
{
try {
get_impl()->set_where(expr);
return *this;
}
CATCH_AND_WRAP
}
protected:
using Impl = common::Table_select_if;
Impl* get_impl()
{
returnstatic_cast<Impl*>(internal::Table_select_base::get_impl());
}
///@cond IGNORED
friend Table;
friend internal::Op_view_create_alter;
///@endcond
};
// ---------------------------------------------------------------------------
classTableUpdate;
namespaceinternal {
structTable_update_cmd
: public Executable<Result, TableUpdate>
{};
structTable_update_base
: public Order_by< Limit< Bind_parameters< Table_update_cmd > > >
{};
}
/**
An operation which updates rows stored in a table.
Methods of this clas specify modifications to be applied to each row as well
as the set of rows that should be modified.
@ingroup devapi_op
*/
classTableUpdate
: public internal::Table_update_base
{
using Operation = internal::Table_update_base;
TableUpdate(Table& table)
{
try {
reset(internal::Crud_factory::mk_update(table));
}
CATCH_AND_WRAP
}
public:
TableUpdate(Table &table, const string &expr)
: TableUpdate(table)
{
where(expr);
}
TableUpdate(const internal::Table_update_cmd &other)
{
internal::Table_update_cmd::operator=(other);
}
TableUpdate(internal::Table_update_cmd &&other)
{
internal::Table_update_cmd::operator=(std::move(other));
}
using internal::Table_update_cmd::operator=;
/**
Set the given field in a row to the given value.
The value can be either a direct literal or an expression given
as `expr(<string>)`, to be evaluated in the server.
*/
TableUpdate& set(const string& field, const Value &val)
{
try {
get_impl()->add_set(field, (const common::Value&)val);
return *this;
}
CATCH_AND_WRAP
}
/**
Specify selection criteria for rows that should be updated.
*/
Operation& where(const string& expr)
{
try {
get_impl()->set_where(expr);
return *this;
}
CATCH_AND_WRAP
}
protected:
using Impl = common::Table_update_if;
Impl* get_impl()
{
returnstatic_cast<Impl*>(internal::Table_update_base::get_impl());
}
///@cond IGNORED
friend Table;
///@endcond
};
// ---------------------------------------------------------------------------
classTableRemove;
namespaceinternal {
structTable_remove_cmd
: public Executable<Result, TableRemove>
{};
structTable_remove_base
: Order_by< Limit< Bind_parameters< Table_remove_cmd > > >
{};
}
/**
An operation which removes rows from a table.
The class defines methods to specify which rows should be removed.
@ingroup devapi_op
*/
classTableRemove
: public internal::Table_remove_base
{
using Operation = internal::Table_remove_base;
TableRemove(Table& table)
{
try {
reset(internal::Crud_factory::mk_remove(table));
}
CATCH_AND_WRAP
}
public:
TableRemove(Table &table, const string &expr)
: TableRemove(table)
{
where(expr);
}
TableRemove(const internal::Table_remove_cmd &other)
{
internal::Table_remove_cmd::operator=(other);
}
TableRemove(internal::Table_remove_cmd &&other)
{
internal::Table_remove_cmd::operator=(std::move(other));
}
using internal::Table_remove_cmd::operator=;
/**
Specify selection criteria for rows to be removed.
*/
Operation& where(const string &expr)
{
try {
get_impl()->set_where(expr);
return *this;
}
CATCH_AND_WRAP
}
protected:
using Impl = common::Table_remove_if;
Impl* get_impl()
{
returnstatic_cast<Impl*>(internal::Table_remove_base::get_impl());
}
///@cond IGNORED
friend Table;
///@endcond
};
MYSQLX_ABI_END(2,0)
} // mysqlx
#endif