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-api.h"
29#include "SmartPtrs.h"
30#include "Exception.h"
31#include <cassert>
32#include <cstddef>
33#include <cstdint>
34#include <optional>
35#include <span>
36#include <utility>
37#include <vector>
38
39
43namespace fbcpp
44{
45 class Attachment;
46 class Transaction;
47
51 class BlobId final
52 {
53 public:
55 bool isEmpty() const noexcept
56 {
57 return id.gds_quad_high == 0 && id.gds_quad_low == 0;
58 }
59
60 public:
64 ISC_QUAD id{0, 0};
65 };
66
70 enum class BlobStorage : std::uint8_t
71 {
75 MAIN = isc_bpb_storage_main,
76
80 TEMPORARY = isc_bpb_storage_temp
81 };
82
86 enum class BlobType : std::uint8_t
87 {
91 SEGMENTED = isc_bpb_type_segmented,
92
96 STREAM = isc_bpb_type_stream
97 };
98
102 class BlobOptions final
103 {
104 public:
108 const std::vector<std::uint8_t>& getBpb() const noexcept
109 {
110 return bpb;
111 }
112
116 BlobOptions& setBpb(const std::vector<std::uint8_t>& value)
117 {
118 bpb = value;
119 return *this;
120 }
121
125 BlobOptions& setBpb(std::vector<std::uint8_t>&& value)
126 {
127 bpb = std::move(value);
128 return *this;
129 }
130
134 const std::optional<BlobType> getType() const
135 {
136 return type;
137 }
138
143 {
144 type = value;
145 return *this;
146 }
147
151 const std::optional<BlobType> getSourceType() const
152 {
153 return sourceType;
154 }
155
160 {
161 sourceType = value;
162 return *this;
163 }
164
168 const std::optional<BlobType> getTargetType() const
169 {
170 return targetType;
171 }
172
177 {
178 targetType = value;
179 return *this;
180 }
181
185 const std::optional<std::int16_t> getSourceCharSet() const
186 {
187 return sourceCharSet;
188 }
189
193 BlobOptions& setSourceCharSet(std::int16_t value)
194 {
195 sourceCharSet = value;
196 return *this;
197 }
198
202 const std::optional<std::int16_t> getTargetCharSet() const
203 {
204 return targetCharSet;
205 }
206
210 BlobOptions& setTargetCharSet(std::int16_t value)
211 {
212 targetCharSet = value;
213 return *this;
214 }
215
219 const std::optional<BlobStorage> getStorage() const
220 {
221 return storage;
222 }
223
228 {
229 storage = value;
230 return *this;
231 }
232
233 private:
234 std::vector<std::uint8_t> bpb;
235 std::optional<BlobType> type;
236 std::optional<BlobType> sourceType;
237 std::optional<BlobType> targetType;
238 std::optional<std::int16_t> sourceCharSet;
239 std::optional<std::int16_t> targetCharSet;
240 std::optional<BlobStorage> storage;
241 };
242
246 enum class BlobSeekMode : int
247 {
251 FROM_BEGIN = 0,
252
256 FROM_CURRENT = blb_seek_relative,
257
261 FROM_END = blb_seek_from_tail
262 };
263
267 class Blob final
268 {
269 public:
273 Blob(Attachment& attachment, Transaction& transaction, const BlobOptions& options = {});
274
278 Blob(Attachment& attachment, Transaction& transaction, const BlobId& blobId, const BlobOptions& options = {});
279
283 Blob(Blob&& o) noexcept
284 : attachment{o.attachment},
285 transaction{o.transaction},
286 id{o.id},
287 status{std::move(o.status)},
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;
431 impl::StatusWrapper statusWrapper;
432 FbRef<fb::IBlob> handle;
433 };
434} // namespace fbcpp
435
436
437#endif // FBCPP_BLOB_H
Represents a connection to a Firebird database.
Definition Attachment.h:177
Represents a Firebird blob identifier.
Definition Blob.h:52
bool isEmpty() const noexcept
Returns whether this blob identifier is empty.
Definition Blob.h:55
Additional options used when creating or opening blobs.
Definition Blob.h:103
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:116
const std::optional< BlobType > getSourceType() const
Retrieves the source blob subtype.
Definition Blob.h:151
const std::optional< BlobType > getType() const
Retrieves the blob type to be used for blob operations.
Definition Blob.h:134
const std::optional< BlobType > getTargetType() const
Retrieves the target blob subtype.
Definition Blob.h:168
BlobOptions & setType(BlobType value)
Sets the blob type to be used for blob operations.
Definition Blob.h:142
BlobOptions & setTargetCharSet(std::int16_t value)
Sets the target character set identifier.
Definition Blob.h:210
BlobOptions & setStorage(BlobStorage value)
Sets the blob storage mode.
Definition Blob.h:227
const std::optional< BlobStorage > getStorage() const
Retrieves the blob storage mode.
Definition Blob.h:219
BlobOptions & setTargetType(BlobType value)
Sets the target blob subtype.
Definition Blob.h:176
const std::vector< std::uint8_t > & getBpb() const noexcept
Retrieves the blob parameter block (BPB) used during blob operations.
Definition Blob.h:108
BlobOptions & setBpb(std::vector< std::uint8_t > &&value)
Sets the blob parameter block (BPB) by moving the provided value.
Definition Blob.h:125
const std::optional< std::int16_t > getTargetCharSet() const
Retrieves the target character set identifier.
Definition Blob.h:202
BlobOptions & setSourceCharSet(std::int16_t value)
Sets the source character set identifier.
Definition Blob.h:193
const std::optional< std::int16_t > getSourceCharSet() const
Retrieves the source character set identifier.
Definition Blob.h:185
BlobOptions & setSourceType(BlobType value)
Sets the source blob subtype.
Definition Blob.h:159
Provides read and write access to Firebird blobs.
Definition Blob.h:268
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:283
unsigned read(std::span< std::byte > buffer)
Reads data from the blob into the provided buffer.
Definition Blob.cpp:156
unsigned readSegment(std::span< char > buffer)
Reads a single segment from the blob into the provided buffer.
Definition Blob.h:377
unsigned getLength()
Retrieves the length of the blob in bytes.
Definition Blob.cpp:106
void write(std::span< const char > buffer)
Writes data from the buffer into the blob.
Definition Blob.h:390
void cancel()
Cancels any changes performed on the blob and releases the handle.
Definition Blob.cpp:238
~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
void write(std::span< const std::byte > buffer)
Writes data from the buffer into the blob.
Definition Blob.cpp:204
int seek(BlobSeekMode mode, int offset)
Repositions the blob read/write cursor.
Definition Blob.cpp:232
unsigned readSegment(std::span< std::byte > buffer)
Reads a single segment from the blob into the provided buffer.
Definition Blob.cpp:179
Blob & operator=(const Blob &)=delete
Copy assignment is not supported.
void writeSegment(std::span< const std::byte > buffer)
Writes a single segment from the buffer into the blob.
Definition Blob.cpp:219
FbRef< fb::IBlob > getHandle() noexcept
Exposes the underlying Firebird blob handle.
Definition Blob.h:351
void close()
Closes the blob and finalizes any pending changes.
Definition Blob.cpp:246
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:42
BlobStorage
Blob storage options.
Definition Blob.h:71
@ 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:247
@ 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:87
@ STREAM
Blob is treated as a continuous stream of bytes.
@ SEGMENTED
Blob is stored and accessed as discrete segments.
std::unique_ptr< T, impl::FbDisposeDeleter > FbUniquePtr
Unique pointer type for Firebird disposable objects.
Definition SmartPtrs.h:53