Disambiguates std::string arguments to Statement::bind, which otherwise match both bind(string_view) and bind(optional<string>) and are ambiguous. Delegates to the string_view overload. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
155 lines
4.7 KiB
C++
155 lines
4.7 KiB
C++
#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);
|
|
// Disambiguates std::string args (which otherwise match both
|
|
// string_view and optional<string>).
|
|
Statement& bind(int index, const std::string& value)
|
|
{
|
|
return bind(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
|