MySQL Connector/C++
MySQL connector library for C and C++ applications
Loading...
Searching...
No Matches
session.h
1/*
2 * Copyright (c) 2017, 2020, 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 also distributed with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an
12 * additional permission to link the program and your derivative works
13 * with the separately licensed software that they have included with
14 * MySQL.
15 *
16 * Without limiting anything contained in the foregoing, this file,
17 * which is part of MySQL Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * http://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()
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();
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(Session *sess, const string &query)
251 {
252 assert(sess);
253 try {
254 reset(internal::Crud_factory::mk_sql(*sess, query));
255 }
256 CATCH_AND_WRAP
257 }
258
259 SQL_statement(SQL_statement_cmd &other)
260 {
261 SQL_statement_cmd::operator=(other);
262 }
263
264 SQL_statement(SQL_statement_cmd &&other)
265 {
266 SQL_statement_cmd::operator=(std::move(other));
267 }
268};
269
270
271struct Session_detail;
272
273struct PUBLIC_API Client_detail
274{
275
276 // Disable copy semantics for client class.
277
278 Client_detail(const Client_detail&) = delete;
279 Client_detail& operator=(const Client_detail&) = delete;
280
281
282
283 Client_detail(common::Settings_impl &settings);
284 //Client_detail(common::Settings_impl &&settings);
285
286 void close();
287
288protected:
289
290 Client_detail(Client_detail && other)
291 {
292 m_impl = other.m_impl;
293 other.m_impl.reset();
294 }
295
296 common::Shared_session_pool& get_session_pool();
297
298
299 struct INTERNAL Impl;
300
301 DLL_WARNINGS_PUSH
302 Shared_client_impl m_impl = NULL;
303 DLL_WARNINGS_POP
304
305 friend Session;
306};
307
308
309struct PUBLIC_API Session_detail
310{
311 // Disable copy semantics for session class.
312
313 Session_detail(const Session_detail&) = delete;
314 Session_detail& operator=(const Session_detail&) = delete;
315
316 /*
317 Sources for lists of schemata and schema names. Only schemata matching
318 the given SQL-style pattern are listed.
319 */
320
321 struct PUBLIC_API Name_src
322 : public Query_src
323 {
324 const Session &m_sess;
325 Name_src(const Session&, const string &pattern);
326 };
327
328 struct PUBLIC_API Schema_src
329 : Name_src
330 {
331 using Value = Schema;
332
333 Schema_src(Session &sess, const string &pattern)
334 : Name_src(sess, pattern)
335 {}
336
337 Schema_src(Session &sess)
338 : Schema_src(sess, "%")
339 {}
340
341 using Name_src::iterator_start;
342 using Name_src::iterator_next;
343 Schema iterator_get();
344 };
345
346public:
347
348 using SchemaList = List_initializer<List_source<Schema_src>>;
349
350protected:
351
352 Session_detail(Session_detail && other)
353 {
354 m_impl = other.m_impl;
355 other.m_impl.reset();
356 }
357
358
359 struct INTERNAL Impl;
360
361 /*
362 Note: Session implementation is shared with result objects because it
363 must exists as long as result implementation exists. This means that
364 even when session object is deleted, its implementation can still hang
365 around.
366 */
367
368 DLL_WARNINGS_PUSH
369 Shared_session_impl m_impl = NULL;
370 DLL_WARNINGS_POP
371
372 Session_detail(common::Settings_impl&);
373 Session_detail(common::Shared_session_pool&);
374
375 virtual ~Session_detail()
376 {
377 try {
378 if (m_impl)
379 close();
380 }
381 catch (...) {}
382 }
383
384 void create_schema(const string &name, bool reuse);
385 void drop_schema(const string &name);
386 string get_default_schema_name();
387
388 void start_transaction();
389 void commit();
390 void rollback(const string &sp = string());
391 string savepoint_set(const string &sp = string());
392 void savepoint_remove(const string&);
393
394
395 common::Session_impl& get_impl()
396 {
397 if (!m_impl)
398 THROW("Invalid session");
399 return *m_impl;
400 }
401
402 INTERNAL cdk::Session& get_cdk_session();
403
404 void close();
405
406 /*
407 Do necessary cleanups before sending new command to the server.
408 */
409 void prepare_for_cmd();
410
411public:
412
414 friend Result_detail::Impl;
415 friend internal::Crud_factory;
417};
418
419
420} // internal namespace
421
422MYSQLX_ABI_END(2,0)
423} // mysqlx namespace
424
425
426#endif
A wrapper around std::wstring that can perform conversions from/to different character encodings used...
Definition: common.h:114