Add Void::Sqlite — RAII SQLite wrapper with migrations

New SHARED module (separate from Data so Json/Csv/Ini consumers don't link
SQLite). Generic, Result-based:
- Database — open (file or :memory:), exec, prepare, begin, last_insert_rowid,
  changes; enables WAL + busy timeout + foreign_keys for writable modes.
- Statement — move-only prepared statement; 1-based typed bind (int/double/
  text/null/optional), step(), reset, 0-based column accessors.
- Transaction — RAII; rolls back on scope exit unless commit() is called.
- Migration + Database::migrate() — forward, in-order, idempotent schema
  upgrades tracked via PRAGMA user_version.

Dependencies.cmake resolves SQLite::SQLite3 via find_package. Tests exercise
CRUD, null handling, commit/rollback, migrations and errors on :memory:.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-22 16:24:18 +03:00
1 parent c92dff6ff7
commit cb639a713d
5 files changed
+579

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
#pragma once
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <cstdint>
#include <filesystem>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <utility>
struct sqlite3;
struct sqlite3_stmt;
// A thin, RAII, Result-based wrapper over the SQLite C API. Generic — no
// application schema baggage. Open a Database, prepare Statements, bind by
// 1-based index, step over result rows, read columns by 0-based index. Schema
// versioning is handled via PRAGMA user_version (migrate()).
namespace Void::Sqlite
{
using Void::Result::Error;
template <typename T> using Result = Void::Result::Result<T, Error>;
enum class OpenMode { ReadOnly, ReadWrite, ReadWriteCreate };
class Database;
/// A prepared statement. Move-only; finalizes on destruction.
class Statement
{
public:
Statement() = default;
~Statement();
Statement(Statement&&) noexcept;
Statement& operator=(Statement&&) noexcept;
Statement(const Statement&) = delete;
Statement& operator=(const Statement&) = delete;
// Bind parameters (1-based, matching SQLite). Chainable.
Statement& bind(int index, std::int64_t value);
Statement& bind(int index, int value)
{
return bind(index, static_cast<std::int64_t>(value));
}
Statement& bind(int index, double value);
Statement& bind(int index, std::string_view value);
Statement& bind(int index, std::nullptr_t);
Statement& bind(int index, const std::optional<std::string>& value);
/// Advance to the next row. Returns true with a row available,
/// false when the statement is done, or an Error on failure.
[[nodiscard]] Result<bool> step();
/// Reset for re-execution (keeps bindings unless clear_bindings).
void reset() noexcept;
void clear_bindings() noexcept;
// Column accessors (0-based), valid after step() yields a row.
[[nodiscard]] int column_count() const noexcept;
[[nodiscard]] bool is_null(int col) const noexcept;
[[nodiscard]] std::int64_t column_int(int col) const noexcept;
[[nodiscard]] double column_double(int col) const noexcept;
[[nodiscard]] std::string column_text(int col) const;
private:
friend class Database;
explicit Statement(sqlite3_stmt* stmt) : m_stmt(stmt) {}
sqlite3_stmt* m_stmt = nullptr;
};
/// RAII transaction. Rolls back on destruction unless commit() was called.
class Transaction
{
public:
~Transaction();
Transaction(Transaction&&) noexcept;
Transaction& operator=(Transaction&&) noexcept;
Transaction(const Transaction&) = delete;
Transaction& operator=(const Transaction&) = delete;
Result<void> commit();
void rollback() noexcept;
private:
friend class Database;
explicit Transaction(Database* db) : m_db(db) {}
Database* m_db = nullptr;
bool m_done = false;
};
/// A single forward schema migration applied when the database's
/// user_version is below `version`.
struct Migration {
int version; ///< target schema version (1, 2, 3, ...)
std::string_view sql; ///< DDL/DML to reach this version
};
class Database
{
public:
Database() = default;
~Database();
Database(Database&&) noexcept;
Database& operator=(Database&&) noexcept;
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
/// Open (or create) a database file. Use ":memory:" for an
/// in-memory db. Enables WAL + a busy timeout for ReadWrite modes.
[[nodiscard]] static Result<Database>
open(const std::filesystem::path& path,
OpenMode mode = OpenMode::ReadWriteCreate);
/// Execute one or more semicolon-separated statements (no results).
Result<void> exec(std::string_view sql);
/// Prepare a single statement for binding/stepping.
[[nodiscard]] Result<Statement> prepare(std::string_view sql);
/// Begin a transaction (BEGIN; commit/rollback via the handle).
[[nodiscard]] Result<Transaction> begin();
[[nodiscard]] std::int64_t last_insert_rowid() const noexcept;
[[nodiscard]] int changes() const noexcept;
/// Current schema version (PRAGMA user_version).
[[nodiscard]] Result<int> user_version();
/// Apply, in order, any migrations whose version exceeds the
/// current user_version, each inside a transaction, then bump
/// user_version. Idempotent and safe to call on every startup.
Result<void> migrate(std::span<const Migration> migrations);
[[nodiscard]] sqlite3* handle() const noexcept { return m_db; }
private:
sqlite3* m_db = nullptr;
};
} // namespace Void::Sqlite