MySQL 26.10.0
Source Code Documentation
mrs_component_mysql_command_service_session.h
Go to the documentation of this file.
1/* Copyright (c) 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MRS_COMPONENT_MYSQL_COMMAND_SERVICE_SESSION_H
25#define MRS_COMPONENT_MYSQL_COMMAND_SERVICE_SESSION_H
26
27#include <stdexcept>
28#include <string>
29
30#include "mysql.h"
32#include "secure_string.h"
33
34namespace mrs_component {
35
38
39/**
40 Owns the component command-service handle and optional thread
41 initialization.
42
43 Callers retain responsibility for SQL policy and result handling.
44*/
46 public:
47 class Connection_error : public std::runtime_error {
48 public:
49 Connection_error(const std::string &message, unsigned int error_code)
50 : std::runtime_error(message), m_error_code(error_code) {}
51
52 [[nodiscard]] unsigned int error_code() const { return m_error_code; }
53
54 private:
55 unsigned int m_error_code;
56 };
57
60
63 const Mysql_command_service_session &) = delete;
66 delete;
67
68 void connect(const std::string &user, const std::string &host,
69 bool initialize_thread, unsigned long extra_client_flags = 0,
70 bool bypass_max_connections = false);
71 // Requires the caller's thread lifecycle to initialize
72 // mysql_command_thread.
73 void connect_authenticated(const std::string &user,
75 unsigned long extra_client_flags = 0);
76 void close() noexcept;
77
78 [[nodiscard]] bool reset() noexcept;
79 [[nodiscard]] bool autocommit(bool mode) noexcept;
80
81 [[nodiscard]] MYSQL_H handle() const noexcept { return mysql_h_; }
82 [[nodiscard]] MYSQL *mysql() const noexcept;
83 [[nodiscard]] bool is_connected() const noexcept {
84 return mysql_h_ != nullptr;
85 }
86
87 [[nodiscard]] unsigned int last_errno() const noexcept;
88 [[nodiscard]] std::string last_error() const;
89 [[nodiscard]] std::string last_sqlstate() const;
90
91 private:
92 void connect_impl(const std::string &user, const std::string &host,
93 bool initialize_thread, unsigned long extra_client_flags,
94 bool bypass_max_connections,
96
97 MYSQL_H mysql_h_{nullptr};
98 bool thread_initialized_{false};
99 std::string user_;
100 std::string host_;
101};
102
103} // namespace mrs_component
104
105#endif // MRS_COMPONENT_MYSQL_COMMAND_SERVICE_SESSION_H
Definition: mrs_component_mysql_command_service_session.h:47
unsigned int error_code() const
Definition: mrs_component_mysql_command_service_session.h:52
Connection_error(const std::string &message, unsigned int error_code)
Definition: mrs_component_mysql_command_service_session.h:49
unsigned int m_error_code
Definition: mrs_component_mysql_command_service_session.h:55
Owns the component command-service handle and optional thread initialization.
Definition: mrs_component_mysql_command_service_session.h:45
Mysql_command_service_session & operator=(const Mysql_command_service_session &)=delete
std::string host_
Definition: mrs_component_mysql_command_service_session.h:100
Mysql_command_service_session & operator=(Mysql_command_service_session &&)=delete
std::string user_
Definition: mrs_component_mysql_command_service_session.h:99
Mysql_command_service_session(const Mysql_command_service_session &)=delete
Mysql_command_service_session(Mysql_command_service_session &&)=delete
Null-terminated string which is securely wiped on destruction.
Definition: secure_string.h:59
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
struct MYSQL_H_imp * MYSQL_H
Definition: mysql_command_services.h:33
static char * password
Definition: mysql_secure_installation.cc:58
const char * host
Definition: mysqladmin.cc:66
static MYSQL * mysql
Definition: mysqldump.cc:151
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
constexpr value_type autocommit
Definition: classic_protocol_constants.h:152
Definition: mrs_component.cc:137
void initialize_worker_command_thread()
Definition: mrs_component_mysql_command_service_session.cc:79
void deinitialize_worker_command_thread() noexcept
Definition: mrs_component_mysql_command_service_session.cc:81
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Singleton class that stores the application type.
Definition: component_application_type_name.h:31
detail::SecureString< char, std::char_traits< char >, SecureAllocator< char > > SecureString
Definition: secure_string.h:402
stdx::expected< void, std::error_code > close(file_handle_type native_handle)
close file handle.
Definition: file.h:239
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
static int handle(int sql_errno, const char *sqlstate, const char *message, void *state)
Bridge function between the C++ API offered by this module and the C API of the parser service.
Definition: services.cc:64
Define std::hash<Gtid>.
Definition: gtid.h:355
mode
Definition: file_handle.h:61
static int is_connected(connection_descriptor *con)
Definition: node_connection.h:95
Definition: mysql.h:303