Creates a named SAVEPOINT on a connection and releases or rolls it back automatically.
It also integrates with sqlite::snapshot so callers can capture and re-open
snapshots inside nested transactions.
Use cases
- Acts like a “mini transaction” that can be nested under a parent
sqlite::transaction. - Great for retrying a chunk of work inside long-running migrations—rollback on the savepoint without aborting the entire outer transaction.
- Expose the current name via
getName()so you can correlate logs with the SQL emitted inside the guarded scope.
Example usage
sqlite::transaction outer{db};
sqlite::savepoint block{db, "apply_patch"};
try {
run_patch(block.getName());
block.release(); // optional; destructor releases automatically
outer.commit();
} catch (...) {
block.rollback(); // rewinds partial changes
throw;
}
Pair a savepoint with a snapshot when you need a
consistent view before touching the data; it keeps long migrations from surprising the
writers.
Public API
| Member | Description |
|---|---|
savepoint(connection& con, std::string const& name) | Issues SAVEPOINT name. |
~savepoint() | Automatically releases the savepoint if still active. |
void release() | Runs RELEASE SAVEPOINT manually. |
void rollback() | Runs ROLLBACK TO SAVEPOINT. |
bool isActive() const | Returns whether the savepoint has not yet been released/rolled back. |
std::string getName() const | Returns the alias provided at construction. |
snapshot take_snapshot(std::string_view schema = "main") | Captures a WAL snapshot inside the savepoint. |
void open_snapshot(snapshot const& snap, std::string_view schema = "main") | Rewinds the connection's read transaction to snap. |