MySQL 8.3.0
Source Code Documentation
sql_exec_context.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef ROUTING_SQL_EXEC_CONTEXT_INCLUDED
26#define ROUTING_SQL_EXEC_CONTEXT_INCLUDED
27
28#include <map>
29#include <optional>
30#include <string>
31#include <vector>
32
34#include "sql_value.h"
35
36/**
37 * execution context for SQL.
38 *
39 * - system-variables
40 * - diagnostics area
41 */
43 public:
44 /**
45 * system-variables as returned by the server.
46 *
47 * can be queried from the server with:
48 *
49 * - SELECT @@SESSION.{k}
50 * - SELECT @@LOCAL.{k}
51 *
52 * can be set on the server with:
53 *
54 * - SET k = v;
55 * - SET @@SESSION.k = v;
56 * - SET @@LOCAL.k = v;
57 * - SET SESSION k = v;
58 * - SET LOCAL k = v;
59 *
60 * changes to system-vars on the server are returned via
61 * the sesssion-tracker for system-variables.
62 */
64 public:
65 using key_type = std::string;
66 using key_view_type = std::string_view;
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_view_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_view_type &k) const {
108 const auto it = vars_.find(k);
109 if (it == vars_.end()) return {std::nullopt};
110
111 return it->second;
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, std::less<>> 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:63
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::string key_type
Definition: sql_exec_context.h:65
std::string_view key_view_type
Definition: sql_exec_context.h:66
value_type get(const key_view_type &k) const
get 'k' from system-vars.
Definition: sql_exec_context.h:107
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
std::map< key_type, value_type, std::less<> > vars_
Definition: sql_exec_context.h:128
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::optional< value_type > find(const key_view_type &k) const
find 'k' in sytem-vars.
Definition: sql_exec_context.h:88
execution context for SQL.
Definition: sql_exec_context.h:42
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:39
Definition: varlen_sort.h:174