- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathmysqlx_cc_internal.h
542 lines (421 loc) · 12.3 KB
/
mysqlx_cc_internal.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Copyright (c) 2016, 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_CC_INTERNAL_H__
#define__MYSQLX_CC_INTERNAL_H__
#include<mysql/cdk.h>
#include"../common/session.h"
#include"../common/settings.h"
#include"../common/db_object.h"
#include<cstdarg>
#include"def_internal.h"
#include"error_internal.h"
#include"crud_internal.h"
using Db_obj_ref = ::mysqlx::impl::common::Object_ref;
classDiag_info_list;
structmysqlx_session_options_struct
: public Mysqlx_diag
, Settings_impl
{
public:
using Settings_impl::Session_option_impl;
using Settings_impl::Client_option_impl;
mysqlx_session_options_struct() = default;
mysqlx_session_options_struct(const std::string host, unsignedshort port,
const std::string usr, const std::string *pwd,
const std::string *db,
unsignedint ssl_mode =
#ifdef WITH_SSL
SSL_MODE_REQUIRED
#else
SSL_MODE_DISABLED
#endif
)
{
Setter set(*this);
set.host(0, host, port);
set.user(usr);
if (pwd)
set.password(*pwd);
if (db)
set.schema(*db);
#ifdef WITH_SSL
set.key_val(Session_option_impl::SSL_MODE)->scalar()->num(uint64_t(ssl_mode));
#endif
set.commit();
}
mysqlx_session_options_struct(const std::string &conn_str)
{
set_from_uri(conn_str);
}
using Settings_impl::get;
boolhas_option(mysqlx_opt_type_enum opt)
{
// Note: assumes the same enum values as used in Settings_impl::Option
returnSettings_impl::has_option(Client_option_impl(opt));
}
voidclear()
{
Mysqlx_diag::clear();
Settings_impl::clear();
}
friend mysqlx_session_struct;
friendintmysqlx_session_option_set(mysqlx_session_options_struct*, ...);
};
template <classhandle_t>
classDb_obj_cache
{
usingmap_t = std::map<cdk::string, handle_t>;
map_t m_storage;
public:
template <typename T>
handle_t* get(T &parent, constchar *name)
{
cdk::string obj_name(name);
typenamemap_t::iterator it = m_storage.find(obj_name);
if (it == m_storage.end())
it = m_storage.emplace(obj_name, handle_t(parent, obj_name)).first;
return &it->second;
}
};
// TODO: Do not store entries for non-existing objects (if check was made)
template <classhandle_t>
inline
handle_t* check_existence(bool check, handle_t *obj)
{
assert(obj);
if (check && !obj->exists())
throwMysqlx_exception("Database object does not exist");
return obj;
}
structmysqlx_session_struct
: public Mysqlx_diag
{
using Stmt_cache = std::forward_list<mysqlx_stmt_struct>;
std::string m_savepoint_name;
public:
std::shared_ptr<Session_impl> m_impl;
Stmt_cache m_stmts;
Db_obj_cache<mysqlx_schema_struct> m_schema_map;
/*
Create new statement. This session takes its ownership.
*/
template <mysqlx_op_t OP, typename... T>
mysqlx_stmt_struct* new_stmt(const T&... args)
{
using Impl = typename stmt_traits<OP>::Impl;
Impl *impl = newImpl(m_impl, args...);
m_stmts.emplace_front(this, OP, impl);
return &m_stmts.front();
}
voidrm_stmt(const mysqlx_stmt_struct *ptr)
{
m_stmts.remove_if(
[ptr](const mysqlx_stmt_struct &stmt) { return &stmt == ptr; }
);
}
public:
mysqlx_session_struct( mysqlx_client_t *cli);
mysqlx_session_struct(
const std::string &host, unsignedshort port,
const std::string &usr, const std::string *pwd,
const std::string *db
);
mysqlx_session_struct(const std::string &conn_str);
mysqlx_session_struct(mysqlx_session_options_struct *opt);
mysqlx_session_struct(mysqlx_session_options_struct &&opt)
: mysqlx_session_struct(&opt)
{}
Session_impl& get_impl()
{
assert(m_impl);
return *m_impl.get();
}
boolis_valid() {
returnget_impl().m_sess->is_valid() == cdk::option_t::YES;
}
const cdk::Error* get_cdk_error();
cdk::Session &get_session() { return *(m_impl->m_sess); }
/*
Execute a plain SQL query (supports parameters and placeholders)
PARAMETERS:
query - SQL query
length - length of the query
RETURN:
CRUD handler containing the results and/or error
*/
mysqlx_stmt_t *sql_query(constchar *query, uint32_t length);
voidreset_diagnostic();
mysqlx_schema_struct* get_schema(constchar *name, bool check)
{
assert(name && *name);
returncheck_existence(check, m_schema_map.get(*this, name));
}
mysqlx_result_struct* get_schemas(constchar *pattern_utf8 = nullptr)
{
cdk::string pattern(pattern_utf8 ? pattern_utf8 : "%");
return new_stmt<OP_LIST_SCHEMAS>(pattern)->exec();
}
voidcreate_schema(constchar *name)
{
assert(name && *name);
Schema_ref schema(name);
create_object<Object_type::SCHEMA>(m_impl, schema);
}
voiddrop_schema(constchar *name)
{
assert(name && *name);
Schema_ref schema(name);
drop_object<Object_type::SCHEMA>(m_impl, schema);
}
voidtransaction_begin();
voidtransaction_commit();
voidtransaction_rollback(constchar *sp);
constchar * savepoint_set(constchar *);
voidsavepoint_remove(constchar *);
mysqlx_error_t *get_last_error();
friend mysqlx_result_struct;
friend mysqlx_stmt_struct;
};
/*
Client object implementation
*/
structmysqlx_client_struct
: public Mysqlx_diag
{
std::shared_ptr<Session_pool> m_impl;
mysqlx_client_struct(constchar *conn_str, constchar *client_opt);
mysqlx_client_struct(mysqlx_session_options_t *opt);
std::shared_ptr<Session_pool>& get_impl()
{
return m_impl;
}
};
inline
boolmysqlx_stmt_struct::session_valid()
{
return m_session.is_valid();
}
structmysqlx_schema_struct
: public Mysqlx_diag
, public Schema_ref
{
private:
Db_obj_cache<mysqlx_collection_struct> m_collection_map;
Db_obj_cache<mysqlx_table_struct> m_table_map;
mysqlx_session_struct &m_session;
public:
mysqlx_schema_struct(mysqlx_session_struct &session, cdk::string name)
: Schema_ref(name)
, m_session(session)
{}
boolexists()
{
returncheck_schema_exists(m_session.m_impl, *this);
}
mysqlx_session_struct& get_session() { return m_session; }
mysqlx_collection_struct* get_collection(constchar *name, bool check)
{
assert(name && *name);
returncheck_existence(check, m_collection_map.get(*this, name));
}
mysqlx_table_struct* get_table(constchar *name, bool check)
{
assert(name && *name);
returncheck_existence(check, m_table_map.get(*this, name));
}
mysqlx_result_struct* get_tables(constchar *pattern_utf8, bool include_views)
{
cdk::string pattern(pattern_utf8 ? pattern_utf8 : "%");
return m_session.new_stmt<OP_LIST_TABLES>(
*this, pattern, include_views
)->exec();
}
mysqlx_result_struct* get_collections(constchar *pattern_utf8)
{
cdk::string pattern(pattern_utf8 ? pattern_utf8 : "%");
return m_session.new_stmt<OP_LIST_COLLECTIONS>(*this, pattern)->exec();
}
voidcreate_collection(constchar *name,
bool reuse)
{
assert(name && *name);
Db_obj_ref coll(this->name(), name);
create_object<Object_type::COLLECTION>(
m_session.m_impl, coll, reuse, std::string()
);
}
voidcreate_collection(constchar *name,
bool reuse,
const std::string &level,
const std::string &schema)
{
assert(name && *name);
Db_obj_ref coll(this->name(), name);
create_object<Object_type::COLLECTION>(
m_session.m_impl, coll, reuse, level, schema
);
}
voidcreate_collection(constchar *name,
bool reuse,
const std::string &validation_json)
{
assert(name && *name);
Db_obj_ref coll(this->name(), name);
create_object<Object_type::COLLECTION>(
m_session.m_impl, coll, reuse, validation_json
);
}
voidcreate_collection(constchar *name,
const std::string &json)
{
assert(name && *name);
Db_obj_ref coll(this->name(), name);
create_object<Object_type::COLLECTION>(
m_session.m_impl, coll, json
);
}
voidmodify_collection(constchar *name,
std::string level,
std::string schema)
{
assert(name && *name);
Db_obj_ref coll(this->name(), name);
modify_object<Object_type::COLLECTION>(
m_session.m_impl, coll, level, schema
);
}
voidmodify_collection(constchar *name,
std::string json,
bool validation_json = false)
{
assert(name && *name);
Db_obj_ref coll(this->name(), name);
modify_object<Object_type::COLLECTION>(
m_session.m_impl, coll, json, validation_json
);
}
template <Object_type T>
voiddrop_object(constchar *name)
{
assert(name && *name);
Db_obj_ref obj(this->name(), name);
::mysqlx::impl::common::drop_object<T>(m_session.m_impl, obj);
}
voiddrop_collection(constchar *name)
{
drop_object<Object_type::COLLECTION>(name);
}
};
structmysqlx_collection_options_struct
: public Mysqlx_diag
{
bool m_reuse = false;
std::string m_validation;
std::string m_validation_level;
std::string m_validation_schema;
enum Usage{
REUSE,
VALIDATION,
VALIDATION_LEVEL,
VALIDATION_SCHEMA,
LAST
};
std::bitset<LAST> m_usage;
};
structmysqlx_collection_struct
: public Mysqlx_diag
, public Object_ref
{
private:
mysqlx_session_struct &m_sess;
public:
mysqlx_collection_struct(mysqlx_schema_struct &schema, cdk::string name)
: Object_ref(schema.name(), name)
, m_sess(schema.get_session())
{}
uint64_tcount();
boolexists()
{
return
check_object_exists<Object_type::COLLECTION>(
m_sess.m_impl, *this
);
}
voidcreate_index(constchar* name_utf8, constchar* idx_json)
{
mysqlx_result_struct *result =
m_sess.new_stmt<OP_IDX_CREATE>(
*this,
cdk::string(name_utf8),
cdk::string(idx_json)
)->exec();
if (!result)
throw_error("Failed to execute create index operation.");
}
voiddrop_index(constchar* name_utf8)
{
mysqlx_result_struct *result =
m_sess.new_stmt<OP_IDX_DROP>(
*this,
cdk::string(name_utf8)
)->exec();
if (!result)
throw_error("Failed to execute drop index operation.");
}
mysqlx_session_t &get_session()
{
return m_sess;
}
};
structmysqlx_table_struct
: public Mysqlx_diag
, public Object_ref
{
private:
mysqlx_session_struct &m_sess;
public:
mysqlx_table_struct(mysqlx_schema_struct &schema, cdk::string name)
: Object_ref(schema.name(), name)
, m_sess(schema.get_session())
{}
uint64_tcount();
boolexists()
{
return
check_object_exists<Object_type::TABLE>(
m_sess.m_impl, *this
);
}
mysqlx_session_struct &get_session()
{
return m_sess;
}
};
#endif/* __MYSQLX_CC_INTERNAL_H__ */