diff --git a/rslib/Cargo.toml b/rslib/Cargo.toml index 064430f96..b2577f674 100644 --- a/rslib/Cargo.toml +++ b/rslib/Cargo.toml @@ -47,13 +47,12 @@ rand = "0.7.3" num-integer = "0.1.42" itertools = "0.9.0" -# pinned until rusqlite 0.22 comes out [target.'cfg(target_vendor="apple")'.dependencies.rusqlite] -version = "0.22" +version = "0.23.1" features = ["trace", "functions", "collation"] [target.'cfg(not(target_vendor="apple"))'.dependencies.rusqlite] -version = "0.22" +version = "0.23.1" features = ["trace", "functions", "collation", "bundled"] [target.'cfg(linux)'.dependencies] diff --git a/rslib/src/storage/sqlite.rs b/rslib/src/storage/sqlite.rs index 6f2870319..f7771e47a 100644 --- a/rslib/src/storage/sqlite.rs +++ b/rslib/src/storage/sqlite.rs @@ -9,7 +9,7 @@ use crate::{i18n::I18n, sched::cutoff::v1_creation_date, text::without_combining use regex::Regex; use rusqlite::{functions::FunctionFlags, params, Connection, NO_PARAMS}; use std::cmp::Ordering; -use std::{borrow::Cow, path::Path}; +use std::{borrow::Cow, path::Path, sync::Arc}; use unicase::UniCase; const SCHEMA_MIN_VERSION: u8 = 11; @@ -89,6 +89,7 @@ fn add_without_combining_function(db: &Connection) -> rusqlite::Result<()> { /// Adds sql function regexp(regex, string) -> is_match /// Taken from the rusqlite docs +type BoxError = Box; fn add_regexp_function(db: &Connection) -> rusqlite::Result<()> { db.create_scalar_function( "regexp", @@ -97,21 +98,12 @@ fn add_regexp_function(db: &Connection) -> rusqlite::Result<()> { move |ctx| { assert_eq!(ctx.len(), 2, "called with unexpected number of arguments"); - let saved_re: Option<&Regex> = ctx.get_aux(0)?; - let new_re = match saved_re { - None => { - let s = ctx.get::(0)?; - match Regex::new(&s) { - Ok(r) => Some(r), - Err(err) => return Err(rusqlite::Error::UserFunctionError(Box::new(err))), - } - } - Some(_) => None, - }; + let re: Arc = ctx + .get_or_create_aux(0, |vr| -> std::result::Result<_, BoxError> { + Ok(Regex::new(vr.as_str()?)?) + })?; let is_match = { - let re = saved_re.unwrap_or_else(|| new_re.as_ref().unwrap()); - let text = ctx .get_raw(1) .as_str() @@ -120,10 +112,6 @@ fn add_regexp_function(db: &Connection) -> rusqlite::Result<()> { re.is_match(text) }; - if let Some(re) = new_re { - ctx.set_aux(0, re); - } - Ok(is_match) }, )