Files
faumarayandClaude Opus 4.8 cb639a713d 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>
2026-06-22 16:24:18 +03:00

139 lines
3.9 KiB
C++

#include <Void/Sqlite/Sqlite.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Suite.hpp>
#include <array>
#include <string>
namespace
{
using Void::Sqlite::Database;
using Void::Sqlite::Migration;
using Void::Testing::Test;
[[nodiscard]] Database fresh()
{
auto db = Database::open(":memory:");
Void::Testing::expect(db.has_value());
return std::move(*db);
}
struct SqliteTests : Void::Testing::Suite<SqliteTests> {
Test open_in_memory()
{
auto db = Database::open(":memory:");
Void::Testing::expect(db.has_value());
}
Test exec_create_and_insert()
{
auto db = fresh();
Void::Testing::expect(
db.exec("CREATE TABLE t(id INTEGER PRIMARY KEY, "
"name TEXT, score REAL)")
.has_value());
Void::Testing::expect(
db.exec("INSERT INTO t(name,score) VALUES('a',1.5)")
.has_value());
Void::Testing::expect_eq(db.last_insert_rowid(),
static_cast<std::int64_t>(1));
}
Test prepared_bind_and_read()
{
auto db = fresh();
(void)db.exec("CREATE TABLE t(id INTEGER, name TEXT, s REAL)");
auto ins = db.prepare(
"INSERT INTO t(id,name,s) VALUES(?,?,?)");
Void::Testing::expect(ins.has_value());
ins->bind(1, 7).bind(2, std::string_view{"hello"}).bind(3, 2.5);
const auto done = ins->step();
Void::Testing::expect(done.has_value() && *done == false);
auto sel = db.prepare("SELECT id,name,s FROM t WHERE id=?");
Void::Testing::expect(sel.has_value());
sel->bind(1, 7);
const auto row = sel->step();
Void::Testing::expect(row.has_value() && *row == true);
Void::Testing::expect_eq(sel->column_int(0),
static_cast<std::int64_t>(7));
Void::Testing::expect_eq(sel->column_text(1),
std::string{"hello"});
Void::Testing::expect(sel->column_double(2) == 2.5);
const auto end = sel->step();
Void::Testing::expect(end.has_value() && *end == false);
}
Test null_handling()
{
auto db = fresh();
(void)db.exec("CREATE TABLE t(v TEXT)");
auto ins = db.prepare("INSERT INTO t(v) VALUES(?)");
ins->bind(1, nullptr);
(void)ins->step();
auto sel = db.prepare("SELECT v FROM t");
(void)sel->step();
Void::Testing::expect(sel->is_null(0));
}
Test transaction_commit_persists()
{
auto db = fresh();
(void)db.exec("CREATE TABLE t(n INTEGER)");
{
auto tx = db.begin();
Void::Testing::expect(tx.has_value());
(void)db.exec("INSERT INTO t VALUES(1)");
(void)db.exec("INSERT INTO t VALUES(2)");
Void::Testing::expect(tx->commit().has_value());
}
auto sel = db.prepare("SELECT COUNT(*) FROM t");
(void)sel->step();
Void::Testing::expect_eq(sel->column_int(0),
static_cast<std::int64_t>(2));
}
Test transaction_rollback_on_scope_exit()
{
auto db = fresh();
(void)db.exec("CREATE TABLE t(n INTEGER)");
{
auto tx = db.begin();
(void)db.exec("INSERT INTO t VALUES(99)");
// no commit -> rolled back on destruction
}
auto sel = db.prepare("SELECT COUNT(*) FROM t");
(void)sel->step();
Void::Testing::expect_eq(sel->column_int(0),
static_cast<std::int64_t>(0));
}
Test migrations_apply_in_order_and_are_idempotent()
{
auto db = fresh();
const std::array<Migration, 2> migs{{
{1, "CREATE TABLE a(id INTEGER)"},
{2, "ALTER TABLE a ADD COLUMN name TEXT"},
}};
Void::Testing::expect(db.migrate(migs).has_value());
Void::Testing::expect_eq(*db.user_version(), 2);
// Second call is a no-op (idempotent) and must not error.
Void::Testing::expect(db.migrate(migs).has_value());
Void::Testing::expect_eq(*db.user_version(), 2);
// The v2 column exists.
Void::Testing::expect(
db.exec("INSERT INTO a(id,name) VALUES(1,'x')")
.has_value());
}
Test prepare_bad_sql_errors()
{
auto db = fresh();
Void::Testing::expect(!db.prepare("SELECT FROM").has_value());
}
};
} // namespace