MySQL 8.0.39
Source Code Documentation
common.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 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 MYSQL_HARNESS_COMMON_INCLUDED
27#define MYSQL_HARNESS_COMMON_INCLUDED
28
29#include <cstdlib>
30#include <functional>
31#include <map>
32#include <sstream>
33#include <string>
34#include "harness_export.h"
35
36/**
37 * @defgroup Various operations
38 *
39 * This module contain various utility operations.
40 */
41
42namespace mysql_harness {
43
44/**
45 * Return a truncated version of input string.
46 *
47 * @param str input text
48 * @param max_len maximum length after truncation
49 * @return truncated string
50 */
51HARNESS_EXPORT
52std::string truncate_string(const std::string &str, size_t max_len = 80);
53
54/**
55 * Emit a range of elements using the serial comma.
56 *
57 * This function can be used to output a range of elements using a
58 * serial comma (also known as the Oxford comma). To emit a list of
59 * the first five prime numbers as "The first five prime numbers are
60 * 2, 3, 5, 7, and 11":
61 *
62 * @code
63 * std::vector<int> primes{2, 3, 5, 7, 11};
64 * std::cout << "The first five prime numbers are ";
65 * serial_comma(std::cout, primes.begin(), primes.end());
66 * std::cout << std::endl;
67 * @endcode
68 *
69 * @param out Output stream
70 * @param start Input iterator to start of range.
71 * @param finish Input iterator to one-after-end of range.
72 * @param delim Delimiter to use. Defaults to "and".
73 */
74template <class InputIt>
75void serial_comma(std::ostream &out, InputIt start, InputIt finish,
76 const std::string &delim = "and") {
77 auto elements = std::distance(start, finish);
78 if (elements == 1) {
79 out << *start;
80 } else if (elements == 2) {
81 out << *start++;
82 out << " " << delim << " " << *start;
83 } else {
84 while (elements-- > 0) {
85 out << *start++;
86 if (elements > 0) out << ", ";
87 if (elements == 1) out << delim << " ";
88 }
89 }
90}
91
92/**
93 * Returns string containing list of the elements using the serial comma.
94 *
95 * This function can be used to output a range of elements using a
96 * serial comma (also known as the Oxford comma). To return a list of
97 * the first five prime numbers as "The first five prime numbers are
98 * 2, 3, 5, 7, and 11":
99 *
100 * @code
101 * std::vector<int> primes{2, 3, 5, 7, 11};
102 * std::cout << "The first five prime numbers are "
103 * << serial_comma(primes.begin(), primes.end()) << std::endl;
104 * @endcode
105 *
106 * @param start Input iterator to start of range.
107 * @param finish Input iterator to one-after-end of range.
108 * @param delim Delimiter to use. Defaults to "and".
109 *
110 * @return string containing list of the elements
111 */
112template <class InputIt>
113std::string serial_comma(InputIt start, InputIt finish,
114 const std::string &delim = "and") {
115 std::stringstream out;
116 serial_comma(out, start, finish, delim);
117
118 return out.str();
119}
120
121/**
122 * Returns string containing list of the elements separated by selected
123 * delimiter.
124 *
125 * To return a list of the first five prime numbers as "The first five prime
126 * numbers are 2, 3, 5, 7, 11":
127 *
128 * @code
129 * std::vector<int> primes{2, 3, 5, 7, 11};
130 * std::cout << "The first five prime numbers are "
131 * << list_elements(primes.begin(), primes.end()) << std::endl;
132 * @endcode
133 *
134 * @param start Input iterator to start of range.
135 * @param finish Input iterator to one-after-end of range.
136 * @param delim Delimiter to use. Defaults to ",".
137 *
138 * @return string containing list of the elements
139 */
140template <class InputIt>
141std::string list_elements(InputIt start, InputIt finish,
142 const std::string &delim = ",") {
143 std::string result;
144 for (auto cur = start; cur != finish; ++cur) {
145 if (cur != start) result += delim;
146 result += *cur;
147 }
148
149 return result;
150}
151
152/**
153 * Returns string containing list of the elements separated by selected
154 * delimiter.
155 *
156 * To return a list of the first five prime numbers as "The first five prime
157 * numbers are 2, 3, 5, 7, 11":
158 *
159 * @code
160 * std::vector<int> primes{2, 3, 5, 7, 11};
161 * std::cout << "The first five prime numbers are "
162 * << list_elements(primes) << std::endl;
163 * @endcode
164 *
165 * @param collection Collection of the elements to output.
166 * @param delim Delimiter to use. Defaults to ",".
167 *
168 * @return string containing list of the elements
169 */
170template <class Collection>
171std::string list_elements(Collection collection,
172 const std::string &delim = ",") {
173 return list_elements(collection.begin(), collection.end(), delim);
174}
175
176/**
177 * Gets a Value from std::map for given Key. Returns provided default if the Key
178 * is not in the map.
179 */
180template <class Key, class Value>
181Value get_from_map(const std::map<Key, Value> &map, const Key &key,
182 const Value &default_value) {
183 auto iter = map.find(key);
184 if (iter == map.end()) return default_value;
185 return iter->second;
186}
187
188} // namespace mysql_harness
189
190#endif /* MYSQL_HARNESS_COMMON_INCLUDED */
a nullable SQL value.
Definition: sql_value.h:40
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:177
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:40
std::string_view Key
The key type for the hash structure in HashJoinRowBuffer.
Definition: hash_join_buffer.h:102
Definition: common.h:42
HARNESS_EXPORT std::string truncate_string(const std::string &str, size_t max_len=80)
Return a truncated version of input string.
Definition: common.cc:32
Value get_from_map(const std::map< Key, Value > &map, const Key &key, const Value &default_value)
Gets a Value from std::map for given Key.
Definition: common.h:181
void serial_comma(std::ostream &out, InputIt start, InputIt finish, const std::string &delim="and")
Emit a range of elements using the serial comma.
Definition: common.h:75
std::string list_elements(InputIt start, InputIt finish, const std::string &delim=",")
Returns string containing list of the elements separated by selected delimiter.
Definition: common.h:141
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2892
struct result result
Definition: result.h:34
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: result.h:30