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-cpp-export.h"
29#include "fb-api.h"
30#include "Blob.h"
31#include "Descriptor.h"
32#include "SmartPtrs.h"
33#include "Exception.h"
34#include <cassert>
35#include <cstddef>
36#include <cstdint>
37#include <optional>
38#include <span>
39#include <string_view>
40#include <vector>
41
42
46namespace fbcpp
47{
48 class Attachment;
49 class Client;
50 class Statement;
51 class Transaction;
52
56 enum class BlobPolicy : std::uint8_t
57 {
61 NONE = fb::IBatch::BLOB_NONE,
62
66 ID_ENGINE = fb::IBatch::BLOB_ID_ENGINE,
67
71 ID_USER = fb::IBatch::BLOB_ID_USER,
72
76 STREAM = fb::IBatch::BLOB_STREAM,
77 };
78
85 class BatchOptions final
86 {
87 public:
91 bool getMultiError() const
92 {
93 return multiError;
94 }
95
100 {
101 multiError = value;
102 return *this;
103 }
104
108 bool getRecordCounts() const
109 {
110 return recordCounts;
111 }
112
117 {
118 recordCounts = value;
119 return *this;
120 }
121
125 std::optional<unsigned> getBufferBytesSize() const
126 {
127 return bufferBytesSize;
128 }
129
134 {
135 bufferBytesSize = value;
136 return *this;
137 }
138
143 {
144 return blobPolicy;
145 }
146
151 {
152 blobPolicy = value;
153 return *this;
154 }
155
159 unsigned getDetailedErrors() const
160 {
161 return detailedErrors;
162 }
163
168 {
169 detailedErrors = value;
170 return *this;
171 }
172
173 private:
174 bool multiError = false;
175 bool recordCounts = false;
176 std::optional<unsigned> bufferBytesSize;
177 BlobPolicy blobPolicy = BlobPolicy::NONE;
178 unsigned detailedErrors = 64;
179 };
180
186 class FB_CPP_EXPORT BatchCompletionState final
187 {
188 public:
192 static constexpr int EXECUTE_FAILED = fb::IBatchCompletionState::EXECUTE_FAILED;
193
197 static constexpr int SUCCESS_NO_INFO = fb::IBatchCompletionState::SUCCESS_NO_INFO;
198
199 public:
203 explicit BatchCompletionState(Client& client, FbUniquePtr<fb::IBatchCompletionState> handle) noexcept;
204
209
214
219
224
225 public:
229 unsigned getSize();
230
236 int getState(unsigned pos);
237
243 std::optional<unsigned> findError(unsigned pos);
244
250 std::vector<std::intptr_t> getStatus(unsigned pos);
251
252 private:
253 Client* client;
254 impl::StatusWrapper statusWrapper;
256 };
257
274 class FB_CPP_EXPORT 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(const BatchOptions& options);
443 std::vector<std::uint8_t> prepareBpb(const BlobOptions& bpb);
444 void buildInputDescriptors();
445
446 private:
447 Client* client;
448 Transaction* transaction;
449 Statement* statement = nullptr;
450 impl::StatusWrapper statusWrapper;
451 FbRef<fb::IBatch> handle;
452 std::vector<Descriptor> inputDescriptors;
453 };
454} // namespace fbcpp
455
456
457#endif // FBCPP_BATCH_H
Represents a connection to a Firebird database.
Definition Attachment.h:219
Wraps IBatchCompletionState to provide RAII-safe access to batch execution results.
Definition Batch.h:187
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.
Configuration options for creating a Batch.
Definition Batch.h:86
BatchOptions & setRecordCounts(bool value)
Enables or disables per-message affected row counts.
Definition Batch.h:116
std::optional< unsigned > getBufferBytesSize() const
Returns the batch buffer size in bytes, or nullopt for the server default.
Definition Batch.h:125
BatchOptions & setBlobPolicy(BlobPolicy value)
Sets the blob handling policy.
Definition Batch.h:150
bool getRecordCounts() const
Returns whether per-message affected row counts are reported.
Definition Batch.h:108
unsigned getDetailedErrors() const
Returns the maximum number of detailed error statuses to collect.
Definition Batch.h:159
BatchOptions & setDetailedErrors(unsigned value)
Sets the maximum number of detailed error statuses to collect.
Definition Batch.h:167
BlobPolicy getBlobPolicy() const
Returns the blob handling policy.
Definition Batch.h:142
BatchOptions & setMultiError(bool value)
Enables or disables collection of multiple errors per execution.
Definition Batch.h:99
bool getMultiError() const
Returns whether multiple errors are collected per execution.
Definition Batch.h:91
BatchOptions & setBufferBytesSize(unsigned value)
Sets the batch buffer size in bytes.
Definition Batch.h:133
Wraps the Firebird IBatch interface for bulk DML operations.
Definition Batch.h:275
Batch & operator=(const Batch &)=delete
Copy assignment is not supported.
Batch(const Batch &)=delete
Copy construction is not supported.
bool isValid() const noexcept
Returns whether the batch handle is valid.
Definition Batch.h:332
Batch & operator=(Batch &&)=delete
Move assignment is not supported.
~Batch() noexcept
Closes the batch handle if still valid.
Definition Batch.h:313
Represents a Firebird blob identifier.
Definition Blob.h:53
Additional options used when creating or opening blobs.
Definition Blob.h:104
Represents a Firebird client library instance.
Definition Client.h:54
Prepares, executes, and fetches SQL statements against a Firebird attachment.
Definition Statement.h:143
Represents a transaction in one or more Firebird databases.
fb-cpp namespace.
Definition Attachment.h:46
BlobPolicy
Selects the blob handling policy for a Batch.
Definition Batch.h:57
@ 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