Use decode_result_raw instead of check

This commit is contained in:
gwenn 2024-11-10 19:28:01 +01:00
parent 79723ef343
commit cd0fc4e4f3
3 changed files with 27 additions and 17 deletions

View File

@ -422,6 +422,14 @@ pub unsafe fn error_from_handle(db: *mut ffi::sqlite3, code: c_int) -> Error {
error_from_sqlite_code(code, message)
}
pub unsafe fn decode_result_raw(db: *mut ffi::sqlite3, code: c_int) -> Result<()> {
if code == ffi::SQLITE_OK {
Ok(())
} else {
Err(error_from_handle(db, code))
}
}
#[cold]
#[cfg(not(feature = "modern_sqlite"))] // SQLite >= 3.38.0
pub unsafe fn error_with_offset(db: *mut ffi::sqlite3, code: c_int, _sql: &str) -> Error {

View File

@ -7,7 +7,7 @@ use std::ptr;
use crate::ffi;
use crate::{error, Connection, DatabaseName, InnerConnection, Result};
use crate::{error::decode_result_raw, Connection, DatabaseName, InnerConnection, Result};
#[cfg(feature = "preupdate_hook")]
pub use preupdate_hook::*;
@ -450,15 +450,24 @@ pub struct Wal {
impl Wal {
/// Checkpoint a database
pub fn checkpoint(&self) -> Result<()> {
error::check(unsafe { ffi::sqlite3_wal_checkpoint(self.db, self.db_name) })
unsafe { decode_result_raw(self.db, ffi::sqlite3_wal_checkpoint(self.db, self.db_name)) }
}
/// Checkpoint a database
pub fn checkpoint_v2(&self, mode: c_int) -> Result<(c_int, c_int)> {
let mut n_log = 0;
let mut n_ckpt = 0;
error::check(unsafe {
ffi::sqlite3_wal_checkpoint_v2(self.db, self.db_name, mode, &mut n_log, &mut n_ckpt)
})?;
unsafe {
decode_result_raw(
self.db,
ffi::sqlite3_wal_checkpoint_v2(
self.db,
self.db_name,
mode,
&mut n_log,
&mut n_ckpt,
),
)?
};
Ok((n_log, n_ckpt))
}

View File

@ -9,7 +9,9 @@ use std::sync::{Arc, Mutex};
use super::ffi;
use super::str_for_sqlite;
use super::{Connection, InterruptHandle, OpenFlags, PrepFlags, Result};
use crate::error::{error_from_handle, error_from_sqlite_code, error_with_offset, Error};
use crate::error::{
decode_result_raw, error_from_handle, error_from_sqlite_code, error_with_offset, Error,
};
use crate::raw_statement::RawStatement;
use crate::statement::Statement;
use crate::version_number;
@ -134,16 +136,7 @@ impl InnerConnection {
#[inline]
pub fn decode_result(&self, code: c_int) -> Result<()> {
unsafe { Self::decode_result_raw(self.db(), code) }
}
#[inline]
unsafe fn decode_result_raw(db: *mut ffi::sqlite3, code: c_int) -> Result<()> {
if code == ffi::SQLITE_OK {
Ok(())
} else {
Err(error_from_handle(db, code))
}
unsafe { decode_result_raw(self.db(), code) }
}
pub fn close(&mut self) -> Result<()> {
@ -165,7 +158,7 @@ impl InnerConnection {
let r = ffi::sqlite3_close(self.db);
// Need to use _raw because _guard has a reference out, and
// decode_result takes &mut self.
let r = Self::decode_result_raw(self.db, r);
let r = decode_result_raw(self.db, r);
if r.is_ok() {
*shared_handle = ptr::null_mut();
self.db = ptr::null_mut();