fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
Attachment.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 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_ATTACHMENT_H
26#define FBCPP_ATTACHMENT_H
27
28#include "fb-api.h"
29#include "SmartPtrs.h"
30#include <cstdint>
31#include <memory>
32#include <optional>
33#include <string>
34#include <vector>
35#include <cstddef>
36
37
41namespace fbcpp
42{
43 class Client;
44
49 {
50 public:
54 const std::optional<std::string>& getConnectionCharSet() const
55 {
56 return connectionCharSet;
57 }
58
62 AttachmentOptions& setConnectionCharSet(const std::string& value)
63 {
64 connectionCharSet = value;
65 return *this;
66 }
67
71 const std::optional<std::string>& getUserName() const
72 {
73 return userName;
74 }
75
79 AttachmentOptions& setUserName(const std::string& value)
80 {
81 userName = value;
82 return *this;
83 }
84
88 const std::optional<std::string>& getPassword() const
89 {
90 return password;
91 }
92
96 AttachmentOptions& setPassword(const std::string& value)
97 {
98 password = value;
99 return *this;
100 }
101
105 const std::optional<std::string>& getRole() const
106 {
107 return role;
108 }
109
113 AttachmentOptions& setRole(const std::string& value)
114 {
115 role = value;
116 return *this;
117 }
118
122 const std::vector<std::uint8_t>& getDpb() const
123 {
124 return dpb;
125 }
126
130 AttachmentOptions& setDpb(const std::vector<std::uint8_t>& value)
131 {
132 dpb = value;
133 return *this;
134 }
135
139 AttachmentOptions& setDpb(std::vector<std::uint8_t>&& value)
140 {
141 dpb = std::move(value);
142 return *this;
143 }
144
148 bool getCreateDatabase() const
149 {
150 return createDatabase;
151 }
152
157 {
158 createDatabase = value;
159 return *this;
160 }
161
162 private:
163 std::optional<std::string> connectionCharSet;
164 std::optional<std::string> userName;
165 std::optional<std::string> password;
166 std::optional<std::string> role;
167 std::vector<std::uint8_t> dpb;
168 bool createDatabase = false;
169 };
170
176 class Attachment final
177 {
178 public:
183 explicit Attachment(Client& client, const std::string& uri, const AttachmentOptions& options = {});
184
189 Attachment(Attachment&& o) noexcept
190 : client{o.client},
191 handle{std::move(o.handle)}
192 {
193 }
194
202 {
203 if (this != &o)
204 {
205 client = o.client;
206 handle = std::move(o.handle);
207 }
208
209 return *this;
210 }
211
212 Attachment(const Attachment&) = delete;
213 Attachment& operator=(const Attachment&) = delete;
214
218 ~Attachment() noexcept
219 {
220 if (isValid())
221 {
222 try
223 {
224 disconnectOrDrop(false);
225 }
226 catch (...)
227 {
228 // swallow
229 }
230 }
231 }
232
233 public:
237 bool isValid() noexcept
238 {
239 return handle != nullptr;
240 }
241
245 Client& getClient() noexcept
246 {
247 return *client;
248 }
249
254 {
255 return handle;
256 }
257
261 void disconnect();
262
266 void dropDatabase();
267
268 private:
269 void disconnectOrDrop(bool drop);
270
271 private:
272 Client* client;
274 };
275} // namespace fbcpp
276
277
278#endif // FBCPP_ATTACHMENT_H
Represents options used when creating an Attachment object.
Definition Attachment.h:49
AttachmentOptions & setConnectionCharSet(const std::string &value)
Sets the character set which will be used for the connection.
Definition Attachment.h:62
const std::optional< std::string > & getPassword() const
Returns the password which will be used to connect to the database.
Definition Attachment.h:88
AttachmentOptions & setPassword(const std::string &value)
Sets the password which will be used to connect to the database.
Definition Attachment.h:96
const std::optional< std::string > & getRole() const
Returns the role which will be used to connect to the database.
Definition Attachment.h:105
AttachmentOptions & setDpb(const std::vector< std::uint8_t > &value)
Sets the DPB (Database Parameter Block) which will be used to connect to the database.
Definition Attachment.h:130
AttachmentOptions & setRole(const std::string &value)
Sets the role which will be used to connect to the database.
Definition Attachment.h:113
AttachmentOptions & setDpb(std::vector< std::uint8_t > &&value)
Sets the DPB (Database Parameter Block) which will be used to connect to the database.
Definition Attachment.h:139
const std::vector< std::uint8_t > & getDpb() const
Returns the DPB (Database Parameter Block) which will be used to connect to the database.
Definition Attachment.h:122
AttachmentOptions & setCreateDatabase(bool value)
Sets whether the database should be created instead of connected to.
Definition Attachment.h:156
const std::optional< std::string > & getUserName() const
Returns the user name which will be used to connect to the database.
Definition Attachment.h:71
bool getCreateDatabase() const
Returns whether the database should be created instead of connected to.
Definition Attachment.h:148
const std::optional< std::string > & getConnectionCharSet() const
Returns the character set which will be used for the connection.
Definition Attachment.h:54
AttachmentOptions & setUserName(const std::string &value)
Sets the user name which will be used to connect to the database.
Definition Attachment.h:79
Represents a connection to a Firebird database.
Definition Attachment.h:177
Client & getClient() noexcept
Returns the Client object reference used to create this Attachment object.
Definition Attachment.h:245
void disconnect()
Disconnects from the database.
FbRef< fb::IAttachment > getHandle() noexcept
Returns the internal Firebird IAttachment handle.
Definition Attachment.h:253
~Attachment() noexcept
Disconnects from the database.
Definition Attachment.h:218
Attachment(Attachment &&o) noexcept
Move constructor.
Definition Attachment.h:189
bool isValid() noexcept
Returns whether the Attachment object is valid.
Definition Attachment.h:237
void dropDatabase()
Drops the database.
Attachment & operator=(Attachment &&o) noexcept
Transfers ownership of another Attachment into this one.
Definition Attachment.h:201
Represents a Firebird client library instance.
Definition Client.h:53
Reference-counted smart pointer for Firebird objects using addRef/release semantics.
Definition SmartPtrs.h:70
fb-cpp namespace.
Definition Attachment.h:42