- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathquery_string_builder.h
160 lines (128 loc) · 4.95 KB
/
query_string_builder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0,
* as published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms,
* as designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PLUGIN_X_SRC_QUERY_STRING_BUILDER_H_
#definePLUGIN_X_SRC_QUERY_STRING_BUILDER_H_
#include<stdint.h>
#include<string.h>
#include<mutex>// NOLINT(build/c++11)
#include<string>
#include"plugin/x/src/ngs/memory.h"
#include"plugin/x/src/ngs/thread.h"
#include"plugin/x/src/query_formatter.h"
structCHARSET_INFO;
namespacexpl {
classQuery_string_builder {
public:
explicitQuery_string_builder(size_t reserve = 256);
Query_string_builder &bquote() {
m_str.push_back('\'');
m_in_quoted = true;
return *this;
}
Query_string_builder &equote() {
m_str.push_back('\'');
m_in_quoted = false;
return *this;
}
Query_string_builder &bident() {
m_str.push_back('`');
m_in_identifier = true;
return *this;
}
Query_string_builder &eident() {
m_str.push_back('`');
m_in_identifier = false;
return *this;
}
Query_string_builder "e_identifier_if_needed(constchar *s,
size_t length);
Query_string_builder "e_identifier(constchar *s, size_t length);
Query_string_builder "e_string(constchar *s, size_t length);
Query_string_builder "e_json_string(constchar *s, size_t length);
Query_string_builder "e_identifier_if_needed(const std::string &s) {
returnquote_identifier_if_needed(s.data(), s.length());
}
Query_string_builder "e_identifier(const std::string &s) {
returnquote_identifier(s.data(), s.length());
}
Query_string_builder "e_string(const std::string &s) {
returnquote_string(s.data(), s.length());
}
Query_string_builder "e_json_string(const std::string &s) {
returnquote_json_string(s.data(), s.length());
}
Query_string_builder &escape_identifier(constchar *s, size_t length);
Query_string_builder &escape_string(constchar *s, size_t length);
Query_string_builder &escape_json_string(constchar *s, size_t length);
Query_string_builder &dot() { returnput(".", 1); }
Query_string_builder &put(constint64_t i) { returnput(to_string(i)); }
Query_string_builder &put(constuint64_t u) { returnput(to_string(u)); }
Query_string_builder &put(constint32_t i) { returnput(to_string(i)); }
Query_string_builder &put(constuint32_t u) { returnput(to_string(u)); }
Query_string_builder &put(constfloat f) { returnput(to_string(f)); }
Query_string_builder &put(constdouble d) { returnput(to_string(d)); }
Query_string_builder &put(constchar *s, size_t length);
Query_formatter format();
Query_string_builder &put(constchar *s) { returnput(s, strlen(s)); }
Query_string_builder &put(const std::string &s) {
returnput(s.data(), s.length());
}
Query_string_builder &put(const ngs::PFS_string &s) {
returnput(s.data(), s.length());
}
template <typename I>
Query_string_builder &put_list(I begin, I end, const std::string &sep = ",") {
if (std::distance(begin, end) == 0) return *this;
put(*begin);
for (++begin; begin != end; ++begin) {
put(sep);
put(*begin);
}
return *this;
}
template <typename I, typename P>
Query_string_builder &put_list(I begin, I end, P push,
const std::string &sep = ",") {
if (std::distance(begin, end) == 0) return *this;
push(*begin, this);
for (++begin; begin != end; ++begin) {
put(sep);
push(*begin, this);
}
return *this;
}
voidclear() { m_str.clear(); }
voidreserve(size_t bytes) { m_str.reserve(bytes); }
const ngs::PFS_string &get() const { return m_str; }
private:
ngs::PFS_string m_str;
bool m_in_quoted;
bool m_in_identifier;
staticvoidinit_charset();
static std::once_flag m_charset_initialized;
static CHARSET_INFO *m_charset;
};
} // namespace xpl
#endif// PLUGIN_X_SRC_QUERY_STRING_BUILDER_H_