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::optional<std::uint32_t>& getSqlDialect() const
123 {
124 return sqlDialect;
125 }
126
130 AttachmentOptions& setSqlDialect(std::uint32_t value)
131 {
132 sqlDialect = value;
133 return *this;
134 }
135
139 const std::vector<std::uint8_t>& getDpb() const
140 {
141 return dpb;
142 }
143
147 AttachmentOptions& setDpb(const std::vector<std::uint8_t>& value)
148 {
149 dpb = value;
150 return *this;
151 }
152
156 AttachmentOptions& setDpb(std::vector<std::uint8_t>&& value)
157 {
158 dpb = std::move(value);
159 return *this;
160 }
161
165 bool getCreateDatabase() const
166 {
167 return createDatabase;
168 }
169
174 {
175 createDatabase = value;
176 return *this;
177 }
178
182 const std::optional<bool>& getForcedWrites() const
183 {
184 return forcedWrites;
185 }
186
191 {
192 forcedWrites = value;
193 return *this;
194 }
195
196 private:
197 std::optional<std::string> connectionCharSet;
198 std::optional<std::string> userName;
199 std::optional<std::string> password;
200 std::optional<std::string> role;
201 std::optional<std::uint32_t> sqlDialect;
202 std::optional<bool> forcedWrites;
203 std::vector<std::uint8_t> dpb;
204 bool createDatabase = false;
205 };
206
212 class Attachment final
213 {
214 public:
219 explicit Attachment(Client& client, const std::string& uri, const AttachmentOptions& options = {});
220
225 Attachment(Attachment&& o) noexcept
226 : client{o.client},
227 handle{std::move(o.handle)}
228 {
229 }
230
238 {
239 if (this != &o)
240 {
241 client = o.client;
242 handle = std::move(o.handle);
243 }
244
245 return *this;
246 }
247
248 Attachment(const Attachment&) = delete;
249 Attachment& operator=(const Attachment&) = delete;
250
254 ~Attachment() noexcept
255 {
256 if (isValid())
257 {
258 try
259 {
260 disconnectOrDrop(false);
261 }
262 catch (...)
263 {
264 // swallow
265 }
266 }
267 }
268
269 public:
273 bool isValid() noexcept
274 {
275 return handle != nullptr;
276 }
277
281 Client& getClient() noexcept
282 {
283 return *client;
284 }
285
290 {
291 return handle;
292 }
293
297 void disconnect();
298
302 void dropDatabase();
303
304 private:
305 void disconnectOrDrop(bool drop);
306
307 private:
308 Client* client;
310 };
311} // namespace fbcpp
312
313
314#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 & setSqlDialect(std::uint32_t value)
Sets the SQL dialect which will be used to connect to the database.
Definition Attachment.h:130
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:147
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:156
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:139
AttachmentOptions & setCreateDatabase(bool value)
Sets whether the database should be created instead of connected to.
Definition Attachment.h:173
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:165
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
const std::optional< bool > & getForcedWrites() const
Returns whether forced writes should be enabled when creating the database.
Definition Attachment.h:182
AttachmentOptions & setForcedWrites(bool value)
Sets whether forced writes should be enabled when creating the database.
Definition Attachment.h:190
const std::optional< std::uint32_t > & getSqlDialect() const
Returns the SQL dialect which will be used to connect to the database.
Definition Attachment.h:122
Represents a connection to a Firebird database.
Definition Attachment.h:213
Client & getClient() noexcept
Returns the Client object reference used to create this Attachment object.
Definition Attachment.h:281
void disconnect()
Disconnects from the database.
FbRef< fb::IAttachment > getHandle() noexcept
Returns the internal Firebird IAttachment handle.
Definition Attachment.h:289
~Attachment() noexcept
Disconnects from the database.
Definition Attachment.h:254
Attachment(Attachment &&o) noexcept
Move constructor.
Definition Attachment.h:225
bool isValid() noexcept
Returns whether the Attachment object is valid.
Definition Attachment.h:273
void dropDatabase()
Drops the database.
Attachment & operator=(Attachment &&o) noexcept
Transfers ownership of another Attachment into this one.
Definition Attachment.h:237
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