Lightweight 0.20251202.0
Loading...
Searching...
No Matches
BatchManager.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include "../Api.hpp"
5#include "../DataBinder/SqlRawColumn.hpp" // For SqlRawColumn
6#include "../SqlSchema.hpp"
7#include "../SqlServerType.hpp"
8#include "SqlBackupFormats.hpp"
9
10#include <functional>
11#include <memory>
12#include <vector>
13
14namespace Lightweight::detail
15{
16
17struct BatchColumn;
18
19#if defined(_MSC_VER)
20#pragma warning(push)
21#pragma warning(disable: 4251) // STL types in DLL interface
22#endif
23
24/// A batch manager that manages multiple batch columns for a statement.
25struct LIGHTWEIGHT_API BatchManager
26{
27 using BatchExecutor = std::function<void(std::vector<SqlRawColumn> const&, size_t)>;
28
29 /// Create a new BatchManager for the given statement and column declarations.
30 ///
31 /// @param executor The callback to execute the batch.
32 /// @param colDecls The column declarations for the statement.
33 /// @param capacity The capacity of the batch manager.
34 /// @param serverType The server type for database-specific bindings.
35 explicit BatchManager(BatchExecutor executor,
36 std::vector<SqlColumnDeclaration> const& colDecls,
37 size_t capacity,
38 SqlServerType serverType = SqlServerType::UNKNOWN);
39
40 ~BatchManager();
41
42 BatchManager(BatchManager const&) = delete;
43 BatchManager& operator=(BatchManager const&) = delete;
44 BatchManager(BatchManager&&) noexcept;
45 BatchManager& operator=(BatchManager&&) noexcept;
46
47 /// Push a row to the batch manager.
48 void PushRow(std::vector<SqlBackup::BackupValue> const& row);
49
50 /// Push a batch of data (from a chunk) to the batch manager.
51 void PushBatch(SqlBackup::ColumnBatch const& batch);
52
53 /// Flush the current batch to the database.
54 void Flush();
55
56 // private:
57 [[nodiscard]] std::unique_ptr<BatchColumn> CreateColumn(SqlColumnDeclaration const& col) const;
58
59 size_t rowCount = 0;
60 size_t capacity = 1000;
61 BatchExecutor executor;
62 std::vector<std::unique_ptr<BatchColumn>> columns;
63 SqlServerType serverType = SqlServerType::UNKNOWN;
64};
65
66#if defined(_MSC_VER)
67#pragma warning(pop)
68#endif
69
70} // namespace Lightweight::detail