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{
45 class Client;
46
51 {
52 public:
56 const std::optional<std::string>& getServer() const
57 {
58 return server;
59 }
60
64 ServiceManagerOptions& setServer(const std::string& value)
65 {
66 server = value;
67 return *this;
68 }
69
73 const std::string& getServiceManagerName() const
74 {
75 return serviceManagerName;
76 }
77
82 {
83 serviceManagerName = value;
84 return *this;
85 }
86
90 const std::optional<std::string>& getUserName() const
91 {
92 return userName;
93 }
94
98 ServiceManagerOptions& setUserName(const std::string& value)
99 {
100 userName = value;
101 return *this;
102 }
103
107 const std::optional<std::string>& getPassword() const
108 {
109 return password;
110 }
111
115 ServiceManagerOptions& setPassword(const std::string& value)
116 {
117 password = value;
118 return *this;
119 }
120
124 const std::optional<std::string>& getRole() const
125 {
126 return role;
127 }
128
132 ServiceManagerOptions& setRole(const std::string& value)
133 {
134 role = value;
135 return *this;
136 }
137
141 const std::vector<std::uint8_t>& getSpb() const
142 {
143 return spb;
144 }
145
149 ServiceManagerOptions& setSpb(const std::vector<std::uint8_t>& value)
150 {
151 spb = value;
152 return *this;
153 }
154
158 ServiceManagerOptions& setSpb(std::vector<std::uint8_t>&& value)
159 {
160 spb = std::move(value);
161 return *this;
162 }
163
164 private:
165 std::optional<std::string> server;
166 std::string serviceManagerName = "service_mgr";
167 std::optional<std::string> userName;
168 std::optional<std::string> password;
169 std::optional<std::string> role;
170 std::vector<std::uint8_t> spb;
171 };
172
177 {
178 public:
182 using VerboseOutput = std::function<void(std::string_view line)>;
183
187 explicit ServiceManager(Client& client, const ServiceManagerOptions& options = {});
188
194 : client{o.client},
195 handle{std::move(o.handle)}
196 {
197 }
198
206 {
207 if (this != &o)
208 {
209 if (isValid())
210 {
211 try
212 {
213 detachHandle();
214 }
215 catch (...)
216 {
217 // swallow
218 }
219 }
220
221 client = o.client;
222 handle = std::move(o.handle);
223 }
224
225 return *this;
226 }
227
228 ServiceManager(const ServiceManager&) = delete;
229 ServiceManager& operator=(const ServiceManager&) = delete;
230
235 {
236 if (isValid())
237 {
238 try
239 {
240 detachHandle();
241 }
242 catch (...)
243 {
244 // swallow
245 }
246 }
247 }
248
249 public:
253 bool isValid() noexcept
254 {
255 return handle != nullptr;
256 }
257
261 Client& getClient() noexcept
262 {
263 return *client;
264 }
265
270 {
271 return handle;
272 }
273
277 void disconnect();
278
279 protected:
280 static void addSpbInt(fb::IXpbBuilder* builder, impl::StatusWrapper* status, unsigned char tag,
281 std::uint64_t value, const char* what)
282 {
283 if (value > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()))
284 throw FbCppException(std::string(what) + " is too large");
285
286 if (value > static_cast<std::uint64_t>(std::numeric_limits<int>::max()))
287 builder->insertBigInt(status, tag, static_cast<std::int64_t>(value));
288 else
289 builder->insertInt(status, tag, static_cast<int>(value));
290 }
291
292 void startAction(const std::vector<std::uint8_t>& spb);
293 void waitForCompletion(const VerboseOutput& verboseOutput = {}, bool requestStdin = false);
294
295 private:
296 void detachHandle();
297
298 private:
299 Client* client;
300 FbRef<fb::IService> handle;
301 };
302} // namespace fbcpp
303
304
305#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:42