mirror of
https://github.com/isar/rusqlite.git
synced 2025-03-31 19:12:58 +08:00
Improve Connection::extension_init2
This commit is contained in:
parent
79fae3d93c
commit
718599d5f7
@ -20,21 +20,15 @@ use rusqlite::{to_sqlite_error, Connection, Result};
|
|||||||
/// ```
|
/// ```
|
||||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn sqlite3_extension_init(
|
pub unsafe extern "C" fn sqlite3_extension_init(
|
||||||
db: *mut ffi::sqlite3,
|
db: *mut ffi::sqlite3,
|
||||||
pz_err_msg: *mut *mut c_char,
|
pz_err_msg: *mut *mut c_char,
|
||||||
p_api: *mut ffi::sqlite3_api_routines,
|
p_api: *mut ffi::sqlite3_api_routines,
|
||||||
) -> c_int {
|
) -> c_int {
|
||||||
if p_api.is_null() {
|
Connection::extension_init2(db, pz_err_msg, p_api, extension_init)
|
||||||
return ffi::SQLITE_ERROR;
|
|
||||||
} else if let Err(err) = extension_init(db, p_api) {
|
|
||||||
return unsafe { to_sqlite_error(&err, pz_err_msg) };
|
|
||||||
}
|
|
||||||
ffi::SQLITE_OK
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extension_init(db: *mut ffi::sqlite3, p_api: *mut ffi::sqlite3_api_routines) -> Result<()> {
|
fn extension_init(db: Connection) -> Result<bool> {
|
||||||
let db = unsafe { Connection::extension_init2(db, p_api)? };
|
|
||||||
db.create_scalar_function(
|
db.create_scalar_function(
|
||||||
"rusqlite_test_function",
|
"rusqlite_test_function",
|
||||||
0,
|
0,
|
||||||
@ -46,5 +40,5 @@ fn extension_init(db: *mut ffi::sqlite3, p_api: *mut ffi::sqlite3_api_routines)
|
|||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
rusqlite::trace::log(ffi::SQLITE_WARNING, "Rusqlite extension initialized");
|
rusqlite::trace::log(ffi::SQLITE_WARNING, "Rusqlite extension initialized");
|
||||||
Ok(())
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
24
src/lib.rs
24
src/lib.rs
@ -952,15 +952,31 @@ impl Connection {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like SQLITE_EXTENSION_INIT2 macro
|
/// Helper to register an SQLite extension written in Rust.
|
||||||
|
/// For [persistent](https://sqlite.org/loadext.html#persistent_loadable_extensions) extension,
|
||||||
|
/// `init` should returns `Ok(true)`.
|
||||||
|
/// # Safety
|
||||||
|
/// * Results are undefined if `init` does not just register features.
|
||||||
#[cfg(feature = "loadable_extension")]
|
#[cfg(feature = "loadable_extension")]
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "loadable_extension")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "loadable_extension")))]
|
||||||
pub unsafe fn extension_init2(
|
pub unsafe fn extension_init2(
|
||||||
db: *mut ffi::sqlite3,
|
db: *mut ffi::sqlite3,
|
||||||
|
pz_err_msg: *mut *mut c_char,
|
||||||
p_api: *mut ffi::sqlite3_api_routines,
|
p_api: *mut ffi::sqlite3_api_routines,
|
||||||
) -> Result<Connection> {
|
init: fn(Connection) -> Result<bool>,
|
||||||
ffi::rusqlite_extension_init2(p_api)?;
|
) -> c_int {
|
||||||
Connection::from_handle(db)
|
if p_api.is_null() {
|
||||||
|
return ffi::SQLITE_ERROR;
|
||||||
|
}
|
||||||
|
match ffi::rusqlite_extension_init2(p_api)
|
||||||
|
.map_err(Error::from)
|
||||||
|
.and(Connection::from_handle(db))
|
||||||
|
.and_then(init)
|
||||||
|
{
|
||||||
|
Err(err) => to_sqlite_error(&err, pz_err_msg),
|
||||||
|
Ok(true) => ffi::SQLITE_OK_LOAD_PERMANENTLY,
|
||||||
|
_ => ffi::SQLITE_OK,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `Connection` from a raw owned handle.
|
/// Create a `Connection` from a raw owned handle.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user