MySQL 8.0.39
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 value_type = Value; // aka std::optional<std::string>
68
69 /**
70 * set k to v.
71 *
72 * if k doesn't exist in the system-vars yet, it gets inserted.
73 */
74 void set(key_type k, value_type v) {
75 vars_.insert_or_assign(std::move(k), std::move(v));
76 }
77
78 /**
79 * find 'k' in sytem-vars.
80 *
81 * @param k key
82 *
83 * if 'k' does not exist in system-vars, a NULL-like value is returned.
84 * otherwise return the value for the system-var referenced by 'k'
85 *
86 * @returns std::nullopt if key is not found, the found value otherwise.
87 */
88 std::optional<value_type> find(const key_type &k) const {
89 const auto it = vars_.find(k);
90 if (it == vars_.end()) return {std::nullopt};
91
92 return it->second;
93 }
94
95 /**
96 * get 'k' from system-vars.
97 *
98 * @param k key
99 *
100 * if 'k' does not exist in system-vars, a NULL-like value is returned.
101 * otherwise return the value for the system-var referenced by 'k' which may
102 * be NULL-like or a string.
103 *
104 * @returns std::nullopt if key is not found or value is NULL-like, the
105 * found value otherwise
106 */
107 value_type get(const key_type &k) const {
108 const auto res = find(k);
109 if (!res) return {std::nullopt};
110
111 return *res;
112 }
113
114 using iterator = std::map<key_type, value_type>::iterator;
115 using const_iterator = std::map<key_type, value_type>::const_iterator;
116
117 iterator begin() { return vars_.begin(); }
118 const_iterator begin() const { return vars_.begin(); }
119 iterator end() { return vars_.end(); }
120 const_iterator end() const { return vars_.end(); }
121
122 /**
123 * check if their is a no system-var.
124 */
125 bool empty() const { return vars_.empty(); }
126
127 private:
128 std::map<key_type, value_type> vars_;
129 };
130
131 /**
132 * diagnostics area.
133 *
134 * - warnings, errors and notes.
135 *
136 * used by:
137 *
138 * - SHOW WARNINGS
139 * - SHOW ERRORS
140 * - SHOW COUNT(*) WARNINGS
141 * - SHOW COUNT(*) ERRORS
142 * - SELECT @@warning_count
143 * - SELECT @@error_count
144 */
146 public:
147 class Warning {
148 public:
149 Warning(std::string level, uint64_t code, std::string msg)
150 : level_(std::move(level)), code_(code), msg_{std::move(msg)} {}
151
152 std::string level() const { return level_; }
153 uint64_t code() const { return code_; }
154 std::string message() const { return msg_; }
155
156 private:
157 std::string level_;
158 uint64_t code_;
159 std::string msg_;
160 };
161
162 std::vector<Warning> &warnings() { return warnings_; }
163 const std::vector<Warning> &warnings() const { return warnings_; }
164
165 private:
166 std::vector<Warning> warnings_;
167 };
168
170
172
174
176
177 private:
179
181};
182
183#endif
Definition: sql_exec_context.h:147
std::string level_
Definition: sql_exec_context.h:157
std::string msg_
Definition: sql_exec_context.h:159
uint64_t code_
Definition: sql_exec_context.h:158
std::string level() const
Definition: sql_exec_context.h:152
Warning(std::string level, uint64_t code, std::string msg)
Definition: sql_exec_context.h:149
std::string message() const
Definition: sql_exec_context.h:154
uint64_t code() const
Definition: sql_exec_context.h:153
diagnostics area.
Definition: sql_exec_context.h:145
const std::vector< Warning > & warnings() const
Definition: sql_exec_context.h:163
std::vector< Warning > warnings_
Definition: sql_exec_context.h:166
std::vector< Warning > & warnings()
Definition: sql_exec_context.h:162
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:74
const_iterator begin() const
Definition: sql_exec_context.h:118
std::optional< value_type > find(const key_type &k) const
find 'k' in sytem-vars.
Definition: sql_exec_context.h:88
value_type get(const key_type &k) const
get 'k' from system-vars.
Definition: sql_exec_context.h:107
std::string key_type
Definition: sql_exec_context.h:66
iterator begin()
Definition: sql_exec_context.h:117
std::map< key_type, value_type >::iterator iterator
Definition: sql_exec_context.h:114
const_iterator end() const
Definition: sql_exec_context.h:120
std::map< key_type, value_type >::const_iterator const_iterator
Definition: sql_exec_context.h:115
bool empty() const
check if their is a no system-var.
Definition: sql_exec_context.h:125
iterator end()
Definition: sql_exec_context.h:119
std::map< key_type, value_type > vars_
Definition: sql_exec_context.h:128
execution context for SQL.
Definition: sql_exec_context.h:43
const SystemVariables & system_variables() const
Definition: sql_exec_context.h:175
SystemVariables system_variables_
Definition: sql_exec_context.h:178
DiagnosticsArea & diagnostics_area()
Definition: sql_exec_context.h:169
DiagnosticsArea diagnostics_area_
Definition: sql_exec_context.h:180
const DiagnosticsArea & diagnostics_area() const
Definition: sql_exec_context.h:171
SystemVariables & system_variables()
Definition: sql_exec_context.h:173
a nullable SQL value.
Definition: sql_value.h:40
Definition: gcs_xcom_synode.h:64