Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify loadable extension implementation by providing boilerplate code #1424

Open
nyurik opened this issue Dec 17, 2023 · 0 comments · May be fixed by #1425
Open

Simplify loadable extension implementation by providing boilerplate code #1424

nyurik opened this issue Dec 17, 2023 · 0 comments · May be fixed by #1425

Comments

@nyurik
Copy link
Contributor

nyurik commented Dec 17, 2023

When implementing loadable extension, it seems every/most implementations will have this code (copied mostly from the example). In the spirit of "Rusqlite is an ergonomic wrapper" concept, I think it would be good to provide a macro or some other means to avoid code duplication and complexity in the user's code.

use std::os::raw::{c_char, c_int};
use rusqlite::ffi;
use rusqlite::ffi::SQLITE_NOTICE;
use rusqlite::{to_sqlite_error, Connection, Result};

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle]
pub extern "C" fn sqlite3_extension_init(
    db: *mut ffi::sqlite3,
    pz_err_msg: *mut *mut c_char,
    p_api: *mut ffi::sqlite3_api_routines,
) -> c_int {
    if p_api.is_null() {
        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<()> {
    let db = unsafe { Connection::extension_init2(db, p_api)? };
    
    // Function registration code goes here

    Ok(())
}
@nyurik nyurik linked a pull request Dec 18, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant