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-cpp-export.h"
29#include "fb-api.h"
30#include "Blob.h"
31#include "RowSet.h"
32#include "SmartPtrs.h"
33#include "StatementOptions.h"
34#include <cstddef>
35#include <cstdint>
36#include <memory>
37#include <optional>
38#include <string>
39#include <string_view>
40#include <vector>
41
42
46namespace fbcpp
47{
48 class Client;
49 class Statement;
50 class Transaction;
51
56 {
57 public:
61 const std::optional<std::string>& getConnectionCharSet() const
62 {
63 return connectionCharSet;
64 }
65
69 AttachmentOptions& setConnectionCharSet(const std::string& value)
70 {
71 connectionCharSet = value;
72 return *this;
73 }
74
78 const std::optional<std::string>& getUserName() const
79 {
80 return userName;
81 }
82
86 AttachmentOptions& setUserName(const std::string& value)
87 {
88 userName = value;
89 return *this;
90 }
91
95 const std::optional<std::string>& getPassword() const
96 {
97 return password;
98 }
99
103 AttachmentOptions& setPassword(const std::string& value)
104 {
105 password = value;
106 return *this;
107 }
108
112 const std::optional<std::string>& getRole() const
113 {
114 return role;
115 }
116
120 AttachmentOptions& setRole(const std::string& value)
121 {
122 role = value;
123 return *this;
124 }
125
129 const std::optional<std::uint32_t>& getSqlDialect() const
130 {
131 return sqlDialect;
132 }
133
137 AttachmentOptions& setSqlDialect(std::uint32_t value)
138 {
139 sqlDialect = value;
140 return *this;
141 }
142
146 const std::vector<std::uint8_t>& getDpb() const
147 {
148 return dpb;
149 }
150
154 AttachmentOptions& setDpb(const std::vector<std::uint8_t>& value)
155 {
156 dpb = value;
157 return *this;
158 }
159
163 AttachmentOptions& setDpb(std::vector<std::uint8_t>&& value)
164 {
165 dpb = std::move(value);
166 return *this;
167 }
168
172 bool getCreateDatabase() const
173 {
174 return createDatabase;
175 }
176
181 {
182 createDatabase = value;
183 return *this;
184 }
185
189 const std::optional<bool>& getForcedWrites() const
190 {
191 return forcedWrites;
192 }
193
198 {
199 forcedWrites = value;
200 return *this;
201 }
202
203 private:
204 std::optional<std::string> connectionCharSet;
205 std::optional<std::string> userName;
206 std::optional<std::string> password;
207 std::optional<std::string> role;
208 std::optional<std::uint32_t> sqlDialect;
209 std::optional<bool> forcedWrites;
210 std::vector<std::uint8_t> dpb;
211 bool createDatabase = false;
212 };
213
219 class FB_CPP_EXPORT Attachment final
220 {
221 public:
226 explicit Attachment(Client& client, const std::string& uri, const AttachmentOptions& options = {});
227
232 Attachment(Attachment&& o) noexcept
233 : client{o.client},
234 handle{std::move(o.handle)}
235 {
236 }
237
245 {
246 if (this != &o)
247 {
248 client = o.client;
249 handle = std::move(o.handle);
250 }
251
252 return *this;
253 }
254
255 Attachment(const Attachment&) = delete;
256 Attachment& operator=(const Attachment&) = delete;
257
261 ~Attachment() noexcept
262 {
263 if (isValid())
264 {
265 try
266 {
267 disconnectOrDrop(false);
268 }
269 catch (...)
270 {
271 // swallow
272 }
273 }
274 }
275
276 public:
280 bool isValid() noexcept
281 {
282 return handle != nullptr;
283 }
284
288 Client& getClient() noexcept
289 {
290 return *client;
291 }
292
297 {
298 return handle;
299 }
300
304 std::optional<std::string> blobIdToString(
305 Transaction& transaction, std::optional<BlobId> blobId, const BlobOptions& options = {});
306
310 std::optional<std::vector<std::byte>> blobIdToBytes(
311 Transaction& transaction, std::optional<BlobId> blobId, const BlobOptions& options = {});
312
316 std::optional<BlobId> blobIdFromString(
317 Transaction& transaction, std::optional<std::string_view> value, const BlobOptions& options = {});
318
322 std::optional<BlobId> blobIdFromBytes(
323 Transaction& transaction, std::optional<std::span<const std::byte>> value, const BlobOptions& options = {});
324
328 void disconnect();
329
333 void dropDatabase();
334
338 void ping();
339
343 void resetSession();
344
348 bool execute(Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
349
353 template <typename Params>
354 bool execute(Transaction& transaction, std::string_view sql, const Params& params);
355
360 template <typename Params>
361 bool execute(
362 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
363
367 RowSet queryRowSet(
368 Transaction& transaction, std::string_view sql, unsigned maxRows, const StatementOptions& options = {});
369
374 template <typename Params>
375 RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params);
376
381 template <typename Params>
382 RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
383 const StatementOptions& options, const Params& params);
384
388 template <typename T>
389 std::optional<T> queryScalar(
390 Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
391
396 template <typename T, typename Params>
397 std::optional<T> queryScalar(Transaction& transaction, std::string_view sql, const Params& params);
398
403 template <typename T, typename Params>
404 std::optional<T> queryScalar(
405 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
406
410 template <typename T>
411 std::optional<T> queryFirstRowAs(
412 Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
413
418 template <typename T, typename Params>
419 std::optional<T> queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params);
420
425 template <typename T, typename Params>
426 std::optional<T> queryFirstRowAs(
427 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
428
429 private:
430 void disconnectOrDrop(bool drop);
431 RowSet queryPreparedRowSet(Statement& statement, Transaction& transaction, unsigned maxRows);
432
433 private:
434 Client* client;
435 FbRef<fb::IAttachment> handle;
436 };
437
438 template <typename T>
439 std::optional<T> Attachment::queryScalar(
440 Transaction& transaction, std::string_view sql, const StatementOptions& options)
441 {
442 auto rowSet = queryRowSet(transaction, sql, 1u, options);
443
444 if (rowSet.getCount() == 0u)
445 return std::nullopt;
446
447 return rowSet.getRow(0).get<std::optional<T>>(0);
448 }
449
450 template <typename T>
452 Transaction& transaction, std::string_view sql, const StatementOptions& options)
453 {
454 auto rowSet = queryRowSet(transaction, sql, 1u, options);
455
456 if (rowSet.getCount() == 0u)
457 return std::nullopt;
458
459 return rowSet.getRow(0).get<T>();
460 }
461} // namespace fbcpp
462
463#include "Statement.h"
464
465namespace fbcpp
466{
467 template <typename Params>
468 bool Attachment::execute(Transaction& transaction, std::string_view sql, const Params& params)
469 {
470 return execute(transaction, sql, StatementOptions{}, params);
471 }
472
473 template <typename Params>
475 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
476 {
477 Statement statement{*this, transaction, sql, options};
478 statement.set(params);
479 return statement.execute(transaction);
480 }
481
482 template <typename Params>
484 Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params)
485 {
486 return queryRowSet(transaction, sql, maxRows, StatementOptions{}, params);
487 }
488
489 template <typename Params>
490 RowSet Attachment::queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
491 const StatementOptions& options, const Params& params)
492 {
493 Statement statement{*this, transaction, sql, options};
494 statement.set(params);
495 return queryPreparedRowSet(statement, transaction, maxRows);
496 }
497
498 template <typename T, typename Params>
499 std::optional<T> Attachment::queryScalar(Transaction& transaction, std::string_view sql, const Params& params)
500 {
501 auto rowSet = queryRowSet(transaction, sql, 1u, params);
502
503 if (rowSet.getCount() == 0u)
504 return std::nullopt;
505
506 return rowSet.getRow(0).template get<std::optional<T>>(0);
507 }
508
509 template <typename T, typename Params>
510 std::optional<T> Attachment::queryScalar(
511 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
512 {
513 auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
514
515 if (rowSet.getCount() == 0u)
516 return std::nullopt;
517
518 return rowSet.getRow(0).template get<std::optional<T>>(0);
519 }
520
521 template <typename T, typename Params>
522 std::optional<T> Attachment::queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params)
523 {
524 auto rowSet = queryRowSet(transaction, sql, 1u, params);
525
526 if (rowSet.getCount() == 0u)
527 return std::nullopt;
528
529 return rowSet.getRow(0).template get<T>();
530 }
531
532 template <typename T, typename Params>
534 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
535 {
536 auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
537
538 if (rowSet.getCount() == 0u)
539 return std::nullopt;
540
541 return rowSet.getRow(0).template get<T>();
542 }
543} // namespace fbcpp
544
545#endif // FBCPP_ATTACHMENT_H
Represents options used when creating an Attachment object.
Definition Attachment.h:56
AttachmentOptions & setConnectionCharSet(const std::string &value)
Sets the character set which will be used for the connection.
Definition Attachment.h:69
const std::optional< std::string > & getPassword() const
Returns the password which will be used to connect to the database.
Definition Attachment.h:95
AttachmentOptions & setSqlDialect(std::uint32_t value)
Sets the SQL dialect which will be used to connect to the database.
Definition Attachment.h:137
AttachmentOptions & setPassword(const std::string &value)
Sets the password which will be used to connect to the database.
Definition Attachment.h:103
const std::optional< std::string > & getRole() const
Returns the role which will be used to connect to the database.
Definition Attachment.h:112
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:154
AttachmentOptions & setRole(const std::string &value)
Sets the role which will be used to connect to the database.
Definition Attachment.h:120
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:163
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:146
AttachmentOptions & setCreateDatabase(bool value)
Sets whether the database should be created instead of connected to.
Definition Attachment.h:180
const std::optional< std::string > & getUserName() const
Returns the user name which will be used to connect to the database.
Definition Attachment.h:78
bool getCreateDatabase() const
Returns whether the database should be created instead of connected to.
Definition Attachment.h:172
const std::optional< std::string > & getConnectionCharSet() const
Returns the character set which will be used for the connection.
Definition Attachment.h:61
AttachmentOptions & setUserName(const std::string &value)
Sets the user name which will be used to connect to the database.
Definition Attachment.h:86
const std::optional< bool > & getForcedWrites() const
Returns whether forced writes should be enabled when creating the database.
Definition Attachment.h:189
AttachmentOptions & setForcedWrites(bool value)
Sets whether forced writes should be enabled when creating the database.
Definition Attachment.h:197
const std::optional< std::uint32_t > & getSqlDialect() const
Returns the SQL dialect which will be used to connect to the database.
Definition Attachment.h:129
Represents a connection to a Firebird database.
Definition Attachment.h:220
bool execute(Transaction &transaction, std::string_view sql, const StatementOptions &options={})
Prepares and executes an SQL statement using the supplied transaction.
Client & getClient() noexcept
Returns the Client object reference used to create this Attachment object.
Definition Attachment.h:288
FbRef< fb::IAttachment > getHandle() noexcept
Returns the internal Firebird IAttachment handle.
Definition Attachment.h:296
~Attachment() noexcept
Disconnects from the database.
Definition Attachment.h:261
std::optional< T > queryFirstRowAs(Transaction &transaction, std::string_view sql, const StatementOptions &options={})
Prepares and executes a query using the supplied transaction and returns the first row mapped as T.
Definition Attachment.h:451
Attachment(Attachment &&o) noexcept
Move constructor.
Definition Attachment.h:232
bool isValid() noexcept
Returns whether the Attachment object is valid.
Definition Attachment.h:280
std::optional< T > queryScalar(Transaction &transaction, std::string_view sql, const StatementOptions &options={})
Prepares and executes a query using the supplied transaction and returns the first column of the firs...
Definition Attachment.h:439
RowSet queryRowSet(Transaction &transaction, std::string_view sql, unsigned maxRows, const StatementOptions &options={})
Prepares and executes a query using the supplied transaction and returns up to maxRows rows.
Attachment & operator=(Attachment &&o) noexcept
Transfers ownership of another Attachment into this one.
Definition Attachment.h:244
Additional options used when creating or opening blobs.
Definition Blob.h:104
Represents a Firebird client library instance.
Definition Client.h:54
Reference-counted smart pointer for Firebird objects using addRef/release semantics.
Definition SmartPtrs.h:70
A disconnected buffer of rows fetched from a Statement's result set.
Definition RowSet.h:57
Represents options used when preparing a Statement.
Prepares, executes, and fetches SQL statements against a Firebird attachment.
Definition Statement.h:146
Represents a transaction in one or more Firebird databases.
fb-cpp namespace.
Definition Array.h:43