From 0e8ee1848d344cf788a0ffb5f508afbd826b2dfe Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Thu, 3 Dec 2026 18:15:26 +0300 Subject: [PATCH] Sqlite: expose native primary/extended result codes structurally (VL-SQLITE-01) --- include/Void/Sqlite/Types.hpp | 61 ++++++++++++++++++++- tests/standalone/Sqlite/SqliteHardening.cpp | 37 +++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/include/Void/Sqlite/Types.hpp b/include/Void/Sqlite/Types.hpp index 92bb767..37a85ba 100644 --- a/include/Void/Sqlite/Types.hpp +++ b/include/Void/Sqlite/Types.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -91,6 +92,56 @@ namespace Void::Sqlite sqlite_error = 24 }; + namespace Detail + { + /// std::error_category carrying SQLite's native primary/extended result + /// codes without widening Void::Result::ErrorCode. + class SqliteNativeErrorCategory final : public std::error_category + { + public: + + [[nodiscard]] const char* name() const noexcept override + { + return "void.sqlite.native"; + } + + [[nodiscard]] std::string message(int code) const override + { + return "sqlite native result code " + std::to_string(code); + } + }; + } // namespace Detail + + /// The SQLite error category used for native primary/extended codes. + [[nodiscard]] inline const std::error_category& + sqlite_native_error_category() noexcept + { + static const Detail::SqliteNativeErrorCategory category{}; + return category; + } + + /// SQLite's native result code split into its primary and extended parts. + struct SqliteNativeCode { + int primary{0}; + int extended{0}; + + [[nodiscard]] bool operator==(const SqliteNativeCode&) const = default; + }; + + /// Recover the native SQLite codes from an error, if it came from SQLite. + /// The extended code keeps the fine-grained reason (for example UNIQUE vs + /// FOREIGN KEY constraint failures, or BUSY vs LOCKED). + [[nodiscard]] inline std::optional + sqlite_native_code(const Error& error) noexcept + { + const std::error_code system = error.system_code(); + if (system.category() != sqlite_native_error_category()) { + return std::nullopt; + } + const int extended = system.value(); + return SqliteNativeCode{extended & 0xff, extended}; + } + [[nodiscard]] inline Error make_sqlite_error( SqliteErrorCode code, std::string message, @@ -161,11 +212,19 @@ namespace Void::Sqlite message += ")"; } + std::error_code native{}; + if (primary_code != 0 || extended_code != 0) { + native = std::error_code{ + extended_code != 0 ? extended_code : primary_code, + sqlite_native_error_category()}; + } + return Error{ kind, std::move(message), ErrorCode{Void::Result::kSqliteErrorDomain, - static_cast(code)}}; + static_cast(code)}, + native}; } struct SqliteLimits { diff --git a/tests/standalone/Sqlite/SqliteHardening.cpp b/tests/standalone/Sqlite/SqliteHardening.cpp index 8d48acd..a892b73 100644 --- a/tests/standalone/Sqlite/SqliteHardening.cpp +++ b/tests/standalone/Sqlite/SqliteHardening.cpp @@ -637,3 +637,40 @@ Void::Testing::Test sqlite_transaction_reports_automatic_rollback() Void::Testing::require(db.exec("PRAGMA max_page_count=1073741823").has_value()); } } + +Void::Testing::Test sqlite_native_result_codes_are_structural() +{ + TempSqliteDir dir; + const auto db_path = dir.path / "native.db"; + auto db_res = open_database(db_path); + Void::Testing::require(db_res.has_value()); + auto db = std::move(*db_res); + + Void::Testing::require( + db.exec("CREATE TABLE u(id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL)") + .has_value()); + Void::Testing::require( + db.exec("INSERT INTO u(id, email) VALUES(1, 'a@example.com')").has_value()); + + // A UNIQUE violation exposes the fine-grained extended code structurally. + auto duplicate = db.exec("INSERT INTO u(id, email) VALUES(2, 'a@example.com')"); + Void::Testing::require(!duplicate.has_value()); + auto native = sqlite_native_code(duplicate.error()); + Void::Testing::require(native.has_value()); + Void::Testing::expect_eq(native->primary, SQLITE_CONSTRAINT); + Void::Testing::expect_eq(native->extended, SQLITE_CONSTRAINT_UNIQUE); + + // Same primary code, different extended reason. + auto null_email = db.exec("INSERT INTO u(id, email) VALUES(3, NULL)"); + Void::Testing::require(!null_email.has_value()); + auto null_native = sqlite_native_code(null_email.error()); + Void::Testing::require(null_native.has_value()); + Void::Testing::expect_eq(null_native->primary, SQLITE_CONSTRAINT); + Void::Testing::expect_eq(null_native->extended, SQLITE_CONSTRAINT_NOTNULL); + Void::Testing::expect(null_native->extended != native->extended); + + // Validation errors carry no native SQLite code. + auto invalid = db.prepare("SELECT 1; SELECT 2"); + Void::Testing::require(!invalid.has_value()); + Void::Testing::expect(!sqlite_native_code(invalid.error()).has_value()); +}