BulkInserter Class
(CDP2SQL::BulkInserter)Class for inserting many rows efficiently using database-specific implementations. More...
| Header: | #include <BulkInserter> |
| Inherits: | HasDatabaseConnection |
Public Functions
| BulkInserter() | |
| BulkInserter(Database &db, const std::string &table, const std::vector<std::string> &columns) | |
| BulkInserter(const BulkInserter &) = delete | |
| ~BulkInserter() override | |
| void | AddRow(const std::vector<Utils::Variant> &values) |
| void | Cancel() |
| void | Commit() |
| bool | IsOpen() const |
| void | Open(Database &db, const std::string &table, const std::vector<std::string> &columns) |
| void | SetColumnTypeHint(std::size_t column, IDatabaseBulkInserter::TypeHint hint) |
- 2 public functions inherited from CDP2SQL::HasDatabaseConnection
Detailed Description
Class for inserting many rows efficiently using database-specific implementations.
A common approach for repeated inserts is to use CDP2SQL::Statement with placeholders and bind values for each row. BulkInserter provides a similar workflow, but is specialized for adding many rows efficiently and committing them in one go.
When the underlying database supports a specialized bulk insert path, it will be used automatically for best performance (for example, a PostgreSQL backend may use COPY FROM). If not, BulkInserter falls back to a portable implementation based on a prepared INSERT statement inside a transaction.
The transaction is lazily started on the first AddRow call and committed when Commit is called or aborted when Cancel is called or when the BulkInserter is destroyed without committing.
Column types are usually inferred from the Utils::Variant values passed to AddRow, and in most cases, no extra configuration is required. Use SetColumnTypeHint to provide an explicit CDP2SQL::IDatabaseBulkInserter::TypeHint when inference is insufficient or when a backend benefits from knowing the intended representation (for example, forcing a value to be treated as a BLOB).
A Utils::Variant with type Utils::eUNDEFINED (or !isValid()) is treated as SQL NULL.
Note: While a bulk inserter transaction is active, some backends may not allow executing other SQL on the same connection.
Example usage:
#include <CDP2SQL/CDP2SQL.h> #include <CppSQLite3/SQLite3Factory.h> using namespace CDP2SQL; ... try { Database db(SQLite3Factory().Create(), "my.db"); db.Execute("CREATE TABLE IF NOT EXISTS my_table(id INTEGER, payload BLOB, note TEXT);"); BulkInserter inserter(db, "my_table", {"id", "payload", "note"}); inserter.SetColumnTypeHint(1, IDatabaseBulkInserter::TypeHint::BLOB); std::string payload; payload.push_back('\0'); payload.push_back('A'); payload.push_back('\xff'); std::vector<Utils::Variant> row; row.emplace_back(Utils::Variant(1)); row.emplace_back(Utils::Variant(payload)); row.emplace_back(Utils::Variant(std::string("some text"))); inserter.AddRow(row); inserter.Commit(); } catch (const SQLException& e) { printf("Error occurred: %s\n", e.Message()); }
If Commit is not called, the destructor calls Cancel to roll back/cancel any pending work.
Member Function Documentation
BulkInserter::BulkInserter()
The default constructor. Use this if you need to delay calling Open.
BulkInserter::BulkInserter(Database &db, const std::string &table, const std::vector<std::string> &columns)
Constructs and opens a bulk inserter.
Equivalent to default-constructing and then calling Open.
[delete] BulkInserter::BulkInserter(const BulkInserter &)
Copy constructor.
BulkInserter::~BulkInserter()
Destructor. Calls Cancel to ensure that any pending work is rolled back/canceled.
void BulkInserter::AddRow(const std::vector<Utils::Variant> &values)
Adds a single row to the current batch. The number of values must match the number of columns passed when creating the inserter.
A Utils::Variant with type Utils::eUNDEFINED (or !isValid()) is treated as SQL NULL.
Throws CDP2SQL::SQLException on errors (e.g. mismatched row size, conversion errors, driver errors).
void BulkInserter::Cancel()
Aborts and discards the current batch.
After Cancel returns, the inserter is ready to accept a new batch via AddRow.
void BulkInserter::Commit()
Finalizes and flushes the current batch.
After Commit returns, the inserter is ready to accept a new batch via AddRow.
bool BulkInserter::IsOpen() const
Returns whether this object has been opened successfully.
void BulkInserter::Open(Database &db, const std::string &table, const std::vector<std::string> &columns)
Opens the bulk inserter for a specific table and columns.
BulkInserter will use an optimized backend-specific bulk insert implementation when available (e.g. COPY FROM for PostgreSQL); otherwise it uses a prepared INSERT statement inside a transaction as a portable fallback.
Parameters:
- db - Connected database instance.
- table - Target table name.
- columns - Column names in the same order as later values are passed to AddRow.
void BulkInserter::SetColumnTypeHint(std::size_t column, IDatabaseBulkInserter::TypeHint hint)
Sets an optional type hint for a column.
The column is the 0-based column index within the columns list used to create the inserter.
The hint is an implementation-defined integer value; callers should use values from CDP2SQL::IDatabaseBulkInserter::TypeHint for portable hints, e.g. TypeHint::BLOB to indicate that the column should be treated as a binary large object.
Backend-specific/custom hints may use integer values greater than 100.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.