From e58c2048ed1a6d4438928cb854e0be052f53619e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 19 Nov 2021 15:07:20 -0500 Subject: [PATCH] Fixing fuzzy_search to escape like chars. --- crates/db_schema/src/impls/person.rs | 4 ++-- crates/db_schema/src/lib.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index a9ba1d2e..b0476480 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -5,7 +5,7 @@ use crate::{ source::person::{Person, PersonForm}, traits::Crud, }; -use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, *}; +use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl}; use url::Url; mod safe_type { @@ -194,7 +194,7 @@ impl Person { person .filter(deleted.eq(false)) .filter(local.eq(true)) - .filter(name.ilike(from_name)) + .filter(name.eq(from_name)) .first::(conn) } diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index f30f0bd8..a2a19290 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -70,7 +70,7 @@ pub fn from_opt_str_to_opt_enum(opt: &Option) -> O } pub fn fuzzy_search(q: &str) -> String { - let replaced = q.replace(" ", "%"); + let replaced = q.replace("%", "\\%").replace("_", "\\_").replace(" ", "%"); format!("%{}%", replaced) } @@ -154,8 +154,11 @@ mod tests { #[test] fn test_fuzzy_search() { - let test = "This is a fuzzy search"; - assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string()); + let test = "This %is% _a_ fuzzy search"; + assert_eq!( + fuzzy_search(test), + "%This%\\%is\\%%\\_a\\_%fuzzy%search%".to_string() + ); } #[test] -- 2.44.1