MySQL 8.4.0
Source Code Documentation
sql_exec_context.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2024, 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 ROUTING_SQL_EXEC_CONTEXT_INCLUDED
27#define ROUTING_SQL_EXEC_CONTEXT_INCLUDED
28
29#include <map>
30#include <optional>
31#include <string>
32#include <vector>
33
35#include "sql_value.h"
36
37/**
38 * execution context for SQL.
39 *
40 * - system-variables
41 * - diagnostics area
42 */
44 public:
45 /**
46 * system-variables as returned by the server.
47 *
48 * can be queried from the server with:
49 *
50 * - SELECT @@SESSION.{k}
51 * - SELECT @@LOCAL.{k}
52 *
53 * can be set on the server with:
54 *
55 * - SET k = v;
56 * - SET @@SESSION.k = v;
57 * - SET @@LOCAL.k = v;
58 * - SET SESSION k = v;
59 * - SET LOCAL k = v;
60 *
61 * changes to system-vars on the server are returned via
62 * the sesssion-tracker for system-variables.
63 */
65 public:
66 using key_type = std::string;
67 using key_view_type = std::string_view;
68 using value_type = Value; // aka std::optional<std::string>
69
70 /**
71 * set k to v.
72 *
73 * if k doesn't exist in the system-vars yet, it gets inserted.
74 */
75 void set(key_type k, value_type v) {
76 vars_.insert_or_assign(std::move(k), std::move(v));
77 }
78
79 /**
80 * find 'k' in sytem-vars.
81 *
82 * @param k key
83 *
84 * if 'k' does not exist in system-vars, a NULL-like value is returned.
85 * otherwise return the value for the system-var referenced by 'k'
86 *
87 * @returns std::nullopt if key is not found, the found value otherwise.
88 */
89 std::optional<value_type> find(const key_view_type &k) const {
90 const auto it = vars_.find(k);
91 if (it == vars_.end()) return {std::nullopt};
92
93 return it->second;
94 }
95
96 /**
97 * get 'k' from system-vars.
98 *
99 * @param k key
100 *
101 * if 'k' does not exist in system-vars, a NULL-like value is returned.
102 * otherwise return the value for the system-var referenced by 'k' which may
103 * be NULL-like or a string.
104 *
105 * @returns std::nullopt if key is not found or value is NULL-like, the
106 * found value otherwise
107 */
108 value_type get(const key_view_type &k) const {
109 const auto it = vars_.find(k);
110 if (it == vars_.end()) return {std::nullopt};
111
112 return it->second;
113 }
114
115 using iterator = std::map<key_type, value_type>::iterator;
116 using const_iterator = std::map<key_type, value_type>::const_iterator;
117
118 iterator begin() { return vars_.begin(); }
119 const_iterator begin() const { return vars_.begin(); }
120 iterator end() { return vars_.end(); }
121 const_iterator end() const { return vars_.end(); }
122
123 /**
124 * check if their is a no system-var.
125 */
126 bool empty() const { return vars_.empty(); }
127
128 /**
129 * clear the system-vars.
130 */
131 void clear() { vars_.clear(); }
132
133 private:
134 std::map<key_type, value_type, std::less<>> vars_;
135 };
136
137 /**
138 * diagnostics area.
139 *
140 * - warnings, errors and notes.
141 *
142 * used by:
143 *
144 * - SHOW WARNINGS
145 * - SHOW ERRORS
146 * - SHOW COUNT(*) WARNINGS
147 * - SHOW COUNT(*) ERRORS
148 * - SELECT @@warning_count
149 * - SELECT @@error_count
150 */
152 public:
153 class Warning {
154 public:
155 Warning(std::string level, uint64_t code, std::string msg)
156 : level_(std::move(level)), code_(code), msg_{std::move(msg)} {}
157
158 std::string level() const { return level_; }
159 uint64_t code() const { return code_; }
160 std::string message() const { return msg_; }
161
162 private:
163 std::string level_;
164 uint64_t code_;
165 std::string msg_;
166 };
167
168 std::vector<Warning> &warnings() { return warnings_; }
169 const std::vector<Warning> &warnings() const { return warnings_; }
170
171 private:
172 std::vector<Warning> warnings_;
173 };
174
176
178
180
182
183 private:
185
187};
188
189#endif
Definition: sql_exec_context.h:153
std::string level_
Definition: sql_exec_context.h:163
std::string msg_
Definition: sql_exec_context.h:165
uint64_t code_
Definition: sql_exec_context.h:164
std::string level() const
Definition: sql_exec_context.h:158
Warning(std::string level, uint64_t code, std::string msg)
Definition: sql_exec_context.h:155
std::string message() const
Definition: sql_exec_context.h:160
uint64_t code() const
Definition: sql_exec_context.h:159
diagnostics area.
Definition: sql_exec_context.h:151
const std::vector< Warning > & warnings() const
Definition: sql_exec_context.h:169
std::vector< Warning > warnings_
Definition: sql_exec_context.h:172
std::vector< Warning > & warnings()
Definition: sql_exec_context.h:168
system-variables as returned by the server.
Definition: sql_exec_context.h:64
void set(key_type k, value_type v)
set k to v.
Definition: sql_exec_context.h:75
const_iterator begin() const
Definition: sql_exec_context.h:119
std::string key_type
Definition: sql_exec_context.h:66
void clear()
clear the system-vars.
Definition: sql_exec_context.h:131
std::string_view key_view_type
Definition: sql_exec_context.h:67
value_type get(const key_view_type &k) const
get 'k' from system-vars.
Definition: sql_exec_context.h:108
iterator begin()
Definition: sql_exec_context.h:118
std::map< key_type, value_type >::iterator iterator
Definition: sql_exec_context.h:115
const_iterator end() const
Definition: sql_exec_context.h:121
std::map< key_type, value_type >::const_iterator const_iterator
Definition: sql_exec_context.h:116
std::map< key_type, value_type, std::less<> > vars_
Definition: sql_exec_context.h:134
bool empty() const
check if their is a no system-var.
Definition: sql_exec_context.h:126
iterator end()
Definition: sql_exec_context.h:120
std::optional< value_type > find(const key_view_type &k) const
find 'k' in sytem-vars.
Definition: sql_exec_context.h:89
execution context for SQL.
Definition: sql_exec_context.h:43
const SystemVariables & system_variables() const
Definition: sql_exec_context.h:181
SystemVariables system_variables_
Definition: sql_exec_context.h:184
DiagnosticsArea & diagnostics_area()
Definition: sql_exec_context.h:175
DiagnosticsArea diagnostics_area_
Definition: sql_exec_context.h:186
const DiagnosticsArea & diagnostics_area() const
Definition: sql_exec_context.h:177
SystemVariables & system_variables()
Definition: sql_exec_context.h:179
a nullable SQL value.
Definition: sql_value.h:40
Definition: gcs_xcom_synode.h:64