MySQL 9.3.0
Source Code Documentation
session.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, 2025, 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, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, 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 Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26// MySQL DB access module, for use by plugins and others
27// For the module that implements interactive DB functionality see mod_db
28
29#ifndef MYSQLSHDK_LIBS_DB_MYSQL_SESSION_H_
30#define MYSQLSHDK_LIBS_DB_MYSQL_SESSION_H_
31
32#include <mysql.h>
33#include <mysqld_error.h>
34
35#include <cstring>
36#include <functional>
37#include <memory>
38#include <optional>
39#include <set>
40#include <string>
41#include <string_view>
42#include <vector>
43
45#include "database/result.h"
47#include "mysqlrouter/jit_executor_plugin_export.h"
49
50namespace shcore {
51namespace polyglot {
52namespace database {
53
56
57class JIT_EXECUTOR_PLUGIN_EXPORT Session
58 : public ISession,
59 public std::enable_shared_from_this<Session> {
60 friend class DbResult; // The Result class uses some functions of this class
61 public:
63 ~Session() override;
64
65 // Ensures the connection is ready for the next query execution
66 void reset() override;
67
68 private:
69 std::shared_ptr<IResult> query(
70 const char *sql, size_t len, bool buffered,
71 const std::vector<Query_attribute> &query_attributes = {});
72 std::shared_ptr<IResult> query_udf(std::string_view sql, bool buffered);
73 void execute(const char *sql, size_t len);
74
75 inline void execute(const char *sql) { execute(sql, ::strlen(sql)); }
76
78 void commit();
79 void rollback();
80
81 bool next_resultset();
82 void prepare_fetch(DbResult *target);
83
84 std::string uri() { return _uri; }
85
86 // Utility functions to retrieve session status
87 uint64_t get_thread_id() const { return m_thread_id; }
88
89 uint64_t get_protocol_info() {
90 if (_mysql) return mysql_get_proto_info(_mysql);
91 return 0;
92 }
94 return _mysql ? _mysql->net.compress : false;
95 }
96 const char *get_connection_info() {
97 if (_mysql) return mysql_get_host_info(_mysql);
98 return nullptr;
99 }
100 const char *get_server_info() {
101 if (_mysql) return mysql_get_server_info(_mysql);
102 return nullptr;
103 }
104 const char *get_stats() {
105 _prev_result.reset();
106 if (_mysql) return mysql_stat(_mysql);
107 return nullptr;
108 }
109 const char *get_ssl_cipher() {
110 if (_mysql) return mysql_get_ssl_cipher(_mysql);
111 return nullptr;
112 }
113
114 const char *get_mysql_info() const { return mysql_info(_mysql); }
115
116 bool is_open() const { return _mysql ? true : false; }
117
118 const char *get_last_error(int *out_code, const char **out_sqlstate) {
119 if (out_code) *out_code = mysql_errno(_mysql);
120 if (out_sqlstate) *out_sqlstate = mysql_sqlstate(_mysql);
121
122 return mysql_error(_mysql);
123 }
124
125 std::vector<std::string> get_last_gtids() const;
126 std::optional<std::string> get_last_statement_id() const;
127
128 uint32_t get_server_status() const {
129 return _mysql ? _mysql->server_status : 0;
130 }
131
132 uint64_t warning_count() const {
133 return _mysql ? mysql_warning_count(_mysql) : 0;
134 }
135
136 // TODO(rennox): these functions go on the high level object
137 void set_query_attributes(const shcore::Dictionary_t &args);
138 std::vector<Query_attribute> query_attributes() const;
139
140 std::shared_ptr<IResult> run_sql(const std::string &sql) override;
141
142 std::shared_ptr<IResult> run_sql(
143 const char *sql, size_t len, bool lazy_fetch, bool is_udf,
144 const std::vector<Query_attribute> &query_attributes = {});
145
146 MYSQL *get_handle() { return _mysql; }
147
148 std::string _uri;
149 MYSQL *_mysql = nullptr;
150 std::shared_ptr<MYSQL_RES> _prev_result;
151 uint64_t m_thread_id = 0;
152
154 int (*init)(void **, const char *, void *) = nullptr;
155 int (*read)(void *, char *, unsigned int) = nullptr;
156 void (*end)(void *) = nullptr;
157 int (*error)(void *, char *, unsigned int) = nullptr;
158 void *userdata = nullptr;
159 };
162};
163
164} // namespace database
165} // namespace polyglot
166} // namespace shcore
167#endif // MYSQLSHDK_LIBS_DB_MYSQL_SESSION_H_
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:566
Definition: jit_executor_db_interface.h:151
Definition: jit_executor_db_interface.h:186
Cache for query attributes to be associated to the next user SQL executed.
Definition: query_attributes.h:115
Definition: session.h:59
const char * get_last_error(int *out_code, const char **out_sqlstate)
Definition: session.h:118
MYSQL * get_handle()
Definition: session.h:146
const char * get_server_info()
Definition: session.h:100
std::string _uri
Definition: session.h:148
void execute(const char *sql)
Definition: session.h:75
const char * get_mysql_info() const
Definition: session.h:114
Local_infile_callbacks m_local_infile
Definition: session.h:160
bool is_open() const
Definition: session.h:116
const char * get_connection_info()
Definition: session.h:96
uint64_t get_thread_id() const
Definition: session.h:87
uint64_t get_protocol_info()
Definition: session.h:89
uint32_t get_server_status() const
Definition: session.h:128
const char * get_stats()
Definition: session.h:104
uint64_t warning_count() const
Definition: session.h:132
bool is_compression_enabled() const
Definition: session.h:93
const char * get_ssl_cipher()
Definition: session.h:109
Query_attribute_store m_query_attributes
Definition: session.h:161
std::string uri()
Definition: session.h:84
std::shared_ptr< MYSQL_RES > _prev_result
Definition: session.h:150
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:1831
static char * query
Definition: myisam_ftdump.cc:47
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:9536
unsigned int STDCALL mysql_errno(MYSQL *mysql)
Definition: client.cc:9194
const char *STDCALL mysql_info(MYSQL *mysql)
Definition: client.cc:9383
const char *STDCALL mysql_get_host_info(MYSQL *mysql)
Definition: libmysql.cc:901
unsigned int STDCALL mysql_warning_count(MYSQL *mysql)
Definition: libmysql.cc:932
const char *STDCALL mysql_stat(MYSQL *mysql)
Definition: libmysql.cc:881
unsigned int STDCALL mysql_get_proto_info(MYSQL *mysql)
Definition: libmysql.cc:905
const char *STDCALL mysql_get_server_info(MYSQL *mysql)
Definition: libmysql.cc:897
const char *STDCALL mysql_get_ssl_cipher(MYSQL *mysql)
Definition: client.cc:3493
const char *STDCALL mysql_error(MYSQL *mysql)
Definition: client.cc:9198
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
constexpr value_type query_attributes
Definition: classic_protocol_constants.h:65
AuthorizeManager::Session Session
Definition: authorize_manager.cc:75
Definition: instrumented_condition_variable.h:32
Definition: file_system_exceptions.h:34
Value::Map_type_ref Dictionary_t
Definition: jit_executor_value.h:430
Definition: mysql.h:300