-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add macro, entrypoint function, and example for permanently-loaded ex…
…tensions (#13) * Add macro, entrypoint function, and example for permanently-loaded extensions. See https://www.sqlite.org/rescode.html#ok_load_permanently * Update load_permanent.rs correct the name of the example to build in the comments * keep version at 0.0.5 for now --------- Co-authored-by: Alex Garcia <alexsebastian.garcia@gmail.com>
- Loading branch information
Showing
6 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! cargo build --example load_permanent | ||
//! sqlite3 :memory: '.read examples/test.sql' | ||
|
||
use sqlite_loadable::prelude::*; | ||
use sqlite_loadable::{api, define_scalar_function, Result}; | ||
|
||
// This function will be registered as a scalar function named "hello", and will be called on | ||
// every invocation. It's goal is to return a string of "hello, NAME!" where NAME is the | ||
// text value of the 1st argument. | ||
pub fn hello(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> { | ||
let name = api::value_text(values.get(0).expect("1st argument as name"))?; | ||
|
||
api::result_text(context, format!("hello, {}!", name))?; | ||
Ok(()) | ||
} | ||
|
||
// Exposes a extern C function named "sqlite3_hello_init" in the compiled dynamic library, | ||
// the "entrypoint" that SQLite will use to load the extension. | ||
// Notice the naming sequence - "sqlite3_" followed by "hello" then "_init". Since the | ||
// compiled file is named "libhello.dylib" (or .so/.dll depending on your operating system), | ||
// SQLite by default will look for an entrypoint called "sqlite3_hello_init". | ||
// See "Loading an Extension" for more details <https://www.sqlite.org/loadext.html#loading_an_extension> | ||
#[sqlite_entrypoint_permanent] | ||
pub fn sqlite3_hello_init(db: *mut sqlite3) -> Result<()> { | ||
let flags = FunctionFlags::UTF8 | FunctionFlags::DETERMINISTIC; | ||
define_scalar_function(db, "hello", 1, hello, flags)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters