MySQL Connector/C++ 9.3.0
MySQL connector library for C and C++ applications
All Classes Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
op_if.h
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_COMMON_OP_IF_H
32#define MYSQLX_COMMON_OP_IF_H
33
34/*
35 This file defines a hierarchy of abstract interfaces for objects that
36 represent database operations. The base interface is the Executable_if for
37 any operation that can be executed. Other interfaces in the hierarchy allow
38 specifying more details of the operation.
39
40 Note: Header op_impl.h defines implementations of these interfaces used
41 in the connector.
42*/
43
44#include "api.h"
45#include "../common_constants.h"
46#include <string>
47
48
49namespace mysqlx {
50MYSQLX_ABI_BEGIN(2,0)
51namespace common {
52
53class Result_init;
54class Value;
55
56#define LOCK_MODE(X,N) X = N,
57enum Lock_mode
58{
59 LOCK_MODE_LIST(LOCK_MODE)
60};
61
62#define LOCK_CONTENTION(X,N) X = N,
63enum class Lock_contention
64{
65 LOCK_CONTENTION_LIST(LOCK_CONTENTION)
66};
67
68
69/*
70 Abstract interface for internal implementations of an executable object.
71
72 The execute() method returns a Result_init object which can be
73 used to construct a result instance.
74
75 Implementation of an executable object holds a description of the operation
76 that should be executed. Executable objects can be copied (for example
77 by copy assignment operation) and in this case a new copy of the current
78 description of the operation should be created by clone() method. After
79 cloning, the 2 executable implementations can be modified and executed
80 independently.
81
82 See various Op_xxx classes defined for example in operation.h to see examples
83 of executable object implementations. Note that these Op_xxx classes do
84 not directly inherit from Executable_if. Instead they use a whole hierarchy
85 of implementation classes based on Executable_if. But in the end, each
86 implementation of an executable object defines the execute() method that
87 executes given operation using all the information collected using other
88 methods of the implementation class.
89*/
90
91struct Executable_if
92{
93 /*
94 Execute the operation and return reference to object which implements
95 Result_init interface. Such object is then used to construct a result
96 instance.
97 */
98
99 virtual Result_init& execute() = 0;
100
101 virtual Executable_if *clone() const = 0;
102
103 virtual ~Executable_if() NOEXCEPT {}
104};
105
106
107/*
108 The XXX_if classes defined below form a hierarchy of interfaces, based
109 on Executable_if, for internal implementations of various crud operations.
110 The concrete implementations, like Op_collection_find defined in
111 operation.h, implements one of the top interfaces in this hierarchy but
112 the hierarchy allows casting the implementation down to the layer
113 implementing particular aspect of the operation. For example
114 Limit_if interface allows setting limit and offset for returned/affected
115 rows/documents, which is common for different CRUD operations.
116*/
117
118
119struct Bind_if : public Executable_if
120{
121 using string = std::string;
122
123 // Add value for named parameter
124
125 virtual void add_param(const string&, const Value&) = 0;
126
127 // Add value for positional parameter
128
129 virtual void add_param(Value) = 0;
130
131 virtual void clear_params() = 0;
132};
133
134
135struct Limit_if : public Bind_if
136{
137 virtual void set_offset(unsigned) = 0;
138 virtual void clear_offset() = 0;
139
140 virtual void set_limit(unsigned) = 0;
141 virtual void clear_limit() = 0;
142};
143
144
145struct Sort_if : public Limit_if
146{
147 using string = std::string;
148 enum direction_t { ASC, DESC };
149
150 virtual void add_sort(const string &expr, direction_t dir) = 0;
151 virtual void add_sort(const string&) = 0;
152 virtual void clear_sort() = 0;
153};
154
155
156struct Having_if : public Sort_if
157{
158 using string = std::string;
159
160 virtual void set_having(const string&) = 0;
161 virtual void clear_having() = 0;
162};
163
164
165struct Group_by_if : public Having_if
166{
167 using string = std::string;
168
169 virtual void add_group_by(const string&) = 0;
170 virtual void clear_group_by() = 0;
171};
172
173
174struct Proj_if : public Group_by_if
175{
176 using string = std::string;
177
178 /*
179 Add projection specification for a table query. It is an expression with
180 optional "AS <alias>" suffix.
181 */
182
183 virtual void add_proj(const string&) = 0;
184
185 /*
186 Set projection for a document query. It is a JSON-like string but document
187 field values are interpreted as expressions.
188 */
189
190 virtual void set_proj(const string&) = 0;
191
192 virtual void clear_proj() = 0;
193};
194
195
196template <class Base>
197struct Select_if : public Base
198{
199 using string = std::string;
200
201 // Set expression to select rows/documents.
202
203 virtual void set_where(const string&) = 0;
204
205 // Define lock mode for rows/documents returned by the query.
206
207 virtual void set_lock_mode(Lock_mode, Lock_contention) = 0;
208 virtual void clear_lock_mode() = 0;
209};
210
211
212// --------------------------------------------------------------------------
213
214
215struct Collection_find_if : public Select_if<Proj_if>
216{};
217
218
219/*
220 Interface to internal implementations of CRUD add operation.
221*/
222
223struct Collection_add_if : public Executable_if
224{
225 /*
226 Note: Current implementation only supports sending
227 documents in form of UTF8 JSON strings.
228 */
229
230 virtual void add_json(const std::string&) = 0;
231 virtual void clear_docs() = 0;
232};
233
234
235struct Collection_remove_if : public Select_if<Sort_if>
236{};
237
238
239/*
240 Interface to internal implementations of CRUD modify operation.
241
242 Methods `add_operation` are used to pass to the implementation object
243 the modifications requested by the user.
244*/
245
246struct Collection_modify_if : public Select_if<Sort_if>
247{
248 using string = std::string;
249
250 enum Operation
251 {
252 SET,
253 UNSET,
254 ARRAY_INSERT,
255 ARRAY_APPEND,
256 ARRAY_DELETE,
257 MERGE_PATCH
258 };
259
260 virtual void add_operation(Operation, const string&, const Value&) = 0;
261 virtual void add_operation(Operation, const string&) = 0;
262 virtual void clear_modifications() = 0;
263};
264
265
266// --------------------------------------------------------------------------
267
268
269/*
270 Interface to be implemented by internal implementations of
271 table insert operation.
272*/
273
274template <class Row_impl>
275struct Table_insert_if : public Executable_if
276{
277 using string = std::string;
278
279 /*
280 Pass to the implementation names of columns specified by
281 the user. Columns are passed one-by-one in the order in
282 which they were specified.
283 */
284
285 virtual void add_column(const string&) = 0;
286 virtual void clear_columns() = 0;
287
288 /*
289 Pass to the implementation a row that should be inserted
290 into the table. Several rows can be passed.
291
292 TODO: use move semantics instead
293 */
294
295 virtual void add_row(const Row_impl&) = 0;
296 virtual void clear_rows() = 0;
297};
298
299
300/*
301 Interface to be implemented by internal implementations
302 of table CRUD select operation.
303
304 Method `add_where` is used to report selection criteria
305 to the implementation.
306*/
307
308struct Table_select_if : public Select_if<Proj_if>
309{};
310
311
312/*
313 Interface to be implemented by internal implementations
314 of table CRUD remove operation.
315
316 Selection criteria which selects rows to be removed is
317 passed to the implementation using `set_where` method.
318
319 Note: setting where condition to empty string removes it.
320*/
321
322struct Table_remove_if : public Select_if<Sort_if>
323{};
324
325
326/*
327 Interface to be implemented by internal implementations of
328 table CRUD update operation. Such update operation sets values
329 of fields in a row. Name of the column that should be set and
330 expression defining new value are reported to the implementation
331 using method `add_set`.
332*/
333
334struct Table_update_if : public Table_remove_if
335{
336 using string = std::string;
337
338 virtual void add_set(const string&, const Value&) = 0;
339 virtual void clear_modifications() = 0;
340};
341
342} // internal
343MYSQLX_ABI_END(2,0)
344} // mysqlx
345
346#endif
internal::Expression expr(std::string &&e)
Function which indicates that a given string should be treated as expression.
Definition: document.h:639