fb-cpp 0.0.1
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
195 Attachment& operator=(Attachment&&) = delete;
196
197 Attachment(const Attachment&) = delete;
198 Attachment& operator=(const Attachment&) = delete;
199
203 ~Attachment() noexcept
204 {
205 if (isValid())
206 {
207 try
208 {
209 disconnectOrDrop(false);
210 }
211 catch (...)
212 {
213 // swallow
214 }
215 }
216 }
217
218 public:
222 bool isValid() noexcept
223 {
224 return handle != nullptr;
225 }
226
230 Client& getClient() noexcept
231 {
232 return client;
233 }
234
239 {
240 return handle;
241 }
242
246 void disconnect();
247
251 void dropDatabase();
252
253 private:
254 void disconnectOrDrop(bool drop);
255
256 private:
257 Client& client;
259 };
260} // namespace fbcpp
261
262
263#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
Attachment(Client &client, const std::string &uri, const AttachmentOptions &options={})
Constructs an Attachment object that connects to (or creates) the database specified by the URI using...
Client & getClient() noexcept
Returns the Client object reference used to create this Attachment object.
Definition Attachment.h:230
void disconnect()
Disconnects from the database.
FbRef< fb::IAttachment > getHandle() noexcept
Returns the internal Firebird IAttachment handle.
Definition Attachment.h:238
~Attachment() noexcept
Disconnects from the database.
Definition Attachment.h:203
Attachment(Attachment &&o) noexcept
Move constructor.
Definition Attachment.h:189
bool isValid() noexcept
Returns whether the Attachment object is valid.
Definition Attachment.h:222
void dropDatabase()
Drops the database.
Represents a Firebird client library instance.
Definition Client.h:53
fb-cpp namespace.
Definition Attachment.h:42