- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathsession.cc
294 lines (245 loc) · 7.33 KB
/
session.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
/*
* 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
*/
#include<mysqlx/common.h>
#include<mysqlx/xapi.h>
#include"mysqlx_cc_internal.h"
#include<algorithm>
#include<string>
usingnamespacemysqlx::common;
mysqlx_session_struct::mysqlx_session_struct( mysqlx_client_t *cli)
{
if (cli)
m_impl = std::make_shared<Session_impl>(cli->get_impl());
else
throw_error("Invalid client pool");
}
mysqlx_session_struct::mysqlx_session_struct(
mysqlx_session_options_struct *opt
)
{
cdk::ds::Multi_source ds;
opt->get_data_source(ds);
m_impl = std::make_shared<Session_impl>(ds);
}
mysqlx_session_struct::mysqlx_session_struct(
const std::string &host, unsignedshort port,
const std::string &usr, const std::string *pwd,
const std::string *db
)
: mysqlx_session_struct(mysqlx_session_options_struct(host, port, usr, pwd, db))
{}
mysqlx_session_struct::mysqlx_session_struct(
const std::string &conn_str
)
: mysqlx_session_struct(mysqlx_session_options_struct(conn_str))
{}
mysqlx_stmt_struct*
mysqlx_session_struct::sql_query(constchar *query_utf8, uint32_t length)
{
if (!query_utf8 || !(*query_utf8))
throwMysqlx_exception("Query is empty");
if (length == MYSQLX_NULL_TERMINATED)
length = (uint32_t)strlen(query_utf8);
std::string query(query_utf8, length);
return new_stmt<OP_SQL>(cdk::string(query)); // note: UTF8 conversion
}
mysqlx_error_struct * mysqlx_session_struct::get_last_error()
{
cdk::Session &sess = get_session();
// Return session errors from CDK first
if (sess.entry_count())
{
m_error.set(&sess.get_error());
}
elseif (!m_error.message() && !m_error.error_num())
returnNULL;
return &m_error;
}
const cdk::Error * mysqlx_session_struct::get_cdk_error()
{
if (get_session().entry_count())
return &get_session().get_error();
returnNULL;
}
voidmysqlx_session_struct::reset_diagnostic()
{
m_error.reset();
}
voidmysqlx_session_struct::transaction_begin()
{
// Note: the internal implementation object handles registered results etc.
stmt_traits<OP_TRX_BEGIN>::Impl stmt(m_impl);
stmt.execute();
}
voidmysqlx_session_struct::transaction_commit()
{
stmt_traits<OP_TRX_COMMIT>::Impl stmt(m_impl);
stmt.execute();
}
voidmysqlx_session_struct::transaction_rollback(constchar *sp)
{
stmt_traits<OP_TRX_ROLLBACK>::Impl stmt(
m_impl,
sp ? std::string(sp) : std::string()
);
stmt.execute();
}
constchar * mysqlx_session_struct::savepoint_set(constchar *sp)
{
stmt_traits<OP_TRX_SAVEPOINT_SET>::Impl stmt(
m_impl,
sp ? std::string(sp) : std::string()
);
stmt.execute();
m_savepoint_name = stmt.get_name();
return m_savepoint_name.c_str();
}
voidmysqlx_session_struct::savepoint_remove(constchar *sp)
{
if (!sp || !sp[0])
throw_error("Invalid empty save point name");
stmt_traits<OP_TRX_SAVEPOINT_RM>::Impl stmt(m_impl, std::string(sp));
stmt.execute();
}
/*
============================================================================
Client object implementation
*/
mysqlx_client_struct::mysqlx_client_struct(constchar *conn_str,
constchar *client_opt)
{
mysqlx_session_options_struct opt(conn_str);
if (client_opt)
opt.set_client_opts(client_opt);
cdk::ds::Multi_source ds;
opt.get_data_source(ds);
m_impl.reset(newSession_pool(ds));
m_impl->set_pool_opts(opt);
}
mysqlx_client_struct::mysqlx_client_struct(mysqlx_session_options_t *opt)
{
cdk::ds::Multi_source ds;
opt->get_data_source(ds);
m_impl.reset(newSession_pool(ds));
m_impl->set_pool_opts(*opt);
}
using cdk::foundation::connection::TLS;
TLS::Options::SSL_MODE uint_to_ssl_mode(unsignedint mode)
{
switch (mode)
{
case SSL_MODE_DISABLED:
return TLS::Options::SSL_MODE::DISABLED;
case SSL_MODE_REQUIRED:
return TLS::Options::SSL_MODE::REQUIRED;
case SSL_MODE_VERIFY_CA:
return TLS::Options::SSL_MODE::VERIFY_CA;
case SSL_MODE_VERIFY_IDENTITY:
return TLS::Options::SSL_MODE::VERIFY_IDENTITY;
default:
assert(false);
// Quiet compile warnings
return TLS::Options::SSL_MODE::DISABLED;
}
}
unsignedintssl_mode_to_uint(TLS::Options::SSL_MODE mode)
{
switch (mode)
{
case TLS::Options::SSL_MODE::DISABLED:
return SSL_MODE_DISABLED;
case TLS::Options::SSL_MODE::REQUIRED:
return SSL_MODE_REQUIRED;
case TLS::Options::SSL_MODE::VERIFY_CA:
return SSL_MODE_VERIFY_CA;
case TLS::Options::SSL_MODE::VERIFY_IDENTITY:
return SSL_MODE_VERIFY_IDENTITY;
default:
assert(false);
// Quiet compile warnings
return0;
}
}
constchar* opt_name(mysqlx_opt_type_t opt)
{
using Option = Settings_impl::Session_option_impl;
returnSettings_impl::option_name(Option(opt));
}
constchar* ssl_mode_name(mysqlx_ssl_mode_t m)
{
using SSL_mode = Settings_impl::SSL_mode;
returnSettings_impl::ssl_mode_name(SSL_mode(m));
}
structError_bad_option : publicMysqlx_exception
{
Error_bad_option()
: Mysqlx_exception("Unrecognized connection option")
{}
Error_bad_option(const std::string &opt) : Error_bad_option()
{
m_message += ": " + opt;
}
Error_bad_option(unsignedint opt) : Error_bad_option()
{
std::ostringstream buf;
buf << opt;
m_message += " (" + buf.str() + ")";
}
};
structError_dup_option : publicMysqlx_exception
{
Error_dup_option(mysqlx_opt_type_t opt)
{
m_message = "Option ";
m_message += opt_name(opt);
m_message += " defined twice";
}
};
structError_bad_mode : publicMysqlx_exception
{
Error_bad_mode(const std::string &m)
{
m_message = "Unrecognized ssl-mode: " + m;
}
};
structError_ca_mode : publicMysqlx_exception
{
Error_ca_mode()
: Mysqlx_exception("The ssl-ca option is not compatible with ssl-mode")
{}
Error_ca_mode(mysqlx_ssl_mode_t m) : Error_ca_mode()
{
m_message += "";
m_message += ssl_mode_name(m);
}
Error_ca_mode(TLS::Options::SSL_MODE m)
: Error_ca_mode(mysqlx_ssl_mode_t(ssl_mode_to_uint(m)))
{}
};