mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-19 08:39:44 +00:00
feat(Sqlite): add LockedSqliteHandle::last_error (#3707)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user