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 "RowSet.h"
30#include "SmartPtrs.h"
31#include "StatementOptions.h"
32#include <cstddef>
33#include <cstdint>
34#include <memory>
35#include <optional>
36#include <string>
37#include <string_view>
38#include <vector>
39
40
44namespace fbcpp
45{
46 class Client;
47 class Statement;
48 class Transaction;
49
54 {
55 public:
59 const std::optional<std::string>& getConnectionCharSet() const
60 {
61 return connectionCharSet;
62 }
63
67 AttachmentOptions& setConnectionCharSet(const std::string& value)
68 {
69 connectionCharSet = value;
70 return *this;
71 }
72
76 const std::optional<std::string>& getUserName() const
77 {
78 return userName;
79 }
80
84 AttachmentOptions& setUserName(const std::string& value)
85 {
86 userName = value;
87 return *this;
88 }
89
93 const std::optional<std::string>& getPassword() const
94 {
95 return password;
96 }
97
101 AttachmentOptions& setPassword(const std::string& value)
102 {
103 password = value;
104 return *this;
105 }
106
110 const std::optional<std::string>& getRole() const
111 {
112 return role;
113 }
114
118 AttachmentOptions& setRole(const std::string& value)
119 {
120 role = value;
121 return *this;
122 }
123
127 const std::optional<std::uint32_t>& getSqlDialect() const
128 {
129 return sqlDialect;
130 }
131
135 AttachmentOptions& setSqlDialect(std::uint32_t value)
136 {
137 sqlDialect = value;
138 return *this;
139 }
140
144 const std::vector<std::uint8_t>& getDpb() const
145 {
146 return dpb;
147 }
148
152 AttachmentOptions& setDpb(const std::vector<std::uint8_t>& value)
153 {
154 dpb = value;
155 return *this;
156 }
157
161 AttachmentOptions& setDpb(std::vector<std::uint8_t>&& value)
162 {
163 dpb = std::move(value);
164 return *this;
165 }
166
170 bool getCreateDatabase() const
171 {
172 return createDatabase;
173 }
174
179 {
180 createDatabase = value;
181 return *this;
182 }
183
187 const std::optional<bool>& getForcedWrites() const
188 {
189 return forcedWrites;
190 }
191
196 {
197 forcedWrites = value;
198 return *this;
199 }
200
201 private:
202 std::optional<std::string> connectionCharSet;
203 std::optional<std::string> userName;
204 std::optional<std::string> password;
205 std::optional<std::string> role;
206 std::optional<std::uint32_t> sqlDialect;
207 std::optional<bool> forcedWrites;
208 std::vector<std::uint8_t> dpb;
209 bool createDatabase = false;
210 };
211
217 class Attachment final
218 {
219 public:
224 explicit Attachment(Client& client, const std::string& uri, const AttachmentOptions& options = {});
225
230 Attachment(Attachment&& o) noexcept
231 : client{o.client},
232 handle{std::move(o.handle)}
233 {
234 }
235
243 {
244 if (this != &o)
245 {
246 client = o.client;
247 handle = std::move(o.handle);
248 }
249
250 return *this;
251 }
252
253 Attachment(const Attachment&) = delete;
254 Attachment& operator=(const Attachment&) = delete;
255
259 ~Attachment() noexcept
260 {
261 if (isValid())
262 {
263 try
264 {
265 disconnectOrDrop(false);
266 }
267 catch (...)
268 {
269 // swallow
270 }
271 }
272 }
273
274 public:
278 bool isValid() noexcept
279 {
280 return handle != nullptr;
281 }
282
286 Client& getClient() noexcept
287 {
288 return *client;
289 }
290
295 {
296 return handle;
297 }
298
302 void disconnect();
303
307 void dropDatabase();
308
312 void ping();
313
317 void resetSession();
318
322 bool execute(Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
323
327 template <typename Params>
328 bool execute(Transaction& transaction, std::string_view sql, const Params& params);
329
334 template <typename Params>
335 bool execute(
336 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
337
341 RowSet queryRowSet(
342 Transaction& transaction, std::string_view sql, unsigned maxRows, const StatementOptions& options = {});
343
348 template <typename Params>
349 RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params);
350
355 template <typename Params>
356 RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
357 const StatementOptions& options, const Params& params);
358
362 template <typename T>
363 std::optional<T> queryScalar(
364 Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
365
370 template <typename T, typename Params>
371 std::optional<T> queryScalar(Transaction& transaction, std::string_view sql, const Params& params);
372
377 template <typename T, typename Params>
378 std::optional<T> queryScalar(
379 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
380
384 template <typename T>
385 std::optional<T> queryFirstRowAs(
386 Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
387
392 template <typename T, typename Params>
393 std::optional<T> queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params);
394
399 template <typename T, typename Params>
400 std::optional<T> queryFirstRowAs(
401 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
402
403 private:
404 void disconnectOrDrop(bool drop);
405 RowSet queryPreparedRowSet(Statement& statement, Transaction& transaction, unsigned maxRows);
406
407 private:
408 Client* client;
409 FbRef<fb::IAttachment> handle;
410 };
411
412 template <typename T>
413 std::optional<T> Attachment::queryScalar(
414 Transaction& transaction, std::string_view sql, const StatementOptions& options)
415 {
416 auto rowSet = queryRowSet(transaction, sql, 1u, options);
417
418 if (rowSet.getCount() == 0u)
419 return std::nullopt;
420
421 return rowSet.getRow(0).get<std::optional<T>>(0);
422 }
423
424 template <typename T>
426 Transaction& transaction, std::string_view sql, const StatementOptions& options)
427 {
428 auto rowSet = queryRowSet(transaction, sql, 1u, options);
429
430 if (rowSet.getCount() == 0u)
431 return std::nullopt;
432
433 return rowSet.getRow(0).get<T>();
434 }
435} // namespace fbcpp
436
437#include "Statement.h"
438
439namespace fbcpp
440{
441 template <typename Params>
442 bool Attachment::execute(Transaction& transaction, std::string_view sql, const Params& params)
443 {
444 return execute(transaction, sql, StatementOptions{}, params);
445 }
446
447 template <typename Params>
449 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
450 {
451 Statement statement{*this, transaction, sql, options};
452 statement.set(params);
453 return statement.execute(transaction);
454 }
455
456 template <typename Params>
458 Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params)
459 {
460 return queryRowSet(transaction, sql, maxRows, StatementOptions{}, params);
461 }
462
463 template <typename Params>
464 RowSet Attachment::queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
465 const StatementOptions& options, const Params& params)
466 {
467 Statement statement{*this, transaction, sql, options};
468 statement.set(params);
469 return queryPreparedRowSet(statement, transaction, maxRows);
470 }
471
472 template <typename T, typename Params>
473 std::optional<T> Attachment::queryScalar(Transaction& transaction, std::string_view sql, const Params& params)
474 {
475 auto rowSet = queryRowSet(transaction, sql, 1u, params);
476
477 if (rowSet.getCount() == 0u)
478 return std::nullopt;
479
480 return rowSet.getRow(0).template get<std::optional<T>>(0);
481 }
482
483 template <typename T, typename Params>
484 std::optional<T> Attachment::queryScalar(
485 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
486 {
487 auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
488
489 if (rowSet.getCount() == 0u)
490 return std::nullopt;
491
492 return rowSet.getRow(0).template get<std::optional<T>>(0);
493 }
494
495 template <typename T, typename Params>
496 std::optional<T> Attachment::queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params)
497 {
498 auto rowSet = queryRowSet(transaction, sql, 1u, params);
499
500 if (rowSet.getCount() == 0u)
501 return std::nullopt;
502
503 return rowSet.getRow(0).template get<T>();
504 }
505
506 template <typename T, typename Params>
508 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
509 {
510 auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
511
512 if (rowSet.getCount() == 0u)
513 return std::nullopt;
514
515 return rowSet.getRow(0).template get<T>();
516 }
517} // namespace fbcpp
518
519#endif // FBCPP_ATTACHMENT_H
Represents options used when creating an Attachment object.
Definition Attachment.h:54
AttachmentOptions & setConnectionCharSet(const std::string &value)
Sets the character set which will be used for the connection.
Definition Attachment.h:67
const std::optional< std::string > & getPassword() const
Returns the password which will be used to connect to the database.
Definition Attachment.h:93
AttachmentOptions & setSqlDialect(std::uint32_t value)
Sets the SQL dialect which will be used to connect to the database.
Definition Attachment.h:135
AttachmentOptions & setPassword(const std::string &value)
Sets the password which will be used to connect to the database.
Definition Attachment.h:101
const std::optional< std::string > & getRole() const
Returns the role which will be used to connect to the database.
Definition Attachment.h:110
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:152
AttachmentOptions & setRole(const std::string &value)
Sets the role which will be used to connect to the database.
Definition Attachment.h:118
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:161
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:144
AttachmentOptions & setCreateDatabase(bool value)
Sets whether the database should be created instead of connected to.
Definition Attachment.h:178
const std::optional< std::string > & getUserName() const
Returns the user name which will be used to connect to the database.
Definition Attachment.h:76
bool getCreateDatabase() const
Returns whether the database should be created instead of connected to.
Definition Attachment.h:170
const std::optional< std::string > & getConnectionCharSet() const
Returns the character set which will be used for the connection.
Definition Attachment.h:59
AttachmentOptions & setUserName(const std::string &value)
Sets the user name which will be used to connect to the database.
Definition Attachment.h:84
const std::optional< bool > & getForcedWrites() const
Returns whether forced writes should be enabled when creating the database.
Definition Attachment.h:187
AttachmentOptions & setForcedWrites(bool value)
Sets whether forced writes should be enabled when creating the database.
Definition Attachment.h:195
const std::optional< std::uint32_t > & getSqlDialect() const
Returns the SQL dialect which will be used to connect to the database.
Definition Attachment.h:127
Represents a connection to a Firebird database.
Definition Attachment.h:218
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:286
void disconnect()
Disconnects from the database.
FbRef< fb::IAttachment > getHandle() noexcept
Returns the internal Firebird IAttachment handle.
Definition Attachment.h:294
~Attachment() noexcept
Disconnects from the database.
Definition Attachment.h:259
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:425
Attachment(Attachment &&o) noexcept
Move constructor.
Definition Attachment.h:230
void ping()
Checks if the connection to the database is alive.
bool isValid() noexcept
Returns whether the Attachment object is valid.
Definition Attachment.h:278
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:413
void dropDatabase()
Drops the database.
void resetSession()
Resets the session state.
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:242
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
A disconnected buffer of rows fetched from a Statement's result set.
Definition RowSet.h:56
Represents options used when preparing a Statement.
Prepares, executes, and fetches SQL statements against a Firebird attachment.
Definition Statement.h:138
Represents a transaction in one or more Firebird databases.
fb-cpp namespace.
Definition Attachment.h:45