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

How to use it in rust? #10

Open
spa5k opened this issue Apr 28, 2023 · 6 comments
Open

How to use it in rust? #10

spa5k opened this issue Apr 28, 2023 · 6 comments

Comments

@spa5k
Copy link

spa5k commented Apr 28, 2023

Same as title

@asg017
Copy link
Owner

asg017 commented May 1, 2023

Hey, there's not a great answer for this yet, which sucks, planning to make that easier. If you're using rusqlite, you can use load_extension() to load a pre-compiled version of sqlite-regex, but that's not ideal since you'll have to separately manage the loadable extension file.

One day you'll be able to easily statically compile sqlite-regex into C/C++/Rust/Go, which you can only do now if you compile it yourself. I'll also add sqlite-regex to crates.io, which maybe an optional wrapper to easily integrate with rusqlite.

@spa5k
Copy link
Author

spa5k commented May 2, 2023

Yeah it's a big problem since you can't just precompile the files and place them in directory like in node modules in node.

@Sytten
Copy link

Sytten commented Jun 9, 2023

Also interested in that.
Generally the preferred way would be to compile statically and call libsqlite3_sys::sqlite3_auto_extension at runtime.
We will do experiments and report back.

@asg017
Copy link
Owner

asg017 commented Jun 10, 2023

Here's one way you can do it:

Cargo.toml:

[package]
name = "sqlite-regex-test"
version = "0.1.0"
edition = "2021"

[dependencies]
rusqlite = {version="0.29.0", features=["bundled"]}
sqlite-regex = {git="https://github.com/asg017/sqlite-regex.git"}

And lib/main.rs:

use rusqlite::{ffi::sqlite3_auto_extension, Connection, Result};
use sqlite_regex::sqlite3_regex_init;

fn main() -> Result<()> {
    unsafe {
        let sqlite3_regex_init = std::mem::transmute(sqlite3_regex_init as *const ());
        sqlite3_auto_extension(Some(sqlite3_regex_init));
    }

    let db = Connection::open_in_memory()?;

    let (version, result): (String, i32) =
        db.query_row("SELECT regex_version(), ? regexp 'a'", ["alex"], |row| {
            Ok((row.get(0)?, row.get(1)?))
        })?;
    println!("version={version} result={result}");

    Ok(())
}

Requires some slightly sketch and unsafe transmuting, but works in a pinch! I'll publish this crate to https://crates.io eventually.

@asg017
Copy link
Owner

asg017 commented Jun 10, 2023

sqlite-regex is now published on crates.io, at https://crates.io/crates/sqlite-regex

The above example works with this replacement in Cargo.toml:

sqlite-regex = "0.2.3-alpha.9"

@MikeTheSapien
Copy link

MikeTheSapien commented Nov 4, 2024

I'm using Windows 11.

Tried to add sqlite-regex = "0.2.3-alpha.9" in my Cargo.toml file and ran cargo check. It invoked an atypical error. I think it's a linker error? I still know a minuscule on that domain.

May I copy and paste the error here?

Other stuff in my .toml that might be relevant.

diesel = { version = "2.1.0", features = ["sqlite", "chrono"] }
dotenv = "0.15.0"
chrono = "0.4.26"

...
-- snip --
...

sqlite-regex = "0.2.3-alpha.9"
cargo --version
cargo 1.81.0 (2dbb1af80 2024-08-20)

PS.
I ran cargo update, no avail. Then cargo clean and cargo check, still no successful compilation.

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

No branches or pull requests

4 participants