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 <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
@@ -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<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(
|
||||
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<std::int32_t>(code)}};
|
||||
static_cast<std::int32_t>(code)},
|
||||
native};
|
||||
}
|
||||
|
||||
struct SqliteLimits {
|
||||
|
||||
Reference in new issue
Block a user