Fixing fuzzy_search to escape like chars.

This commit is contained in:
Dessalines 2021-11-19 15:07:20 -05:00
parent 4ce01f8bb4
commit e58c2048ed
2 changed files with 8 additions and 5 deletions

View file

@ -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::<Person>(conn)
}

View file

@ -70,7 +70,7 @@ pub fn from_opt_str_to_opt_enum<T: std::str::FromStr>(opt: &Option<String>) -> 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]