fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
Batch.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 F.D.Castel
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_BATCH_H
26#define FBCPP_BATCH_H
27
28#include "fb-api.h"
29#include "Blob.h"
30#include "Descriptor.h"
31#include "SmartPtrs.h"
32#include "Exception.h"
33#include <cassert>
34#include <cstddef>
35#include <cstdint>
36#include <optional>
37#include <span>
38#include <string_view>
39#include <vector>
40
41
45namespace fbcpp
46{
47 class Attachment;
48 class Client;
49 class Statement;
50 class Transaction;
51
55 enum class BlobPolicy : std::uint8_t
56 {
60 NONE = fb::IBatch::BLOB_NONE,
61
65 ID_ENGINE = fb::IBatch::BLOB_ID_ENGINE,
66
70 ID_USER = fb::IBatch::BLOB_ID_USER,
71
75 STREAM = fb::IBatch::BLOB_STREAM,
76 };
77
84 class BatchOptions final
85 {
86 public:
90 bool getMultiError() const
91 {
92 return multiError;
93 }
94
99 {
100 multiError = value;
101 return *this;
102 }
103
107 bool getRecordCounts() const
108 {
109 return recordCounts;
110 }
111
116 {
117 recordCounts = value;
118 return *this;
119 }
120
124 std::optional<unsigned> getBufferBytesSize() const
125 {
126 return bufferBytesSize;
127 }
128
133 {
134 bufferBytesSize = value;
135 return *this;
136 }
137
142 {
143 return blobPolicy;
144 }
145
150 {
151 blobPolicy = value;
152 return *this;
153 }
154
158 unsigned getDetailedErrors() const
159 {
160 return detailedErrors;
161 }
162
167 {
168 detailedErrors = value;
169 return *this;
170 }
171
172 private:
173 bool multiError = false;
174 bool recordCounts = false;
175 std::optional<unsigned> bufferBytesSize;
176 BlobPolicy blobPolicy = BlobPolicy::NONE;
177 unsigned detailedErrors = 64;
178 };
179
186 {
187 public:
191 static constexpr int EXECUTE_FAILED = fb::IBatchCompletionState::EXECUTE_FAILED;
192
196 static constexpr int SUCCESS_NO_INFO = fb::IBatchCompletionState::SUCCESS_NO_INFO;
197
198 public:
202 explicit BatchCompletionState(Client& client, FbUniquePtr<fb::IBatchCompletionState> handle) noexcept;
203
208
213
218
223
224 public:
228 unsigned getSize();
229
235 int getState(unsigned pos);
236
242 std::optional<unsigned> findError(unsigned pos);
243
249 std::vector<std::intptr_t> getStatus(unsigned pos);
250
251 private:
252 Client* client;
254 impl::StatusWrapper statusWrapper;
256 };
257
274 class Batch final
275 {
276 public:
282 explicit Batch(Statement& statement, Transaction& transaction, const BatchOptions& options = {});
283
287 explicit Batch(Attachment& attachment, Transaction& transaction, std::string_view sql, unsigned dialect = 3,
288 const BatchOptions& options = {});
289
293 Batch(Batch&& o) noexcept;
294
298 Batch& operator=(Batch&&) = delete;
299
303 Batch(const Batch&) = delete;
304
308 Batch& operator=(const Batch&) = delete;
309
313 ~Batch() noexcept
314 {
315 if (isValid())
316 {
317 try
318 {
319 close();
320 }
321 catch (...)
322 {
323 // swallow
324 }
325 }
326 }
327
328 public:
332 bool isValid() const noexcept
333 {
334 return handle != nullptr;
335 }
336
341
347 void add(unsigned count, const void* inBuffer);
348
359 void addMessage();
360
364
369
375 BlobId addBlob(std::span<const std::byte> data, const BlobOptions& bpb = {});
376
380 void appendBlobData(std::span<const std::byte> data);
381
385 void addBlobStream(std::span<const std::byte> data);
386
391 BlobId registerBlob(const BlobId& existingBlob);
392
396 void setDefaultBpb(const BlobOptions& bpb);
397
401 unsigned getBlobAlignment();
402
406
411
415 BatchCompletionState execute();
416
420 void cancel();
421
425 void close();
426
430 FbRef<fb::IMessageMetadata> getInputMetadata();
431
435 const std::vector<Descriptor>& getInputDescriptors();
436
440
441 private:
442 std::vector<std::uint8_t> buildParametersBlock(Client& client, const BatchOptions& options);
443 std::vector<std::uint8_t> prepareBpb(Client& client, const BlobOptions& bpb);
444 void buildInputDescriptors();
445
446 private:
447 Client* client;
448 Transaction* transaction;
449 Statement* statement = nullptr;
450 FbUniquePtr<fb::IStatus> status;
451 impl::StatusWrapper statusWrapper;
452 FbRef<fb::IBatch> handle;
453 std::vector<Descriptor> inputDescriptors;
454 };
455} // namespace fbcpp
456
457
458#endif // FBCPP_BATCH_H
Represents a connection to a Firebird database.
Definition Attachment.h:213
Wraps IBatchCompletionState to provide RAII-safe access to batch execution results.
Definition Batch.h:186
BatchCompletionState & operator=(BatchCompletionState &&)=delete
Move assignment is not supported.
BatchCompletionState & operator=(const BatchCompletionState &)=delete
Copy assignment is not supported.
BatchCompletionState(const BatchCompletionState &)=delete
Copy construction is not supported.
std::optional< unsigned > findError(unsigned pos)
Finds the next error at or after the given position.
Definition Batch.cpp:63
static constexpr int EXECUTE_FAILED
Per-message state value indicating the message failed to execute.
Definition Batch.h:191
int getState(unsigned pos)
Returns the per-message result at the given position.
Definition Batch.cpp:58
std::vector< std::intptr_t > getStatus(unsigned pos)
Returns the detailed error status vector for the given position.
Definition Batch.cpp:73
static constexpr int SUCCESS_NO_INFO
Per-message state value indicating success with no row-count information.
Definition Batch.h:196
unsigned getSize()
Returns the number of messages processed.
Definition Batch.cpp:53
Configuration options for creating a Batch.
Definition Batch.h:85
BatchOptions & setRecordCounts(bool value)
Enables or disables per-message affected row counts.
Definition Batch.h:115
std::optional< unsigned > getBufferBytesSize() const
Returns the batch buffer size in bytes, or nullopt for the server default.
Definition Batch.h:124
BatchOptions & setBlobPolicy(BlobPolicy value)
Sets the blob handling policy.
Definition Batch.h:149
bool getRecordCounts() const
Returns whether per-message affected row counts are reported.
Definition Batch.h:107
unsigned getDetailedErrors() const
Returns the maximum number of detailed error statuses to collect.
Definition Batch.h:158
BatchOptions & setDetailedErrors(unsigned value)
Sets the maximum number of detailed error statuses to collect.
Definition Batch.h:166
BlobPolicy getBlobPolicy() const
Returns the blob handling policy.
Definition Batch.h:141
BatchOptions & setMultiError(bool value)
Enables or disables collection of multiple errors per execution.
Definition Batch.h:98
bool getMultiError() const
Returns whether multiple errors are collected per execution.
Definition Batch.h:90
BatchOptions & setBufferBytesSize(unsigned value)
Sets the batch buffer size in bytes.
Definition Batch.h:132
Wraps the Firebird IBatch interface for bulk DML operations.
Definition Batch.h:275
Batch & operator=(const Batch &)=delete
Copy assignment is not supported.
void close()
Closes the batch handle and releases resources.
Definition Batch.cpp:231
void addBlobStream(std::span< const std::byte > data)
Adds blob data in stream mode (BlobPolicy::STREAM only).
Definition Batch.cpp:182
Batch(const Batch &)=delete
Copy construction is not supported.
bool isValid() const noexcept
Returns whether the batch handle is valid.
Definition Batch.h:332
void appendBlobData(std::span< const std::byte > data)
Appends more data to the last blob added with addBlob().
Definition Batch.cpp:176
void addMessage()
Adds the Statement's current input-message buffer as one message.
Definition Batch.cpp:153
Batch & operator=(Batch &&)=delete
Move assignment is not supported.
void add(unsigned count, const void *inBuffer)
Adds one or more raw messages to the batch buffer.
Definition Batch.cpp:147
const std::vector< Descriptor > & getInputDescriptors()
Returns cached input parameter descriptors for this batch.
Definition Batch.cpp:248
FbRef< fb::IMessageMetadata > getInputMetadata()
Returns the input metadata for this batch.
Definition Batch.cpp:238
BlobId registerBlob(const BlobId &existingBlob)
Registers an existing blob (created via the normal Blob class) for use in the batch,...
Definition Batch.cpp:188
void setDefaultBpb(const BlobOptions &bpb)
Sets the default BPB (Blob Parameter Block) for blobs in this batch.
Definition Batch.cpp:198
BlobId addBlob(std::span< const std::byte > data, const BlobOptions &bpb={})
Adds an inline blob and returns its batch-local ID.
Definition Batch.cpp:163
BatchCompletionState execute()
Executes all queued messages and returns the completion state.
Definition Batch.cpp:215
void cancel()
Cancels the batch, discarding all queued messages.
Definition Batch.cpp:224
unsigned getBlobAlignment()
Returns the blob alignment requirement for this batch.
Definition Batch.cpp:206
~Batch() noexcept
Closes the batch handle if still valid.
Definition Batch.h:313
Represents a Firebird blob identifier.
Definition Blob.h:52
Additional options used when creating or opening blobs.
Definition Blob.h:103
Represents a Firebird client library instance.
Definition Client.h:53
Prepares, executes, and fetches SQL statements against a Firebird attachment.
Definition Statement.h:261
Represents a transaction in one or more Firebird databases.
fb-cpp namespace.
Definition Attachment.h:42
BlobPolicy
Selects the blob handling policy for a Batch.
Definition Batch.h:56
@ ID_ENGINE
Batch-local blob IDs are generated by the Firebird engine.
@ STREAM
Blobs are sent inline as a stream.
@ NONE
Blobs are not allowed in the batch.
@ ID_USER
Batch-local blob IDs are generated by the caller.
std::unique_ptr< T, impl::FbDisposeDeleter > FbUniquePtr
Unique pointer type for Firebird disposable objects.
Definition SmartPtrs.h:53