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