MySQL Connector/C++
MySQL connector library for C and C++ applications
crud.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, as
6 * 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, as
10 * 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 * Without limiting anything contained in the foregoing, this file,
17 * which is part of Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * https://oss.oracle.com/licenses/universal-foss-exception.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License, version 2.0, for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31#ifndef MYSQLX_DETAIL_CRUD_H
32#define MYSQLX_DETAIL_CRUD_H
33
40#include "../common.h"
41#include "../executable.h"
42
43
44namespace mysqlx {
45MYSQLX_ABI_BEGIN(2,0)
46
47namespace internal {
48
49
50struct PUBLIC_API Bind_detail
51{
52protected:
53
54 using Impl = common::Bind_if;
55 using Args_prc = Args_processor<Bind_detail, Impl*>;
56
57 static void process_one(Impl *impl, const Value &val)
58 {
59 impl->add_param((const common::Value&)val);
60 }
61
62 template <typename... T>
63 static void add_params(Impl *impl, T&&... vals)
64 {
65 Args_prc::process_args(impl, std::forward<T>(vals)...);
66 }
67
68 friend Args_prc;
69};
70
71
72struct PUBLIC_API Sort_detail
73{
74protected:
75
76 using Impl = common::Sort_if;
77 using Args_prc = Args_processor<Sort_detail, Impl*>;
78
79 static void process_one(Impl *impl, const string &ord_spec)
80 {
81 impl->add_sort(ord_spec);
82 }
83
84 template <typename... T>
85 static void add_sort(Impl *impl, T... args)
86 {
87 Args_prc::process_args(impl, args...);
88 }
89
90 friend Args_prc;
91};
92
93
94struct PUBLIC_API Group_by_detail
95{
96protected:
97
98 using Impl = common::Group_by_if;
99 using Args_prc = Args_processor<Group_by_detail, Impl*>;
100
101 static void process_one(Impl *impl, const string &spec)
102 {
103 impl->add_group_by(spec);
104 }
105
106 template <typename... T>
107 static void do_group_by(Impl *impl, T... args)
108 {
109 Args_prc::process_args(impl, args...);
110 }
111
112 friend Args_prc;
113};
114
115
116struct PUBLIC_API Proj_detail
117{
118protected:
119
120 using Impl = common::Proj_if;
121 using Args_prc = Args_processor<Proj_detail, Impl*>;
122
123 static void process_one(Impl *impl, const string &spec)
124 {
125 impl->add_proj(spec);
126 }
127
128 template <typename... T>
129 static void add_proj(Impl *impl, T... proj_spec)
130 {
131 Args_prc::process_args(impl, proj_spec...);
132 }
133
134 friend Args_prc;
135};
136
137
138struct PUBLIC_API Collection_add_detail
139{
140protected:
141
142 using Impl = common::Collection_add_if;
143 using Args_prc = Args_processor<Collection_add_detail, Impl*>;
144
145 static void process_one(Impl *impl, const string &json)
146 {
147 impl->add_json(json);
148 }
149
150 static void process_one(Impl *impl, const DbDoc &doc)
151 {
152 // TODO: Do it better when we support sending structured
153 // document descriptions to the server.
154
155 std::ostringstream buf;
156 buf << doc;
157 // Note: utf8 conversion using mysqlx::string.
158 impl->add_json(mysqlx::string(buf.str()));
159 }
160
161 template <typename... T>
162 static void do_add(Impl *impl, T... args)
163 {
164 Args_prc::process_args(impl, args...);
165 }
166
167 friend Args_prc;
168};
169
170
171struct PUBLIC_API Collection_find_detail
172{
173protected:
174
175 using Impl = common::Proj_if;
176 using Args_prc = Args_processor<Collection_find_detail, Impl*>;
177
178 static void process_one(Impl *impl, const string &proj)
179 {
180 impl->add_proj(proj);
181 }
182
183
184 static void do_fields(Impl *impl, const Expression &proj)
185 {
186 impl->set_proj(proj.get<string>());
187 }
188
189 /*
190 Note: If e is an expression (of type Expression) then only
191 .fields(e) is valid - the multi-argument variant .fields(e,...)
192 should be disabled.
193 */
194
195 template <
196 typename T, typename... R,
197 typename std::enable_if<!std::is_same<T, Expression>::value>::type* = nullptr
198 >
199 static void do_fields(Impl *impl, T first, R... rest)
200 {
201 Args_prc::process_args(impl, first, rest...);
202 }
203
204 friend Args_prc;
205};
206
207
208struct PUBLIC_API Table_insert_detail
209{
210protected:
211
212 using Row_impl = internal::Row_detail::Impl;
213 using Impl = common::Table_insert_if<Row_impl>;
214
215 /*
216 Helper methods which pass column/row information to the
217 internal implementation object.
218 */
219
220 struct Add_column
221 {
222 static void process_one(Impl *impl, const string &col)
223 {
224 impl->add_column(col);
225 }
226 };
227
228 struct Add_value
229 {
230 using Impl = std::pair<Row, unsigned>;
231
232 static void process_one(Impl *impl, const mysqlx::Value &val)
233 {
234 impl->first.set((impl->second)++, val);
235 }
236 };
237
238 struct Add_row
239 {
240 static void process_one(Impl *impl, const Row &row)
241 {
242 impl->add_row(*row.m_impl);
243 }
244 };
245
246 template <typename... T>
247 static void add_columns(Impl *impl, T... args)
248 {
249 Args_processor<Add_column, Impl*>::process_args(impl, args...);
250 }
251
252 template <typename... T>
253 static void add_rows(Impl *impl, T... args)
254 {
255 Args_processor<Add_row, Impl*>::process_args(impl, args...);
256 }
257
258 template <typename... T>
259 static void add_values(Impl *impl, T... args)
260 {
261 Add_value::Impl row{ {}, 0 };
262 Args_processor<Add_value>::process_args(&row, args...);
263 Add_row::process_one(impl, row.first);
264 }
265
266 friend Args_processor<Add_column, Impl*>;
267 friend Args_processor<Add_row, Impl*>;
268 friend Args_processor<Add_value, Impl*>;
269
270};
271
272
273using Table_select_detail = Proj_detail;
274
275} // internal
276
277MYSQLX_ABI_END(2,0)
278} // mysqlx
279
280#endif
Value object can store value of scalar type, string, array or document.
Definition: document.h:230
A wrapper around std::wstring that can perform conversions from/to different character encodings used...
Definition: common.h:114