MySQL Connector/C++ 9.1.0
MySQL connector library for C and C++ applications
executable.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_EXECUTABLE_H
32#define MYSQLX_EXECUTABLE_H
33
40#include "common.h"
41#include "result.h"
42#include "../common/op_if.h"
43
44
45namespace mysqlx {
46MYSQLX_ABI_BEGIN(2,0)
47
48using std::ostream;
49
50
66template <class Res, class Op>
68{
69private:
70 using Impl = common::Executable_if;
71
72 std::unique_ptr<Impl> m_impl;
73protected:
74
75
76 Executable() = default;
77
78 void reset(Impl *impl)
79 {
80 m_impl.reset(impl);
81 }
82
83 /*
84 Copy semantics implementation: the current state of the other executable
85 object (of its implementation, to be precise) is copied to this new
86 instance. After that the two executables are independent objects describing
87 the same operation.
88 */
89
90 void reset(const Executable &other)
91 {
92 if (m_impl.get() != other.m_impl.get()) m_impl.reset(other.m_impl->clone());
93 }
94
95 void reset(const Executable &&other)
96 {
97 m_impl.reset(other.m_impl->clone());
98 }
99
100 void check_if_valid() const
101 {
102 if (!m_impl)
103 throw Error("Attempt to use invalid operation");
104 }
105
106 Impl* get_impl()
107 {
108 check_if_valid();
109 return m_impl.get();
110 }
111
112public:
113
114 Executable(const Executable &other)
115 {
116 operator=(other);
117 }
118
119 Executable(Executable &&other)
120 {
121 operator=(std::move(other));
122 }
123
124 virtual ~Executable() {}
125
126
127 Executable& operator=(const Executable &other)
128 {
129 try {
130 reset(other);
131 return *this;
132 }
133 CATCH_AND_WRAP
134 }
135
136 Executable& operator=(Executable &&other)
137 {
138 try {
139 reset(std::move(other));
140 return *this;
141 }
142 CATCH_AND_WRAP
143 }
144
145
147
148 virtual Res execute()
149 {
150 try {
151 check_if_valid();
152 /*
153 Note: The implementation object's execute() methods returns a reference
154 to a common::Result_init object which provides information about
155 the session and pending server reply. The returned Res instance should
156 be constructible from such Result_init reference.
157 */
158 return m_impl->execute();
159 }
160 CATCH_AND_WRAP
161 }
162
163 struct Access;
164 friend Access;
165};
166
167
168MYSQLX_ABI_END(2,0)
169} // mysqlx
170
171#endif
Base class for connector errors.
Definition: common.h:84
Represents an operation that can be executed.
Definition: executable.h:68
virtual Res execute()
Execute given operation and return its result.
Definition: executable.h:148
Classes used to access query and command execution results.