26#ifndef _ROUTER_MYSQL_SESSION_H_
27#define _ROUTER_MYSQL_SESSION_H_
52 operator bool() {
return code_ != 0; }
74template <mysql_option Opt,
class ValueType>
86 constexpr const void *
data()
const {
return std::addressof(
v_); }
89 constexpr void *
data() {
return std::addressof(
v_); }
110template <mysql_option Opt>
120 constexpr const void *
data()
const {
return v_; }
122 constexpr void *
data() {
return std::addressof(v_); }
132template <mysql_option Opt>
143 constexpr const void *
data()
const {
return nullptr; }
145 constexpr void *
data() {
return nullptr; }
157 typedef std::vector<const char *>
Row;
162 static const char kSslModeDisabled[];
163 static const char kSslModePreferred[];
164 static const char kSslModeRequired[];
165 static const char kSslModeVerifyCa[];
166 static const char kSslModeVerifyIdentity[];
173 template <mysql_option Opt>
177 template <mysql_option Opt>
181 template <mysql_option Opt>
185 template <mysql_option Opt>
238 session_->execute(
"START TRANSACTION");
244 session_->execute(
"ROLLBACK");
252 session_->execute(
"COMMIT");
257 session_->execute(
"ROLLBACK");
265 class Error :
public std::runtime_error {
271 Error(
const char *error,
unsigned int code,
272 const std::string message =
"<not set>")
273 :
std::runtime_error(
error), code_(code), message_(message) {}
275 Error(
const std::string &error,
unsigned int code,
276 const std::string &message =
"<not set>")
277 :
std::runtime_error(
error), code_(code), message_(message) {}
279 unsigned int code()
const {
return code_; }
280 std::string
message()
const {
return message_; }
291 size_t size()
const {
return row_.size(); }
311 virtual void log(
const std::string &msg) = 0;
318 void log(
const std::string & )
override {}
323 bool log_will_be_ignored()
const override;
325 void log(
const std::string &msg)
override;
328 MySQLSession(std::unique_ptr<LoggingStrategy> logging_strategy =
329 std::make_unique<LoggingStrategyNone>());
333 std::string ssl_mode);
338 const std::string &tls_version,
339 const std::string &ssl_cipher,
340 const std::string &ca,
const std::string &capath,
341 const std::string &crl,
342 const std::string &crlpath);
345 std::string tls_version()
const;
346 std::string ssl_cipher()
const;
347 std::string ssl_ca()
const;
348 std::string ssl_capath()
const;
349 std::string ssl_crl()
const;
350 std::string ssl_crlpath()
const;
352 std::string ssl_cert()
const;
353 std::string ssl_key()
const;
356 int read_timeout()
const;
359 virtual void set_ssl_cert(
const std::string &cert,
const std::string &
key);
374 template <
class SettableMysqlOption>
376 if (0 !=
mysql_options(connection_, opt.option(), opt.data())) {
400 template <
class GettableMysqlOption>
410 const std::string &username,
const std::string &
password,
411 const std::string &unix_socket,
412 const std::string &default_schema,
425 const std::string &
query);
427 const std::string &
query,
const RowProcessor &processor,
428 const FieldValidator &validator);
429 virtual std::unique_ptr<MySQLSession::ResultRow> query_one(
430 const std::string &
query,
431 const FieldValidator &validator);
437 std::unique_ptr<MySQLSession::ResultRow>
query_one(
const std::string &stmt) {
438 return query_one(stmt, [](
unsigned,
MYSQL_FIELD *) {});
441 virtual uint64_t last_insert_id() noexcept;
443 virtual
unsigned warning_count() noexcept;
445 virtual
std::
string quote(const
std::
string &s,
char qchar = '\'') const;
447 virtual
bool is_connected() noexcept {
return connection_ && connected_; }
448 const std::string &
get_address() noexcept {
return connection_address_; }
451 virtual unsigned int last_errno();
453 virtual const char *ssl_cipher();
455 virtual bool is_ssl_session_reused();
498 const std::string &
q);
504 const std::string &
q);
Definition: mysql_session.h:265
Error(const char *error, unsigned int code, const std::string message="<not set>")
Definition: mysql_session.h:271
std::string message() const
Definition: mysql_session.h:280
unsigned int code() const
Definition: mysql_session.h:279
const std::string message_
Definition: mysql_session.h:284
Error(const std::string &error, unsigned int code, const std::string &message="<not set>")
Definition: mysql_session.h:275
const unsigned int code_
Definition: mysql_session.h:283
Definition: mysql_session.h:477
void operator()(MYSQL_RES *res)
Definition: mysql_session.h:479
Definition: mysql_session.h:287
ResultRow(Row row)
Definition: mysql_session.h:289
size_t size() const
Definition: mysql_session.h:291
Row row_
Definition: mysql_session.h:295
virtual ~ResultRow()=default
const char *& operator[](size_t i)
Definition: mysql_session.h:292
Definition: mysql_session.h:235
void commit()
Definition: mysql_session.h:251
Transaction(MySQLSession *session)
Definition: mysql_session.h:237
void rollback()
Definition: mysql_session.h:256
~Transaction()
Definition: mysql_session.h:241
MySQLSession * session_
Definition: mysql_session.h:262
Definition: mysql_session.h:153
std::function< void(unsigned, MYSQL_FIELD *)> FieldValidator
Definition: mysql_session.h:159
std::string connection_address_
Definition: mysql_session.h:474
bool get_option(GettableMysqlOption &opt) const
get a mysql option.
Definition: mysql_session.h:401
MYSQL * connection_
Definition: mysql_session.h:472
std::unique_ptr< MySQLSession::ResultRow > query_one(const std::string &stmt)
Definition: mysql_session.h:437
std::unique_ptr< MYSQL_RES, MYSQL_RES_Deleter > mysql_result_type
Definition: mysql_session.h:482
std::function< bool(const Row &)> RowProcessor
Definition: mysql_session.h:158
stdx::expected< void, MysqlError > set_option(const SettableMysqlOption &opt)
set a mysql option.
Definition: mysql_session.h:375
void query(const std::string &stmt, const RowProcessor &processor)
Definition: mysql_session.h:433
std::string default_schema
Definition: mysql_session.h:469
SQLLogFilter log_filter_
Definition: mysql_session.h:475
const std::string & get_address() noexcept
Definition: mysql_session.h:448
bool connected_
Definition: mysql_session.h:473
std::string host
Definition: mysql_session.h:466
std::unique_ptr< LoggingStrategy > logging_strategy_
Definition: mysql_session.h:460
std::string unix_socket
Definition: mysql_session.h:468
std::vector< const char * > Row
Definition: mysql_session.h:157
Definition: mysql_session.h:44
MysqlError(unsigned int code, std::string message, std::string sql_state)
Definition: mysql_session.h:47
unsigned int value() const
Definition: mysql_session.h:56
std::string message() const
Definition: mysql_session.h:54
std::string sql_state() const
Definition: mysql_session.h:55
std::string sql_state_
Definition: mysql_session.h:61
unsigned int code_
Definition: mysql_session.h:59
std::string message_
Definition: mysql_session.h:60
A SQLLogFilter allows to replace substrings defined by a set of hardcoded regular expressions with '*...
Definition: log_filter.h:77
gettable, settable option for 'const char *' based mysql_option's.
Definition: mysql_session.h:111
constexpr const void * data() const
Definition: mysql_session.h:120
const char * value_type
Definition: mysql_session.h:113
constexpr void * data()
Definition: mysql_session.h:122
constexpr Option(value_type v)
Definition: mysql_session.h:116
constexpr value_type value() const
Definition: mysql_session.h:126
constexpr mysql_option option() const noexcept
Definition: mysql_session.h:118
constexpr void value(value_type v)
Definition: mysql_session.h:124
constexpr mysql_option option() const noexcept
Definition: mysql_session.h:141
constexpr value_type value() const
Definition: mysql_session.h:147
constexpr void * data()
Definition: mysql_session.h:145
std::nullptr_t value_type
Definition: mysql_session.h:135
constexpr Option(value_type)
Definition: mysql_session.h:139
constexpr const void * data() const
Definition: mysql_session.h:143
gettable, settable option for mysql_option's.
Definition: mysql_session.h:75
constexpr Option()=default
constexpr Option(value_type v)
Definition: mysql_session.h:80
constexpr const void * data() const
Definition: mysql_session.h:86
ValueType value_type
Definition: mysql_session.h:77
constexpr void * data()
Definition: mysql_session.h:89
value_type v_
Definition: mysql_session.h:98
constexpr mysql_option option() const noexcept
Definition: mysql_session.h:83
constexpr value_type value() const
Definition: mysql_session.h:95
constexpr void value(value_type v)
Definition: mysql_session.h:92
Definition: expected.h:286
static bool execute(MYSQL_STMT *stmt, char *packet, ulong length, bool send_param_count)
Auxiliary function to send COM_STMT_EXECUTE packet to server and read reply.
Definition: libmysql.cc:1734
static char * query
Definition: myisam_ftdump.cc:47
static char * server_version
Definition: mysql.cc:120
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
const char *STDCALL mysql_sqlstate(MYSQL *mysql)
Definition: client.cc:9516
unsigned int STDCALL mysql_errno(MYSQL *mysql)
Definition: client.cc:9174
mysql_option
Definition: mysql.h:170
int STDCALL mysql_get_option(MYSQL *mysql, enum mysql_option option, const void *arg)
Return the current values for the options settable through mysql_options()
Definition: client.cc:8873
const char *STDCALL mysql_error(MYSQL *mysql)
Definition: client.cc:9178
void STDCALL mysql_free_result(MYSQL_RES *result)
Definition: client.cc:1956
int STDCALL mysql_options(MYSQL *mysql, enum mysql_option option, const void *arg)
Definition: client.cc:8534
mysql_ssl_mode
Definition: mysql.h:272
static char * password
Definition: mysql_secure_installation.cc:58
const char * host
Definition: mysqladmin.cc:65
void error(const char *format,...)
ulong connect_timeout
Definition: mysqld.cc:1340
void disconnect(Connection &c)
Definition: server.cc:48
Definition: http_server_component.cc:34
int last_error()
get last socket error.
Definition: socket_error.h:82
stdx::expected< void, error_type > connect(native_handle_type native_handle, const struct sockaddr *addr, size_t addr_len)
wrap connect() in a portable way.
Definition: socket.h:353
Definition: gcs_xcom_synode.h:64
unexpected(E) -> unexpected< E >
static int is_connected(connection_descriptor *con)
Definition: node_connection.h:94
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
#define ROUTER_MYSQL_EXPORT
Definition: router_mysql_export.h:15
constexpr const char * ssl_mode_to_string(SslMode mode)
Definition: ssl_mode.h:44
Definition: mysql_session.h:322
Definition: mysql_session.h:314
bool log_will_be_ignored() const override
Definition: mysql_session.h:316
void log(const std::string &) override
Definition: mysql_session.h:318
Definition: mysql_session.h:298
LoggingStrategy(const LoggingStrategy &)=default
LoggingStrategy & operator=(const LoggingStrategy &)=default
virtual ~LoggingStrategy()=default
virtual bool log_will_be_ignored() const =0
LoggingStrategy(LoggingStrategy &&)=default
LoggingStrategy()=default
virtual void log(const std::string &msg)=0
LoggingStrategy & operator=(LoggingStrategy &&)=default
synode_no q[FIFO_SIZE]
Definition: xcom_base.cc:4086