fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
Blob.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 do so, subject to the
11 * 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_BLOB_H
26#define FBCPP_BLOB_H
27
28#include "fb-cpp-export.h"
29#include "fb-api.h"
30#include "SmartPtrs.h"
31#include "Exception.h"
32#include <cassert>
33#include <cstddef>
34#include <cstdint>
35#include <optional>
36#include <span>
37#include <utility>
38#include <vector>
39
40
44namespace fbcpp
45{
46 class Attachment;
47 class Transaction;
48
52 class BlobId final
53 {
54 public:
56 bool isEmpty() const noexcept
57 {
58 return id.gds_quad_high == 0 && id.gds_quad_low == 0;
59 }
60
61 public:
65 ISC_QUAD id{0, 0};
66 };
67
71 enum class BlobStorage : std::uint8_t
72 {
76 MAIN = isc_bpb_storage_main,
77
81 TEMPORARY = isc_bpb_storage_temp
82 };
83
87 enum class BlobType : std::uint8_t
88 {
92 SEGMENTED = isc_bpb_type_segmented,
93
97 STREAM = isc_bpb_type_stream
98 };
99
103 class BlobOptions final
104 {
105 public:
109 const std::vector<std::uint8_t>& getBpb() const noexcept
110 {
111 return bpb;
112 }
113
117 BlobOptions& setBpb(const std::vector<std::uint8_t>& value)
118 {
119 bpb = value;
120 return *this;
121 }
122
126 BlobOptions& setBpb(std::vector<std::uint8_t>&& value)
127 {
128 bpb = std::move(value);
129 return *this;
130 }
131
135 const std::optional<BlobType> getType() const
136 {
137 return type;
138 }
139
144 {
145 type = value;
146 return *this;
147 }
148
152 const std::optional<BlobType> getSourceType() const
153 {
154 return sourceType;
155 }
156
161 {
162 sourceType = value;
163 return *this;
164 }
165
169 const std::optional<BlobType> getTargetType() const
170 {
171 return targetType;
172 }
173
178 {
179 targetType = value;
180 return *this;
181 }
182
186 const std::optional<std::int16_t> getSourceCharSet() const
187 {
188 return sourceCharSet;
189 }
190
194 BlobOptions& setSourceCharSet(std::int16_t value)
195 {
196 sourceCharSet = value;
197 return *this;
198 }
199
203 const std::optional<std::int16_t> getTargetCharSet() const
204 {
205 return targetCharSet;
206 }
207
211 BlobOptions& setTargetCharSet(std::int16_t value)
212 {
213 targetCharSet = value;
214 return *this;
215 }
216
220 const std::optional<BlobStorage> getStorage() const
221 {
222 return storage;
223 }
224
229 {
230 storage = value;
231 return *this;
232 }
233
234 private:
235 std::vector<std::uint8_t> bpb;
236 std::optional<BlobType> type;
237 std::optional<BlobType> sourceType;
238 std::optional<BlobType> targetType;
239 std::optional<std::int16_t> sourceCharSet;
240 std::optional<std::int16_t> targetCharSet;
241 std::optional<BlobStorage> storage;
242 };
243
247 enum class BlobSeekMode : int
248 {
252 FROM_BEGIN = 0,
253
257 FROM_CURRENT = blb_seek_relative,
258
262 FROM_END = blb_seek_from_tail
263 };
264
268 class FB_CPP_EXPORT Blob final
269 {
270 public:
274 Blob(Attachment& attachment, Transaction& transaction, const BlobOptions& options = {});
275
279 Blob(Attachment& attachment, Transaction& transaction, const BlobId& blobId, const BlobOptions& options = {});
280
284 Blob(Blob&& o) noexcept
285 : attachment{o.attachment},
286 transaction{o.transaction},
287 id{o.id},
288 statusWrapper{std::move(o.statusWrapper)},
289 handle{std::move(o.handle)}
290 {
291 }
292
296 Blob& operator=(Blob&&) = delete;
297
301 Blob(const Blob&) = delete;
302
306 Blob& operator=(const Blob&) = delete;
307
311 ~Blob() noexcept
312 {
313 if (isValid())
314 {
315 try
316 {
317 close();
318 }
319 catch (...)
320 {
321 // swallow
322 }
323 }
324 }
325
326 public:
330 bool isValid() const noexcept
331 {
332 return handle != nullptr;
333 }
334
338 const BlobId& getId() const noexcept
339 {
340 return id;
341 }
342
346 unsigned getLength();
347
352 {
353 return handle;
354 }
355
359 unsigned read(std::span<std::byte> buffer);
360
364 unsigned read(std::span<char> buffer)
365 {
366 return read(std::as_writable_bytes(buffer));
367 }
368
372 unsigned readSegment(std::span<std::byte> buffer);
373
377 unsigned readSegment(std::span<char> buffer)
378 {
379 return readSegment(std::as_writable_bytes(buffer));
380 }
381
385 void write(std::span<const std::byte> buffer);
386
390 void write(std::span<const char> buffer)
391 {
392 write(std::as_bytes(buffer));
393 }
394
398 void writeSegment(std::span<const std::byte> buffer);
399
403 void writeSegment(std::span<const char> buffer)
404 {
405 writeSegment(std::as_bytes(buffer));
406 }
407
411 int seek(BlobSeekMode mode, int offset);
412
416 void cancel();
417
421 void close();
422
423 private:
424 std::vector<std::uint8_t> prepareBpb(const BlobOptions& options);
425
426 private:
427 Attachment& attachment;
428 Transaction& transaction;
429 BlobId id;
430 impl::StatusWrapper statusWrapper;
431 FbRef<fb::IBlob> handle;
432 };
433} // namespace fbcpp
434
435
436#endif // FBCPP_BLOB_H
Represents a connection to a Firebird database.
Definition Attachment.h:219
Represents a Firebird blob identifier.
Definition Blob.h:53
bool isEmpty() const noexcept
Returns whether this blob identifier is empty.
Definition Blob.h:56
Additional options used when creating or opening blobs.
Definition Blob.h:104
BlobOptions & setBpb(const std::vector< std::uint8_t > &value)
Sets the blob parameter block (BPB) using a copy of the provided value.
Definition Blob.h:117
const std::optional< BlobType > getSourceType() const
Retrieves the source blob subtype.
Definition Blob.h:152
const std::optional< BlobType > getType() const
Retrieves the blob type to be used for blob operations.
Definition Blob.h:135
const std::optional< BlobType > getTargetType() const
Retrieves the target blob subtype.
Definition Blob.h:169
BlobOptions & setType(BlobType value)
Sets the blob type to be used for blob operations.
Definition Blob.h:143
BlobOptions & setTargetCharSet(std::int16_t value)
Sets the target character set identifier.
Definition Blob.h:211
BlobOptions & setStorage(BlobStorage value)
Sets the blob storage mode.
Definition Blob.h:228
const std::optional< BlobStorage > getStorage() const
Retrieves the blob storage mode.
Definition Blob.h:220
BlobOptions & setTargetType(BlobType value)
Sets the target blob subtype.
Definition Blob.h:177
const std::vector< std::uint8_t > & getBpb() const noexcept
Retrieves the blob parameter block (BPB) used during blob operations.
Definition Blob.h:109
BlobOptions & setBpb(std::vector< std::uint8_t > &&value)
Sets the blob parameter block (BPB) by moving the provided value.
Definition Blob.h:126
const std::optional< std::int16_t > getTargetCharSet() const
Retrieves the target character set identifier.
Definition Blob.h:203
BlobOptions & setSourceCharSet(std::int16_t value)
Sets the source character set identifier.
Definition Blob.h:194
const std::optional< std::int16_t > getSourceCharSet() const
Retrieves the source character set identifier.
Definition Blob.h:186
BlobOptions & setSourceType(BlobType value)
Sets the source blob subtype.
Definition Blob.h:160
Provides read and write access to Firebird blobs.
Definition Blob.h:269
bool isValid() const noexcept
Returns whether the blob handle is valid.
Definition Blob.h:330
Blob(const Blob &)=delete
Copy construction is not supported.
unsigned read(std::span< char > buffer)
Reads data from the blob into the provided buffer.
Definition Blob.h:364
Blob(Blob &&o) noexcept
Transfers blob ownership from another instance.
Definition Blob.h:284
unsigned readSegment(std::span< char > buffer)
Reads a single segment from the blob into the provided buffer.
Definition Blob.h:377
void write(std::span< const char > buffer)
Writes data from the buffer into the blob.
Definition Blob.h:390
~Blob() noexcept
Automatically closes the blob if still open.
Definition Blob.h:311
Blob & operator=(Blob &&)=delete
Move assignment is not supported.
void writeSegment(std::span< const char > buffer)
Writes a single segment from the buffer into the blob.
Definition Blob.h:403
const BlobId & getId() const noexcept
Provides access to the current blob identifier.
Definition Blob.h:338
Blob & operator=(const Blob &)=delete
Copy assignment is not supported.
FbRef< fb::IBlob > getHandle() noexcept
Exposes the underlying Firebird blob handle.
Definition Blob.h:351
Reference-counted smart pointer for Firebird objects using addRef/release semantics.
Definition SmartPtrs.h:70
Represents a transaction in one or more Firebird databases.
fb-cpp namespace.
Definition Attachment.h:46
BlobStorage
Blob storage options.
Definition Blob.h:72
@ MAIN
Blob is stored in the main database file.
@ TEMPORARY
Blob is stored in temporary storage and will not persist beyond the transaction.
BlobSeekMode
Defines the origin used when repositioning a blob.
Definition Blob.h:248
@ FROM_BEGIN
Offset is relative to the beginning of the blob.
@ FROM_CURRENT
Offset is relative to the current position in the blob.
@ FROM_END
Offset is relative to the end of the blob.
BlobType
Blob type.
Definition Blob.h:88
@ SEGMENTED
Blob is stored and accessed as discrete segments.
@ STREAM
Blobs are sent inline as a stream.