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-api.h"
30#include "SmartPtrs.h"
31#include <cstdint>
32#include <functional>
33#include <limits>
34#include <optional>
35#include <string>
36#include <string_view>
37#include <vector>
38
39
43namespace fbcpp
44{
48 enum class ReplicaMode
49 {
53 NONE,
54
59
64 };
65
66 class Client;
67
72 {
73 public:
77 const std::optional<std::string>& getServer() const
78 {
79 return server;
80 }
81
85 ServiceManagerOptions& setServer(const std::string& value)
86 {
87 server = value;
88 return *this;
89 }
90
94 const std::string& getServiceManagerName() const
95 {
96 return serviceManagerName;
97 }
98
103 {
104 serviceManagerName = value;
105 return *this;
106 }
107
111 const std::optional<std::string>& getUserName() const
112 {
113 return userName;
114 }
115
119 ServiceManagerOptions& setUserName(const std::string& value)
120 {
121 userName = value;
122 return *this;
123 }
124
128 const std::optional<std::string>& getPassword() const
129 {
130 return password;
131 }
132
136 ServiceManagerOptions& setPassword(const std::string& value)
137 {
138 password = value;
139 return *this;
140 }
141
145 const std::optional<std::string>& getRole() const
146 {
147 return role;
148 }
149
153 ServiceManagerOptions& setRole(const std::string& value)
154 {
155 role = value;
156 return *this;
157 }
158
162 const std::vector<std::uint8_t>& getSpb() const
163 {
164 return spb;
165 }
166
170 ServiceManagerOptions& setSpb(const std::vector<std::uint8_t>& value)
171 {
172 spb = value;
173 return *this;
174 }
175
179 ServiceManagerOptions& setSpb(std::vector<std::uint8_t>&& value)
180 {
181 spb = std::move(value);
182 return *this;
183 }
184
185 private:
186 std::optional<std::string> server;
187 std::string serviceManagerName = "service_mgr";
188 std::optional<std::string> userName;
189 std::optional<std::string> password;
190 std::optional<std::string> role;
191 std::vector<std::uint8_t> spb;
192 };
193
198 {
199 public:
203 using VerboseOutput = std::function<void(std::string_view line)>;
204
208 explicit ServiceManager(Client& client, const ServiceManagerOptions& options = {});
209
215 : client{o.client},
216 handle{std::move(o.handle)}
217 {
218 }
219
227 {
228 if (this != &o)
229 {
230 if (isValid())
231 {
232 try
233 {
234 detachHandle();
235 }
236 catch (...)
237 {
238 // swallow
239 }
240 }
241
242 client = o.client;
243 handle = std::move(o.handle);
244 }
245
246 return *this;
247 }
248
249 ServiceManager(const ServiceManager&) = delete;
250 ServiceManager& operator=(const ServiceManager&) = delete;
251
256 {
257 if (isValid())
258 {
259 try
260 {
261 detachHandle();
262 }
263 catch (...)
264 {
265 // swallow
266 }
267 }
268 }
269
270 public:
274 bool isValid() noexcept
275 {
276 return handle != nullptr;
277 }
278
282 Client& getClient() noexcept
283 {
284 return *client;
285 }
286
291 {
292 return handle;
293 }
294
298 void disconnect();
299
300 protected:
301 static void addSpbInt(fb::IXpbBuilder* builder, impl::StatusWrapper* status, unsigned char tag,
302 std::uint64_t value, const char* what)
303 {
304 if (value > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()))
305 throw FbCppException(std::string(what) + " is too large");
306
307 if (value > static_cast<std::uint64_t>(std::numeric_limits<int>::max()))
308 builder->insertBigInt(status, tag, static_cast<std::int64_t>(value));
309 else
310 builder->insertInt(status, tag, static_cast<int>(value));
311 }
312
313 void startAction(const std::vector<std::uint8_t>& spb);
314 void waitForCompletion(const VerboseOutput& verboseOutput = {}, bool requestStdin = false);
315
316 private:
317 void detachHandle();
318
319 private:
320 Client* client;
321 FbRef<fb::IService> handle;
322 };
323} // namespace fbcpp
324
325
326#endif // FBCPP_SERVICE_MANAGER_H
Represents a Firebird client library instance.
Definition Client.h:53
Base exception class for all fb-cpp exceptions.
Definition Exception.h:230
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.
void disconnect()
Detaches from the service manager.
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:45
ReplicaMode
Replica mode.
@ READ_ONLY
Read-only replica.
@ READ_WRITE
Read-write replica.
@ NONE
Blobs are not allowed in the batch.