Sqlite: expose native primary/extended result codes structurally (VL-SQLITE-01)
This commit is contained in:
1 parent
a29c9475f2
commit
0e8ee1848d
2 files changed
+97
-1
No files matched your search
@@ -11,6 +11,7 @@
|
|||||||
#include <span>
|
#include <span>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <system_error>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -91,6 +92,56 @@ namespace Void::Sqlite
|
|||||||
sqlite_error = 24
|
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<SqliteNativeCode>
|
||||||
|
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(
|
[[nodiscard]] inline Error make_sqlite_error(
|
||||||
SqliteErrorCode code,
|
SqliteErrorCode code,
|
||||||
std::string message,
|
std::string message,
|
||||||
@@ -161,11 +212,19 @@ namespace Void::Sqlite
|
|||||||
message += ")";
|
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{
|
return Error{
|
||||||
kind,
|
kind,
|
||||||
std::move(message),
|
std::move(message),
|
||||||
ErrorCode{Void::Result::kSqliteErrorDomain,
|
ErrorCode{Void::Result::kSqliteErrorDomain,
|
||||||
static_cast<std::int32_t>(code)}};
|
static_cast<std::int32_t>(code)},
|
||||||
|
native};
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SqliteLimits {
|
struct SqliteLimits {
|
||||||
|
|||||||
@@ -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::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());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user