Skip to content

Commit

Permalink
Added a with_regexp function to sqliteconnectoptions
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders committed Feb 9, 2023
1 parent 041b331 commit d3895c7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ mac_address = ["sqlx-core/mac_address", "sqlx-macros?/mac_address", "sqlx-postgr
rust_decimal = ["sqlx-core/rust_decimal", "sqlx-macros?/rust_decimal", "sqlx-mysql?/rust_decimal", "sqlx-postgres?/rust_decimal"]
time = ["sqlx-core/time", "sqlx-macros?/time", "sqlx-mysql?/time", "sqlx-postgres?/time", "sqlx-sqlite?/time"]
uuid = ["sqlx-core/uuid", "sqlx-macros?/uuid", "sqlx-mysql?/uuid", "sqlx-postgres?/uuid", "sqlx-sqlite?/uuid"]
regexp = ["sqlx-sqlite?/regexp"]

[workspace.dependencies]
# Driver crates
Expand Down
2 changes: 2 additions & 0 deletions sqlx-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ offline = ["sqlx-core/offline", "serde"]
migrate = ["sqlx-core/migrate"]

chrono = ["dep:chrono", "bitflags"]
regexp = ["dep:regex"]

[dependencies]
futures-core = { version = "0.3.19", default-features = false }
Expand All @@ -43,6 +44,7 @@ atoi = "1.0"
log = "0.4.17"

serde = { version = "1.0.145", features = ["derive"], optional = true }
regex = { version = "1.5.5", optional = true }

[dependencies.libsqlite3-sys]
version = "0.25.1"
Expand Down
8 changes: 6 additions & 2 deletions sqlx-sqlite/src/connection/establish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct EstablishParams {
extensions: IndexMap<CString, Option<CString>>,
pub(crate) thread_name: String,
pub(crate) command_channel_size: usize,
#[cfg(feature = "regexp")]
register_regexp_function: bool,
}

impl EstablishParams {
Expand Down Expand Up @@ -145,6 +147,8 @@ impl EstablishParams {
extensions,
thread_name: (options.thread_name)(THREAD_ID.fetch_add(1, Ordering::AcqRel)),
command_channel_size: options.command_channel_size,
#[cfg(feature = "regexp")]
register_regexp_function: options.register_regexp_function,
})
}

Expand Down Expand Up @@ -250,8 +254,8 @@ impl EstablishParams {
}
}

#[cfg(feature = "regex")]
{
#[cfg(feature = "regexp")]
if self.register_regexp_function {
// configure a `regexp` function for sqlite, it does not come with one by default
let status = crate::sqlite::regexp::register(handle.as_ptr());
if status != SQLITE_OK {
Expand Down
22 changes: 22 additions & 0 deletions sqlx-sqlite/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub struct SqliteConnectOptions {

pub(crate) serialized: bool,
pub(crate) thread_name: Arc<DebugFn<dyn Fn(u64) -> String + Send + Sync + 'static>>,

#[cfg(feature = "regexp")]
pub(crate) register_regexp_function: bool,
}

impl Default for SqliteConnectOptions {
Expand Down Expand Up @@ -458,4 +461,23 @@ impl SqliteConnectOptions {
.insert(extension_name.into(), Some(entry_point.into()));
self
}

/// Register a regexp function that allows using regular expressions in queries.
///
/// ```
/// use std::str::FromStr;
/// use sqlx::sqlite::SqliteConnectOptions;
/// let sqlite = SqliteConnectOptions::new()
/// .with_regex()
/// .connect()
/// .unwrap();
/// let tables = query_unchecked!("SELECT name FROM sqlite_schema WHERE name REGEXP 'foo(\d+)bar'").fetch_all(&sqlite).unwrap();
/// ```
///
/// This uses the [`regex`] crate, and is only enabled when you enable the `regex` feature is enabled on sqlx
#[cfg(feature = "regexp")]
pub fn with_regexp(mut self) -> Self {
self.register_regexp_function = true;
self
}
}

0 comments on commit d3895c7

Please sign in to comment.