fb-cpp 0.0.1
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 {
72 MAIN = isc_bpb_storage_main,
73 TEMPORARY = isc_bpb_storage_temp
74 };
75
79 enum class BlobType : std::uint8_t
80 {
81 SEGMENTED = isc_bpb_type_segmented,
82 STREAM = isc_bpb_type_stream
83 };
84
88 class BlobOptions final
89 {
90 public:
94 const std::vector<std::uint8_t>& getBpb() const noexcept
95 {
96 return bpb;
97 }
98
102 BlobOptions& setBpb(const std::vector<std::uint8_t>& value)
103 {
104 bpb = value;
105 return *this;
106 }
107
111 BlobOptions& setBpb(std::vector<std::uint8_t>&& value)
112 {
113 bpb = std::move(value);
114 return *this;
115 }
116
120 const std::optional<BlobType> getType() const
121 {
122 return type;
123 }
124
129 {
130 type = value;
131 return *this;
132 }
133
137 const std::optional<BlobType> getSourceType() const
138 {
139 return sourceType;
140 }
141
146 {
147 sourceType = value;
148 return *this;
149 }
150
154 const std::optional<BlobType> getTargetType() const
155 {
156 return targetType;
157 }
158
163 {
164 targetType = value;
165 return *this;
166 }
167
171 const std::optional<std::int16_t> getSourceCharSet() const
172 {
173 return sourceCharSet;
174 }
175
179 BlobOptions& setSourceCharSet(std::int16_t value)
180 {
181 sourceCharSet = value;
182 return *this;
183 }
184
188 const std::optional<std::int16_t> getTargetCharSet() const
189 {
190 return targetCharSet;
191 }
192
196 BlobOptions& setTargetCharSet(std::int16_t value)
197 {
198 targetCharSet = value;
199 return *this;
200 }
201
205 const std::optional<BlobStorage> getStorage() const
206 {
207 return storage;
208 }
209
214 {
215 storage = value;
216 return *this;
217 }
218
219 private:
220 std::vector<std::uint8_t> bpb;
221 std::optional<BlobType> type;
222 std::optional<BlobType> sourceType;
223 std::optional<BlobType> targetType;
224 std::optional<std::int16_t> sourceCharSet;
225 std::optional<std::int16_t> targetCharSet;
226 std::optional<BlobStorage> storage;
227 };
228
232 enum class BlobSeekMode : int
233 {
234 FROM_BEGIN = 0,
235 FROM_CURRENT = blb_seek_relative,
236 FROM_END = blb_seek_from_tail
237 };
238
242 class Blob final
243 {
244 public:
248 Blob(Attachment& attachment, Transaction& transaction, const BlobOptions& options = {});
249
253 Blob(Attachment& attachment, Transaction& transaction, const BlobId& blobId, const BlobOptions& options = {});
254
258 Blob(Blob&& o) noexcept
259 : attachment{o.attachment},
260 transaction{o.transaction},
261 id{o.id},
262 status{std::move(o.status)},
263 statusWrapper{std::move(o.statusWrapper)},
264 handle{std::move(o.handle)}
265 {
266 }
267
271 Blob& operator=(Blob&&) = delete;
272
276 Blob(const Blob&) = delete;
277
281 Blob& operator=(const Blob&) = delete;
282
286 ~Blob() noexcept
287 {
288 if (isValid())
289 {
290 try
291 {
292 close();
293 }
294 catch (...)
295 {
296 // swallow
297 }
298 }
299 }
300
301 public:
305 bool isValid() const noexcept
306 {
307 return handle != nullptr;
308 }
309
313 const BlobId& getId() const noexcept
314 {
315 return id;
316 }
317
321 unsigned getLength();
322
327 {
328 return handle;
329 }
330
334 unsigned read(std::span<std::byte> buffer);
335
339 unsigned read(std::span<char> buffer)
340 {
341 return read(std::as_writable_bytes(buffer));
342 }
343
347 unsigned readSegment(std::span<std::byte> buffer);
348
352 unsigned readSegment(std::span<char> buffer)
353 {
354 return readSegment(std::as_writable_bytes(buffer));
355 }
356
360 void write(std::span<const std::byte> buffer);
361
365 void write(std::span<const char> buffer)
366 {
367 write(std::as_bytes(buffer));
368 }
369
373 void writeSegment(std::span<const std::byte> buffer);
374
378 void writeSegment(std::span<const char> buffer)
379 {
380 writeSegment(std::as_bytes(buffer));
381 }
382
386 int seek(BlobSeekMode mode, int offset);
387
391 void cancel();
392
396 void close();
397
398 private:
399 std::vector<std::uint8_t> prepareBpb(const BlobOptions& options);
400
401 private:
402 Attachment& attachment;
403 Transaction& transaction;
404 BlobId id;
405 FbUniquePtr<fb::IStatus> status;
406 impl::StatusWrapper statusWrapper;
407 FbRef<fb::IBlob> handle;
408 };
409} // namespace fbcpp
410
411
412#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:89
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:102
const std::optional< BlobType > getSourceType() const
Retrieves the source blob subtype.
Definition Blob.h:137
const std::optional< BlobType > getType() const
Retrieves the blob type to be used for blob operations.
Definition Blob.h:120
const std::optional< BlobType > getTargetType() const
Retrieves the target blob subtype.
Definition Blob.h:154
BlobOptions & setType(BlobType value)
Sets the blob type to be used for blob operations.
Definition Blob.h:128
BlobOptions & setTargetCharSet(std::int16_t value)
Sets the target character set identifier.
Definition Blob.h:196
BlobOptions & setStorage(BlobStorage value)
Sets the blob storage mode.
Definition Blob.h:213
const std::optional< BlobStorage > getStorage() const
Retrieves the blob storage mode.
Definition Blob.h:205
BlobOptions & setTargetType(BlobType value)
Sets the target blob subtype.
Definition Blob.h:162
const std::vector< std::uint8_t > & getBpb() const noexcept
Retrieves the blob parameter block (BPB) used during blob operations.
Definition Blob.h:94
BlobOptions & setBpb(std::vector< std::uint8_t > &&value)
Sets the blob parameter block (BPB) by moving the provided value.
Definition Blob.h:111
const std::optional< std::int16_t > getTargetCharSet() const
Retrieves the target character set identifier.
Definition Blob.h:188
BlobOptions & setSourceCharSet(std::int16_t value)
Sets the source character set identifier.
Definition Blob.h:179
const std::optional< std::int16_t > getSourceCharSet() const
Retrieves the source character set identifier.
Definition Blob.h:171
BlobOptions & setSourceType(BlobType value)
Sets the source blob subtype.
Definition Blob.h:145
Provides read and write access to Firebird blobs.
Definition Blob.h:243
bool isValid() const noexcept
Returns whether the blob handle is valid.
Definition Blob.h:305
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:339
Blob(Blob &&o) noexcept
Transfers blob ownership from another instance.
Definition Blob.h:258
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:352
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:365
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:286
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:378
const BlobId & getId() const noexcept
Provides access to the current blob identifier.
Definition Blob.h:313
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:326
void close()
Closes the blob and finalizes any pending changes.
Definition Blob.cpp:246
Represents a transaction in a Firebird database.
fb-cpp namespace.
Definition Attachment.h:42
BlobStorage
Blob storage options.
Definition Blob.h:71
BlobSeekMode
Defines the origin used when repositioning a blob.
Definition Blob.h:233
BlobType
Blob type.
Definition Blob.h:80