feat(Sqlite): add LockedSqliteHandle::last_error (#3707)

This commit is contained in:
joeydewaal
2025-01-28 05:56:21 +01:00
committed by GitHub
parent 6ca52fe80c
commit 546ec960a9
4 changed files with 36 additions and 3 deletions

View File

@@ -28,7 +28,7 @@ use crate::connection::establish::EstablishParams;
use crate::connection::worker::ConnectionWorker;
use crate::options::OptimizeOnClose;
use crate::statement::VirtualStatement;
use crate::{Sqlite, SqliteConnectOptions};
use crate::{Sqlite, SqliteConnectOptions, SqliteError};
pub(crate) mod collation;
pub(crate) mod describe;
@@ -542,6 +542,10 @@ impl LockedSqliteHandle<'_> {
pub fn remove_rollback_hook(&mut self) {
self.guard.remove_rollback_hook();
}
pub fn last_error(&mut self) -> Option<SqliteError> {
SqliteError::try_new(self.guard.handle.as_ptr())
}
}
impl Drop for ConnectionState {

View File

@@ -23,9 +23,17 @@ pub struct SqliteError {
impl SqliteError {
pub(crate) fn new(handle: *mut sqlite3) -> Self {
Self::try_new(handle).expect("There should be an error")
}
pub(crate) fn try_new(handle: *mut sqlite3) -> Option<Self> {
// returns the extended result code even when extended result codes are disabled
let code: c_int = unsafe { sqlite3_extended_errcode(handle) };
if code == 0 {
return None;
}
// return English-language text that describes the error
let message = unsafe {
let msg = sqlite3_errmsg(handle);
@@ -34,10 +42,10 @@ impl SqliteError {
from_utf8_unchecked(CStr::from_ptr(msg).to_bytes())
};
Self {
Some(Self {
code,
message: message.to_owned(),
}
})
}
/// For errors during extension load, the error message is supplied via a separate pointer