fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
ServiceManager.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef FBCPP_SERVICE_MANAGER_H
26#define FBCPP_SERVICE_MANAGER_H
27
28#include "Exception.h"
29#include "fb-cpp-export.h"
30#include "fb-api.h"
31#include "SmartPtrs.h"
32#include <cstdint>
33#include <functional>
34#include <limits>
35#include <optional>
36#include <string>
37#include <string_view>
38#include <vector>
39
40
44namespace fbcpp
45{
49 enum class ReplicaMode
50 {
54 NONE,
55
60
65 };
66
67 class Client;
68
73 {
74 public:
78 const std::optional<std::string>& getServer() const
79 {
80 return server;
81 }
82
86 ServiceManagerOptions& setServer(const std::string& value)
87 {
88 server = value;
89 return *this;
90 }
91
95 const std::string& getServiceManagerName() const
96 {
97 return serviceManagerName;
98 }
99
104 {
105 serviceManagerName = value;
106 return *this;
107 }
108
112 const std::optional<std::string>& getUserName() const
113 {
114 return userName;
115 }
116
120 ServiceManagerOptions& setUserName(const std::string& value)
121 {
122 userName = value;
123 return *this;
124 }
125
129 const std::optional<std::string>& getPassword() const
130 {
131 return password;
132 }
133
137 ServiceManagerOptions& setPassword(const std::string& value)
138 {
139 password = value;
140 return *this;
141 }
142
146 const std::optional<std::string>& getRole() const
147 {
148 return role;
149 }
150
154 ServiceManagerOptions& setRole(const std::string& value)
155 {
156 role = value;
157 return *this;
158 }
159
163 const std::vector<std::uint8_t>& getSpb() const
164 {
165 return spb;
166 }
167
171 ServiceManagerOptions& setSpb(const std::vector<std::uint8_t>& value)
172 {
173 spb = value;
174 return *this;
175 }
176
180 ServiceManagerOptions& setSpb(std::vector<std::uint8_t>&& value)
181 {
182 spb = std::move(value);
183 return *this;
184 }
185
186 private:
187 std::optional<std::string> server;
188 std::string serviceManagerName = "service_mgr";
189 std::optional<std::string> userName;
190 std::optional<std::string> password;
191 std::optional<std::string> role;
192 std::vector<std::uint8_t> spb;
193 };
194
198 class FB_CPP_EXPORT ServiceManager
199 {
200 public:
204 using VerboseOutput = std::function<void(std::string_view line)>;
205
209 explicit ServiceManager(Client& client, const ServiceManagerOptions& options = {});
210
216 : client{o.client},
217 handle{std::move(o.handle)}
218 {
219 }
220
228 {
229 if (this != &o)
230 {
231 if (isValid())
232 {
233 try
234 {
235 detachHandle();
236 }
237 catch (...)
238 {
239 // swallow
240 }
241 }
242
243 client = o.client;
244 handle = std::move(o.handle);
245 }
246
247 return *this;
248 }
249
250 ServiceManager(const ServiceManager&) = delete;
251 ServiceManager& operator=(const ServiceManager&) = delete;
252
257 {
258 if (isValid())
259 {
260 try
261 {
262 detachHandle();
263 }
264 catch (...)
265 {
266 // swallow
267 }
268 }
269 }
270
271 public:
275 bool isValid() noexcept
276 {
277 return handle != nullptr;
278 }
279
283 Client& getClient() noexcept
284 {
285 return *client;
286 }
287
292 {
293 return handle;
294 }
295
299 void disconnect();
300
301 protected:
302 static void addSpbInt(fb::IXpbBuilder* builder, impl::StatusWrapper* status, unsigned char tag,
303 std::uint64_t value, const char* what)
304 {
305 if (value > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()))
306 throw FbCppException(std::string(what) + " is too large");
307
308 if (value > static_cast<std::uint64_t>(std::numeric_limits<int>::max()))
309 builder->insertBigInt(status, tag, static_cast<std::int64_t>(value));
310 else
311 builder->insertInt(status, tag, static_cast<int>(value));
312 }
313
314 void startAction(const std::vector<std::uint8_t>& spb);
315 void waitForCompletion(const VerboseOutput& verboseOutput = {}, bool requestStdin = false);
316
317 private:
318 void detachHandle();
319
320 private:
321 Client* client;
322 FbRef<fb::IService> handle;
323 };
324} // namespace fbcpp
325
326
327#endif // FBCPP_SERVICE_MANAGER_H
Represents a Firebird client library instance.
Definition Client.h:54
Base exception class for all fb-cpp exceptions.
Definition Exception.h:231
Reference-counted smart pointer for Firebird objects using addRef/release semantics.
Definition SmartPtrs.h:70
Represents options used when creating a ServiceManager object.
ServiceManagerOptions & setServiceManagerName(const std::string &value)
Sets the service manager name.
ServiceManagerOptions & setUserName(const std::string &value)
Sets the user name used to attach to the service manager.
const std::vector< std::uint8_t > & getSpb() const
Returns the raw service attach SPB.
ServiceManagerOptions & setSpb(const std::vector< std::uint8_t > &value)
Sets the raw service attach SPB.
const std::optional< std::string > & getServer() const
Returns the server used to attach to the service manager.
const std::string & getServiceManagerName() const
Returns the service manager name.
ServiceManagerOptions & setSpb(std::vector< std::uint8_t > &&value)
Sets the raw service attach SPB.
ServiceManagerOptions & setRole(const std::string &value)
Sets the role used to attach to the service manager.
const std::optional< std::string > & getPassword() const
Returns the password used to attach to the service manager.
const std::optional< std::string > & getUserName() const
Returns the user name used to attach to the service manager.
const std::optional< std::string > & getRole() const
Returns the role used to attach to the service manager.
ServiceManagerOptions & setPassword(const std::string &value)
Sets the password used to attach to the service manager.
ServiceManagerOptions & setServer(const std::string &value)
Sets the server used to attach to the service manager.
Represents a connection to the Firebird service manager.
Client & getClient() noexcept
Returns the Client object reference used to create this ServiceManager object.
ServiceManager & operator=(ServiceManager &&o) noexcept
Transfers ownership of another ServiceManager into this one.
ServiceManager(ServiceManager &&o) noexcept
Move constructor.
bool isValid() noexcept
Returns whether the ServiceManager object is valid.
std::function< void(std::string_view line)> VerboseOutput
Function invoked when a verbose service output line is available.
~ServiceManager() noexcept
Detaches from the service manager.
FbRef< fb::IService > getHandle() noexcept
Returns the internal Firebird IService handle.
fb-cpp namespace.
Definition Attachment.h:46
ReplicaMode
Replica mode.
@ READ_ONLY
Read-only replica.
@ READ_WRITE
Read-write replica.
@ NONE
Blobs are not allowed in the batch.