MySQL Connector/C++ 9.3.0
MySQL connector library for C and C++ applications
All Classes Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
session.h
1/*
2 * Copyright (c) 2017, 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_DETAIL_SESSION_H
32#define MYSQLX_DETAIL_SESSION_H
33
34#include "../common.h"
35#include "../crud.h"
36
37#include <set>
38
39namespace cdk {
40 class Session;
41}
42
43
44namespace mysqlx {
45MYSQLX_ABI_BEGIN(2,0)
46
47class Value;
48class Session;
49class Schema;
50class Table;
51class Collection;
52class CollectionOptions;
53
54namespace common {
55 class Session_impl;
56 class Session_pool;
57 using Shared_session_pool = std::shared_ptr<Session_pool>;
58 class Result_init;
59 class Result_impl;
60}
61
62namespace internal {
63
64class Schema_detail;
65using Client_impl = common::Session_pool;
66using Shared_client_impl = std::shared_ptr<Client_impl>;
67using Session_impl = common::Session_impl;
68using Shared_session_impl = std::shared_ptr<common::Session_impl>;
69
70
71/*
72 Base class for database objects. Can't be used alone.
73*/
74
75class PUBLIC_API Db_obj_base
76{
77protected:
78
79 DLL_WARNINGS_PUSH
80 Shared_session_impl m_sess;
81 string m_name;
82 DLL_WARNINGS_POP
83
84 Db_obj_base(const Shared_session_impl& sess, const string& name)
85 : m_sess(sess), m_name(name)
86 {}
87
88 virtual ~Db_obj_base() NOEXCEPT
89 {}
90};
91
92
93class PUBLIC_API Collection_detail
94 : public Db_obj_base
95{
96protected:
97
98 Collection_detail(const Shared_session_impl &sess, const string &name)
99 : Db_obj_base(sess, name)
100 {}
101
102 virtual Schema_detail& get_schema() = 0;
103
104 Result add_or_replace_one(const string &id, Value&&, bool);
105
106 void index_drop(const string &name);
107 void index_create(const string &name, Value &&spec);
108};
109
110
111// ---------------------------------------------------------------------------
112
113/*
114 Base class for classes to be used by common::List_source<> which get item
115 from query results.
116
117 It assumes that the first column in the results contains string
118 data. An instance of this class iterates over the string data in the
119 result until all rows are consumed.
120
121 Derived class must send the query to the server and set m_res member to point
122 at the result of this query.
123*/
124
125struct PUBLIC_API Query_src
126{
127 using Value = string;
128 using Res_impl = common::Result_impl;
129
130 Res_impl *m_res = nullptr;
131 const void *m_row = nullptr;
132
133public:
134
135 Query_src()
136 {}
137
138 Query_src(Query_src &&other)
139 : m_res(other.m_res)
140 {
141 // Note: only one instance of Query_src owns m_res.
142 other.m_res = nullptr;
143 }
144
145 Query_src(const Query_src&) = delete;
146
147 virtual ~Query_src() NOEXCEPT;
148
149 virtual void iterator_start()
150 {
151 assert(m_res);
152 }
153
154 bool iterator_next();
155 string iterator_get();
156};
157
158
159// ---------------------------------------------------------------------------
160
161
162class PUBLIC_API Schema_detail
163 : public Db_obj_base
164{
165protected:
166
167 enum Obj_type { COLLECTION, TABLE };
168
169 /*
170 Sources for lists of schema objects and their names.
171
172 When constructing a source, a SQL style patter on object names is
173 given as ctor parameter -- only object matching the pattern are listed.
174 Name_src accepts a parameter which tells whether names of tables or
175 collections should be listed.
176 */
177
178 struct PUBLIC_API Name_src
179 : public Query_src
180 {
181 const Schema &m_schema;
182 Name_src(const Schema&, Obj_type, const string &pattern);
183 };
184
185 struct PUBLIC_API Collection_src
186 : Name_src
187 {
188 using Value = Collection;
189
190 Collection_src(const Schema &sch, const string &pattern)
191 : Name_src(sch, COLLECTION, pattern)
192 {}
193
194 using Name_src::iterator_start;
195 using Name_src::iterator_next;
196 Collection iterator_get();
197 };
198
199 struct PUBLIC_API Table_src
200 : Name_src
201 {
202 using Value = Table;
203
204 Table_src(const Schema &sch, const string &pattern)
205 : Name_src(sch, TABLE, pattern)
206 {}
207
208 using Name_src::iterator_start;
209 using Name_src::iterator_next;
210 Table iterator_get();
211 };
212
213 Schema_detail(const Shared_session_impl &sess, const string &name)
214 : Db_obj_base(sess, name)
215 {}
216
217public:
218
219 using CollectionList = List_initializer<List_source<Collection_src>>;
220 using TableList = List_initializer<List_source<Table_src>>;
221 using StringList = List_initializer<List_source<Name_src>>;
222
223protected:
224
225
226 void create_collection(const mysqlx::string &name,
227 CollectionOptions options);
228 void modify_collection(const mysqlx::string &name,
229 CollectionOptions options);
230 void drop_collection(const string &name);
231
232 friend Collection_detail;
233
234 struct Access;
235 friend Access;
236};
237
238
239/*
240 Class representing an SQL statement that can be executed on the server.
241*/
242
243struct SQL_statement;
244
245using SQL_statement_cmd = Executable<SqlResult, SQL_statement>;
246
247struct SQL_statement
248 : public Bind_placeholders< SQL_statement_cmd >
249{
250 ~SQL_statement() NOEXCEPT
251 {}
252
253 SQL_statement(Session *sess, const string &query)
254 {
255 assert(sess);
256 try {
257 reset(internal::Crud_factory::mk_sql(*sess, query));
258 }
259 CATCH_AND_WRAP
260 }
261
262 SQL_statement(SQL_statement_cmd &other)
263 {
264 SQL_statement_cmd::operator=(other);
265 }
266
267 SQL_statement(SQL_statement_cmd &&other)
268 {
269 SQL_statement_cmd::operator=(std::move(other));
270 }
271};
272
273
274struct Session_detail;
275
276struct PUBLIC_API Client_detail
277{
278
279 // Disable copy semantics for client class.
280
281 Client_detail(const Client_detail&) = delete;
282 Client_detail& operator=(const Client_detail&) = delete;
283
284
285
286 Client_detail(common::Settings_impl &settings);
287 //Client_detail(common::Settings_impl &&settings);
288
289 void close();
290
291protected:
292
293 Client_detail(Client_detail && other)
294 {
295 m_impl = other.m_impl;
296 other.m_impl.reset();
297 }
298
299 common::Shared_session_pool& get_session_pool();
300
301
302 struct INTERNAL Impl;
303
304 DLL_WARNINGS_PUSH
305 Shared_client_impl m_impl = NULL;
306 DLL_WARNINGS_POP
307
308 friend Session;
309};
310
311
312struct PUBLIC_API Session_detail
313{
314 // Disable copy semantics for session class.
315
316 Session_detail(const Session_detail&) = delete;
317 Session_detail& operator=(const Session_detail&) = delete;
318
319 /*
320 Sources for lists of schemata and schema names. Only schemata matching
321 the given SQL-style pattern are listed.
322 */
323
324 struct PUBLIC_API Name_src
325 : public Query_src
326 {
327 const Session &m_sess;
328 Name_src(const Session&, const string &pattern);
329 };
330
331 struct PUBLIC_API Schema_src
332 : Name_src
333 {
334 using Value = Schema;
335
336 Schema_src(Session &sess, const string &pattern)
337 : Name_src(sess, pattern)
338 {}
339
340 Schema_src(Session &sess)
341 : Schema_src(sess, "%")
342 {}
343
344 using Name_src::iterator_start;
345 using Name_src::iterator_next;
346 Schema iterator_get();
347 };
348
349public:
350
351 using SchemaList = List_initializer<List_source<Schema_src>>;
352
353protected:
354
355 Session_detail(Session_detail && other)
356 {
357 m_impl = other.m_impl;
358 other.m_impl.reset();
359 }
360
361
362 struct INTERNAL Impl;
363
364 /*
365 Note: Session implementation is shared with result objects because it
366 must exists as long as result implementation exists. This means that
367 even when session object is deleted, its implementation can still hang
368 around.
369 */
370
371 DLL_WARNINGS_PUSH
372 Shared_session_impl m_impl = NULL;
373 DLL_WARNINGS_POP
374
375 Session_detail(common::Settings_impl&);
376 Session_detail(common::Shared_session_pool&);
377
378 virtual ~Session_detail() NOEXCEPT
379 {
380 try {
381 if (m_impl)
382 close();
383 }
384 catch (...) {}
385 }
386
387 void create_schema(const string &name, bool reuse);
388 void drop_schema(const string &name);
389 string get_default_schema_name();
390
391 void start_transaction();
392 void commit();
393 void rollback(const string &sp = string());
394 string savepoint_set(const string &sp = string());
395 void savepoint_remove(const string&);
396
397
398 common::Session_impl& get_impl()
399 {
400 if (!m_impl)
401 THROW("Invalid session");
402 return *m_impl;
403 }
404
405 INTERNAL cdk::Session& get_cdk_session();
406
407 void close();
408
409 /*
410 Do necessary cleanups before sending new command to the server.
411 */
412 void prepare_for_cmd();
413
414public:
415
417 friend Result_detail::Impl;
418 friend internal::Crud_factory;
420};
421
422
423} // internal namespace
424
425MYSQLX_ABI_END(2,0)
426} // mysqlx namespace
427
428
429#endif
A wrapper around std::wstring that can perform conversions from/to different character encodings used...
Definition: common.h:114