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 bool execute(Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
313
317 template <typename Params>
318 bool execute(Transaction& transaction, std::string_view sql, const Params& params);
319
324 template <typename Params>
325 bool execute(
326 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
327
331 RowSet queryRowSet(
332 Transaction& transaction, std::string_view sql, unsigned maxRows, const StatementOptions& options = {});
333
338 template <typename Params>
339 RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params);
340
345 template <typename Params>
346 RowSet queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
347 const StatementOptions& options, const Params& params);
348
352 template <typename T>
353 std::optional<T> queryScalar(
354 Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
355
360 template <typename T, typename Params>
361 std::optional<T> queryScalar(Transaction& transaction, std::string_view sql, const Params& params);
362
367 template <typename T, typename Params>
368 std::optional<T> queryScalar(
369 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
370
374 template <typename T>
375 std::optional<T> queryFirstRowAs(
376 Transaction& transaction, std::string_view sql, const StatementOptions& options = {});
377
382 template <typename T, typename Params>
383 std::optional<T> queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params);
384
389 template <typename T, typename Params>
390 std::optional<T> queryFirstRowAs(
391 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params);
392
393 private:
394 void disconnectOrDrop(bool drop);
395 RowSet queryPreparedRowSet(Statement& statement, Transaction& transaction, unsigned maxRows);
396
397 private:
398 Client* client;
399 FbRef<fb::IAttachment> handle;
400 };
401
402 template <typename T>
403 std::optional<T> Attachment::queryScalar(
404 Transaction& transaction, std::string_view sql, const StatementOptions& options)
405 {
406 auto rowSet = queryRowSet(transaction, sql, 1u, options);
407
408 if (rowSet.getCount() == 0u)
409 return std::nullopt;
410
411 return rowSet.getRow(0).get<std::optional<T>>(0);
412 }
413
414 template <typename T>
416 Transaction& transaction, std::string_view sql, const StatementOptions& options)
417 {
418 auto rowSet = queryRowSet(transaction, sql, 1u, options);
419
420 if (rowSet.getCount() == 0u)
421 return std::nullopt;
422
423 return rowSet.getRow(0).get<T>();
424 }
425} // namespace fbcpp
426
427#include "Statement.h"
428
429namespace fbcpp
430{
431 template <typename Params>
432 bool Attachment::execute(Transaction& transaction, std::string_view sql, const Params& params)
433 {
434 return execute(transaction, sql, StatementOptions{}, params);
435 }
436
437 template <typename Params>
439 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
440 {
441 Statement statement{*this, transaction, sql, options};
442 statement.set(params);
443 return statement.execute(transaction);
444 }
445
446 template <typename Params>
448 Transaction& transaction, std::string_view sql, unsigned maxRows, const Params& params)
449 {
450 return queryRowSet(transaction, sql, maxRows, StatementOptions{}, params);
451 }
452
453 template <typename Params>
454 RowSet Attachment::queryRowSet(Transaction& transaction, std::string_view sql, unsigned maxRows,
455 const StatementOptions& options, const Params& params)
456 {
457 Statement statement{*this, transaction, sql, options};
458 statement.set(params);
459 return queryPreparedRowSet(statement, transaction, maxRows);
460 }
461
462 template <typename T, typename Params>
463 std::optional<T> Attachment::queryScalar(Transaction& transaction, std::string_view sql, const Params& params)
464 {
465 auto rowSet = queryRowSet(transaction, sql, 1u, params);
466
467 if (rowSet.getCount() == 0u)
468 return std::nullopt;
469
470 return rowSet.getRow(0).template get<std::optional<T>>(0);
471 }
472
473 template <typename T, typename Params>
474 std::optional<T> Attachment::queryScalar(
475 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
476 {
477 auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
478
479 if (rowSet.getCount() == 0u)
480 return std::nullopt;
481
482 return rowSet.getRow(0).template get<std::optional<T>>(0);
483 }
484
485 template <typename T, typename Params>
486 std::optional<T> Attachment::queryFirstRowAs(Transaction& transaction, std::string_view sql, const Params& params)
487 {
488 auto rowSet = queryRowSet(transaction, sql, 1u, params);
489
490 if (rowSet.getCount() == 0u)
491 return std::nullopt;
492
493 return rowSet.getRow(0).template get<T>();
494 }
495
496 template <typename T, typename Params>
498 Transaction& transaction, std::string_view sql, const StatementOptions& options, const Params& params)
499 {
500 auto rowSet = queryRowSet(transaction, sql, 1u, options, params);
501
502 if (rowSet.getCount() == 0u)
503 return std::nullopt;
504
505 return rowSet.getRow(0).template get<T>();
506 }
507} // namespace fbcpp
508
509#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:415
Attachment(Attachment &&o) noexcept
Move constructor.
Definition Attachment.h:230
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:403
void dropDatabase()
Drops the database.
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