MySQL 8.4.2
Source Code Documentation
utils_sqlstring.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 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 _UTILS_SQLSTRING_H_
27#define _UTILS_SQLSTRING_H_
28
30
31#ifndef __STDC_FORMAT_MACROS
32#define __STDC_FORMAT_MACROS 1
33#endif
34#include <inttypes.h>
35#include <string>
36
37#include <stdexcept>
38
39namespace mysqlrouter {
40
43 UseAnsiQuotes = 1 << 1,
44
45 EndOfInput = 1 << 7
46};
47
48std::string ROUTER_MYSQL_EXPORT
49escape_sql_string(const std::string &string,
50 bool wildcards = false); // "strings" or 'strings'
51std::string ROUTER_MYSQL_EXPORT
52escape_backticks(const std::string &string); // `identifier`
53std::string ROUTER_MYSQL_EXPORT quote_identifier(const std::string &identifier,
54 const char quote_char);
55std::string ROUTER_MYSQL_EXPORT
56quote_identifier_if_needed(const std::string &ident, const char quote_char);
57
59 public:
61 int _flags;
62 sqlstringformat(const int flags) : _flags(flags) {}
63 };
64
65 private:
66 std::string _formatted;
69
70 std::string consume_until_next_escape();
71 int next_escape();
72
73 sqlstring &append(const std::string &s);
74
75 public:
76 static const sqlstring null;
77 static const sqlstring end;
78
79 sqlstring();
80 sqlstring(const char *format_string, const sqlstringformat format = 0);
82 sqlstring &operator=(const sqlstring &) = default;
83 bool done() const;
84
85 operator std::string() const;
86 std::string str() const;
87
88 //! modifies formatting options
90 //! replaces a ? in the format string with a float numeric value
91 sqlstring &operator<<(const float val) { return operator<<((double)val); }
92 //! replaces a ? in the format string with a double numeric value
93 sqlstring &operator<<(const double);
94 //! replaces a ? in the format string with a quoted string value or ! with a
95 //! back-quoted identifier value
96 sqlstring &operator<<(const std::string &);
97 //! replaces a ? in the format string with a quoted string value or ! with a
98 //! back-quoted identifier value is the value is NULL, ? will be replaced with
99 //! a NULL. ! will raise an exception
100 sqlstring &operator<<(const char *);
101 //! replaces a ? or ! with the content of the other string verbatim
103 //! replaces a ? in the format string with any integer numeric value
104 template <typename T>
105 sqlstring &operator<<(const T value) {
106 int esc = next_escape();
107 if (esc != '?')
108 throw std::invalid_argument(
109 "Error formatting SQL query: invalid escape for numeric argument");
110 append(std::to_string(value));
111 append(consume_until_next_escape());
112 return *this;
113 }
114};
115} // namespace mysqlrouter
116
117#endif
Definition: utils_sqlstring.h:58
sqlstringformat _format
Definition: utils_sqlstring.h:68
sqlstring(const sqlstring &copy)
std::string _formatted
Definition: utils_sqlstring.h:66
sqlstring & operator=(const sqlstring &)=default
static const sqlstring end
Definition: utils_sqlstring.h:77
sqlstring & operator<<(const T value)
replaces a ? in the format string with any integer numeric value
Definition: utils_sqlstring.h:105
sqlstring & operator<<(const float val)
replaces a ? in the format string with a float numeric value
Definition: utils_sqlstring.h:91
std::string _format_string_left
Definition: utils_sqlstring.h:67
static int flags[50]
Definition: hp_test1.cc:40
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:354
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
Definition: dim.h:358
SqlStringFlags
Definition: utils_sqlstring.h:41
@ UseAnsiQuotes
Definition: utils_sqlstring.h:43
@ EndOfInput
Definition: utils_sqlstring.h:45
@ QuoteOnlyIfNeeded
Definition: utils_sqlstring.h:42
std::string ROUTER_MYSQL_EXPORT escape_backticks(const std::string &string)
Definition: utils_sqlstring.cc:326
ROUTER_UTILS_EXPORT std::ostream & operator<<(std::ostream &strm, const URI &uri)
Definition: uri.cc:1205
std::string ROUTER_MYSQL_EXPORT quote_identifier(const std::string &identifier, const char quote_char)
Definition: utils_sqlstring.cc:371
std::string ROUTER_MYSQL_EXPORT escape_sql_string(const std::string &string, bool wildcards=false)
Escape a string to be used in a SQL query Same code as used by mysql.
Definition: utils_sqlstring.cc:277
std::string ROUTER_MYSQL_EXPORT quote_identifier_if_needed(const std::string &ident, const char quote_char)
Quotes the given identifier, but only if it needs to be quoted.
Definition: utils_sqlstring.cc:384
#define ROUTER_MYSQL_EXPORT
Definition: router_mysql_export.h:15
Definition: utils_sqlstring.h:60
int _flags
Definition: utils_sqlstring.h:61
sqlstringformat(const int flags)
Definition: utils_sqlstring.h:62