- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathcrud.cc
463 lines (370 loc) · 10.6 KB
/
crud.cc
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
/*
* 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
*/
#include<mysqlx/xdevapi.h>
PUSH_SYS_WARNINGS
#include<time.h>
#include<forward_list>
#include<list>
POP_SYS_WARNINGS
#include"impl.h"
usingnamespace ::mysqlx::impl::common;
usingnamespace ::mysqlx::internal;
usingnamespace ::mysqlx;
/*
Code in this file defines implementations for various CRUD operations used
by X DevAPI. We use common implementations of these operations.
*/
autoCrud_factory::mk_sql(Session &sess, const mysqlx::string &query)
-> Impl*
{
returnnewOp_sql(sess.m_impl, query);
}
// --------------------------------------------------------------------
/*
Collection CRUD operations
==========================
*/
autoCrud_factory::mk_add(Collection &coll) -> Impl*
{
returnnewOp_collection_add(
coll.get_session(), Object_ref(coll)
);
}
autoCrud_factory::mk_remove(
Collection &coll, const mysqlx::string &expr
) -> Impl*
{
returnnewOp_collection_remove(
coll.get_session(), Object_ref(coll), expr
);
}
autoCrud_factory::mk_find(Collection &coll) -> Impl*
{
returnnewOp_collection_find(
coll.get_session(), Object_ref(coll)
);
}
autoCrud_factory::mk_find(
Collection &coll, const mysqlx::string &expr
) -> Impl*
{
returnnewOp_collection_find(
coll.get_session(), Object_ref(coll), expr
);
}
autoCrud_factory::mk_modify(
Collection &coll, const mysqlx::string &expr
) -> Impl*
{
returnnewOp_collection_modify(
coll.get_session(), Object_ref(coll), expr
);
}
structReplace_cmd
: public Executable<Result, Replace_cmd>
{
Replace_cmd(
internal::Shared_session_impl sess,
const cdk::api::Object_ref &coll,
const std::string &id,
const cdk::Expression &doc
)
{
reset(newOp_collection_replace(
sess, coll, id, doc
));
}
};
structUpsert_cmd : publicExecutable<Result, Upsert_cmd>
{
Upsert_cmd(
internal::Shared_session_impl sess,
const cdk::api::Object_ref &coll,
const std::string &id,
cdk::Expression &doc
)
{
reset(newOp_collection_upsert(
sess, coll, id, doc
));
}
};
/*
A helper class used by Collection_detail::add_or_replace_one().
It is a wrapper around CDK expression m_expr that describes a document.
The wrapper forwards this description to a processor, but at the same
time checks if the value of the (top-level) "_id" field equals the value
given in the constructor.
*/
structValue_expr_check_id
: cdk::Expression
, cdk::Expression::Processor
, cdk::Expression::Processor::Doc_prc
{
mysqlx::Value_expr &m_expr;
bool m_is_expr;
Processor *m_prc;
Doc_prc *m_doc_prc;
/*
This class defines m_any_prc member which is used below
to check the value of "_id" field as reported by the
source expression. Before using this class, m_id_prc must be
set to point at the sub-processor that was given for processing
the value of the "_id" field (this is done by key_val() callback in
the main class).
Then all callbacks are forwarded to this sub-processor (or its
sub-processors) and in case of calling scalar str() callback that
gives string value of the "_id" field, the check is done first.
*/
structAny_processor_check
: cdk::Expression::Processor::Doc_prc::Any_prc
, cdk::Expression::Processor::Doc_prc::Any_prc::Scalar_prc
, cdk::Expression::Processor::Doc_prc::Any_prc::Scalar_prc::Value_prc
{
Any_prc *m_id_prc;
Scalar_prc *m_scalar_prc;
Value_prc *m_value_prc;
const std::string &m_id;
Any_processor_check(const std::string& id)
: m_id(id)
{}
// Any processor implementation
Scalar_prc* scalar() override
{
m_scalar_prc = m_id_prc->scalar();
return m_scalar_prc ? this : nullptr;
}
List_prc* arr() override
{
return m_id_prc->arr();
}
Doc_prc* doc() override
{
return m_id_prc->doc();
}
//Scalar processor implementation
Value_prc* val() override
{
m_value_prc = m_scalar_prc->val();
return m_value_prc ? this : nullptr;
}
Args_prc* op(constchar *name) override
{
return m_scalar_prc->op(name);
}
Args_prc* call(const Object_ref&obj) override
{
return m_scalar_prc->call(obj);
}
voidref(const Column_ref &col, const Doc_path *path) override
{
return m_scalar_prc->ref(col, path);
}
voidref(const Doc_path &path) override
{
return m_scalar_prc->ref(path);
}
voidparam(const string &val) override
{
return m_scalar_prc->param(val);
}
voidparam(uint16_t val) override
{
return m_scalar_prc->param(val);
}
voidvar(const string &name) override
{
m_scalar_prc->var(name);
}
// Value processor implementation
voidnull() override { m_value_prc->null();}
voidvalue(cdk::Type_info type,
const cdk::Format_info &format,
cdk::foundation::bytes val) override
{
m_value_prc->value(type, format, val);
}
voidstr(const string &val) override
{
if (m_id != val)
throwmysqlx::Error(R"(Replacement document has an _id that is different than the matched document.)");
m_value_prc->str(val);
}
voidnum(int64_t val) override { m_value_prc->num(val); }
voidnum(uint64_t val) override { m_value_prc->num(val); }
voidnum(float val) override { m_value_prc->num(val); }
voidnum(double val) override { m_value_prc->num(val); }
voidyesno(bool val) override { m_value_prc->yesno(val); }
};
Any_processor_check m_any_prc;
Value_expr_check_id(mysqlx::Value_expr &expr, bool is_expr, const std::string& id)
: m_expr(expr)
, m_is_expr(is_expr)
, m_any_prc(id)
{}
// Expression implementation
voidprocess(Processor& prc) constoverride
{
auto self = const_cast<Value_expr_check_id*>(this);
self->m_prc = &prc;
m_expr.process(*self);
}
// Expression processor implementation
Scalar_prc* scalar() override
{
return m_prc->scalar();
}
List_prc* arr() override
{
return m_prc->arr();
}
Doc_prc* doc() override
{
m_doc_prc = m_prc->doc();
return m_doc_prc ? this : nullptr;
}
// Doc_prc implementation
voiddoc_begin() override
{
m_doc_prc->doc_begin();
}
voiddoc_end() override
{
m_doc_prc->doc_end();
}
Any_prc* key_val(const string &key) override
{
if (string("_id") == key )
{
if (m_is_expr)
mysqlx::throw_error(
R"(Document "_id" will be replaced by expression "_id")"
);
m_any_prc.m_id_prc = m_doc_prc->key_val(key);
return m_any_prc.m_id_prc ? &m_any_prc : nullptr;
}
return m_doc_prc->key_val(key);
}
};
Result
Collection_detail::add_or_replace_one(
const mysqlx::string &id, mysqlx::Value &&doc, bool replace
)
{
/*
This is implemented by executing Replace_cmd or Upsert_command
which internally use Op_collection_replace or Op_collection_upsert
to perform relevant operation on the server.
*/
Object_ref coll(get_schema().m_name, m_name);
std::string id_str(id);
if (!Value::Access::is_expr(doc) &&
doc.getType() == Value::STRING)
{
doc = DbDoc(doc.get<string>());
}
/*
expr is a CDK expression object which describes the document
to be added.
*/
Value_expr expr(doc, parser::Parser_mode::DOCUMENT);
if (replace)
{
/*
Replace_cmd executes Op_collection_replace which picks a document
with the given id and replaes it with the document given as the last
argument.
The document expression is wrapped in Value_expr_check_id to check
if the "_id" field (if present) stores the correct document id
and throws error if it is not the case (otherwise Replace_cmd
would modify the "_id" field to match the given id).
*/
Value_expr_check_id check_id(expr, Value::Access::is_expr(doc), id_str);
Replace_cmd cmd(m_sess, coll, id_str, check_id);
return cmd.execute();
}
else
{
Upsert_cmd cmd(m_sess, coll, std::string(id), expr);
return cmd.execute();
}
}
voidCollection_detail::index_drop(const mysqlx::string &name)
{
Object_ref coll(get_schema().m_name, m_name);
Op_idx_drop cmd(m_sess, coll, name);
cmd.execute();
}
void
Collection_detail::index_create(
const mysqlx::string &name, mysqlx::Value &&spec
)
{
switch (spec.getType())
{
case Value::STRING:
break;
default:
// TODO: support other forms: DbDoc, expr("{...}")?
throw_error("Index specification must be a string.");
}
Object_ref coll(get_schema().m_name, m_name);
Op_idx_create cmd(m_sess, coll, name, (std::string)spec);
cmd.execute();
}
// --------------------------------------------------------------------
/*
Table CRUD operations
=====================
*/
autoCrud_factory::mk_insert(Table &table) -> Impl*
{
returnnew Op_table_insert<Value>(
table.get_session(), Object_ref(table)
);
}
autoCrud_factory::mk_select(Table &table) -> Impl*
{
returnnewOp_table_select(
table.get_session(), Object_ref(table)
);
}
autoCrud_factory::mk_update(Table &table) -> Impl*
{
returnnewOp_table_update(
table.get_session(), Object_ref(table)
);
}
autoCrud_factory::mk_remove(Table &table) -> Impl*
{
returnnewOp_table_remove(
table.get_session(), Object_ref(table)
);
}