MySQL 26.10.0
Source Code Documentation
mrs_component_command_executor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2026, 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,
6 as 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,
10 as 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 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MRS_COMPONENT_COMMAND_EXECUTOR_H
27#define MRS_COMPONENT_COMMAND_EXECUTOR_H
28
33
34#include <cstdint>
35#include <optional>
36#include <stdexcept>
37#include <string>
38#include <vector>
39
40namespace mrs_component {
41
42using Row = std::vector<std::string>;
43using Resultset = std::vector<Row>;
44
45/**
46 Executes text queries and prepared statements through component services.
47*/
49 public:
50 enum class Mode : uint8_t { Query, PreparedStatement };
51
52 class Connection_error : public std::runtime_error {
53 public:
54 Connection_error(const std::string &message, unsigned int error_code)
55 : std::runtime_error(message), m_error_code(error_code) {}
56
57 [[nodiscard]] unsigned int error_code() const { return m_error_code; }
58
59 private:
60 unsigned int m_error_code;
61 };
62
63 // Factory methods
65 static Command_executor make_with_explicit_ctx(std::string user,
66 std::string host, Mode mode);
67
69
70 Command_executor() = delete;
75
76 // Common to prepared statements and queries
77 void set_query(const std::string &query_text);
78 void execute(Resultset *result = nullptr);
79
80 // For prepared statements only
81 void bind_param(uint32_t index, const std::string &value, uint64_t type,
82 bool is_unsigned = false);
83 void bind_param(uint32_t index, const char *value, uint64_t type,
84 bool is_unsigned = false) {
85 bind_param(index, std::string(value), type, is_unsigned);
86 }
87 template <typename T>
88 void bind_param(uint32_t index, const T &value, uint64_t type,
89 bool is_unsigned = false) {
91 throw std::logic_error("Invalid mode");
92 }
93
94 if (stmt_bind->bind_param(m_stmt, index, false, type, is_unsigned, &value,
95 sizeof(T), nullptr, 0) != 0) {
96 throw std::runtime_error(get_last_error());
97 }
98 }
99
100 bool fetch(my_h_row *row);
101 uint64_t affected_rows();
102 uint64_t last_insert_id();
103
104 // Escape string
105 std::string escape_string(const std::string &input);
106
107 // Getters
108 [[nodiscard]] bool is_explicit_mode() const { return m_explicit_ctx_mode; }
109 [[nodiscard]] Mode mode() const { return m_mode; }
110 [[nodiscard]] const std::string &get_query() const { return m_query; }
111 [[nodiscard]] std::string get_last_error() const;
112 [[nodiscard]] int get_last_error_code() const { return m_last_error_code; }
113
114 // Result getters for Prepared Statements
115 std::optional<uint64_t> get_uint64_result();
116 std::optional<int64_t> get_int64_result();
117 std::optional<double> get_double_result();
118 std::optional<std::string> get_string_result();
119
120 private:
121 Command_executor(Mode mode, bool threaded_mode,
122 std::optional<std::string> connect_user = std::nullopt,
123 std::optional<std::string> connect_host = std::nullopt);
124
125 // Internal setup and exec
126 void connect();
127 void cleanup();
128 // Clears per-statement state but keeps the connection alive
129 void reset() noexcept;
132
133 // Aux methods
135 uint32_t col_index);
136
137 std::string m_query;
140
141 // TODO(miguel): Add a wrapper classes to hold m_stmt and m_mysql_res to
142 // manage the life-cycle of those
144 my_h_statement m_stmt = nullptr; // handle for statement service
145 MYSQL_RES_H m_mysql_res = nullptr; // handle for cmd service query result
146
148 nullptr}; // handle for session security context
149 std::string m_connect_user;
150 std::string m_connect_host;
151
152 unsigned int m_last_error_code{0};
153};
154
155} // namespace mrs_component
156
157#endif // MRS_COMPONENT_COMMAND_EXECUTOR_H
Definition: mrs_component_command_executor.h:52
unsigned int m_error_code
Definition: mrs_component_command_executor.h:60
Connection_error(const std::string &message, unsigned int error_code)
Definition: mrs_component_command_executor.h:54
unsigned int error_code() const
Definition: mrs_component_command_executor.h:57
Executes text queries and prepared statements through component services.
Definition: mrs_component_command_executor.h:48
std::optional< std::string > get_string_result()
Definition: mrs_component_command_executor.cc:477
Command_executor & operator=(const Command_executor &)=delete
std::optional< int64_t > get_int64_result()
Definition: mrs_component_command_executor.cc:437
std::string fetch_column_as_string(my_h_statement stmt, my_h_row row, uint32_t col_index)
Definition: mrs_component_command_executor.cc:174
void reset() noexcept
Definition: mrs_component_command_executor.cc:142
void bind_param(uint32_t index, const T &value, uint64_t type, bool is_unsigned=false)
Definition: mrs_component_command_executor.h:88
std::string m_query
Definition: mrs_component_command_executor.h:137
void cleanup()
Definition: mrs_component_command_executor.cc:130
void set_query(const std::string &query_text)
Definition: mrs_component_command_executor.cc:158
void connect()
Definition: mrs_component_command_executor.cc:74
std::string escape_string(const std::string &input)
Definition: mrs_component_command_executor.cc:395
void bind_param(uint32_t index, const std::string &value, uint64_t type, bool is_unsigned=false)
Definition: mrs_component_command_executor.cc:353
std::string get_last_error() const
Definition: mrs_component_command_executor.cc:406
Mode
Definition: mrs_component_command_executor.h:50
bool is_explicit_mode() const
Definition: mrs_component_command_executor.h:108
std::string m_connect_user
Definition: mrs_component_command_executor.h:149
Command_executor(Command_executor &&)=delete
void execute_query(Resultset *result)
Definition: mrs_component_command_executor.cc:298
MYSQL_RES_H m_mysql_res
Definition: mrs_component_command_executor.h:145
std::optional< uint64_t > get_uint64_result()
Definition: mrs_component_command_executor.cc:417
Mode mode() const
Definition: mrs_component_command_executor.h:109
void execute_statement(Resultset *result)
Definition: mrs_component_command_executor.cc:263
bool m_explicit_ctx_mode
Definition: mrs_component_command_executor.h:139
Command_executor(const Command_executor &)=delete
~Command_executor()
Definition: mrs_component_command_executor.cc:72
unsigned int m_last_error_code
Definition: mrs_component_command_executor.h:152
int get_last_error_code() const
Definition: mrs_component_command_executor.h:112
my_h_statement m_stmt
Definition: mrs_component_command_executor.h:144
Security_context_handle m_security_context
Definition: mrs_component_command_executor.h:147
uint64_t affected_rows()
Definition: mrs_component_command_executor.cc:372
const std::string & get_query() const
Definition: mrs_component_command_executor.h:110
bool fetch(my_h_row *row)
Definition: mrs_component_command_executor.cc:363
Mode m_mode
Definition: mrs_component_command_executor.h:138
static Command_executor make_with_explicit_ctx(std::string user, std::string host, Mode mode)
Definition: mrs_component_command_executor.cc:44
static Command_executor make_with_current_ctx(Mode mode)
Definition: mrs_component_command_executor.cc:40
std::optional< double > get_double_result()
Definition: mrs_component_command_executor.cc:457
void execute(Resultset *result=nullptr)
Definition: mrs_component_command_executor.cc:255
Mysql_command_service_session m_command_service_session
Definition: mrs_component_command_executor.h:143
Command_executor & operator=(Command_executor &&)=delete
void bind_param(uint32_t index, const char *value, uint64_t type, bool is_unsigned=false)
Definition: mrs_component_command_executor.h:83
std::string m_connect_host
Definition: mrs_component_command_executor.h:150
uint64_t last_insert_id()
Definition: mrs_component_command_executor.cc:384
Owns the component command-service handle and optional thread initialization.
Definition: mrs_component_mysql_command_service_session.h:45
struct Security_context_handle_imp * Security_context_handle
Definition: dynamic_privilege.h:29
#define T
Definition: jit_executor_value.cc:373
struct MYSQL_RES_H_imp * MYSQL_RES_H
Definition: mysql_command_services.h:34
struct my_h_row_imp * my_h_row
Definition: mysql_statement_service.h:36
struct my_h_statement_imp * my_h_statement
Definition: mysql_statement_service.h:35
const char * host
Definition: mysqladmin.cc:66
constexpr value_type is_unsigned
Definition: classic_protocol_constants.h:273
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: mrs_component.cc:137
std::vector< Row > Resultset
Definition: mrs_component_command_executor.h:43
std::vector< std::string > Row
Definition: mrs_component_command_executor.h:42
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Define std::hash<Gtid>.
Definition: gtid.h:355
required string type
Definition: replication_group_member_actions.proto:34
Definition: classic_prepared_statement.h:33
Definition: result.h:30