Skip to content

Commit

Permalink
store: Fix fulltextsearch space handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Dec 11, 2021
1 parent 528fd19 commit 5d2d6c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions store/postgres/src/relational_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ impl<'a> QueryFragment<Pg> for QueryValue<'a> {
}
ColumnType::TSVector(_) => {
out.push_sql("to_tsquery(");
out.push_bind_param::<Text, _>(s)?;
bind_quoted_param(s, &mut out)?;
out.push_sql(")");
Ok(())
}
Expand Down Expand Up @@ -608,6 +608,15 @@ impl<'a> QueryFragment<Pg> for QueryValue<'a> {
}
}

fn bind_quoted_param(value: &str, out: &mut AstPass<Pg>) -> QueryResult<()> {
if !value.contains(" ") {
return out.push_bind_param::<Text, _>(&value);
}

let value = format!("'{}'", value.replace("'", "\\\'"));
out.push_bind_param::<Text, _>(&value)
}

#[derive(Copy, Clone, PartialEq)]
enum Comparison {
Less,
Expand Down Expand Up @@ -2181,8 +2190,7 @@ impl<'a> SortKey<'a> {
let name = column.name.as_str();
out.push_identifier(name)?;
out.push_sql(", to_tsquery(");

out.push_bind_param::<Text, _>(&value.unwrap())?;
bind_quoted_param(&value.unwrap(), out)?;
out.push_sql("))");
}
_ => {
Expand Down
8 changes: 8 additions & 0 deletions store/postgres/tests/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,14 @@ fn check_find() {
vec!["3"],
user_query().filter(EntityFilter::Equal("userSearch".into(), "Shaq:*".into())),
)
.check(
vec!["1"],
user_query().filter(EntityFilter::Equal("userSearch".into(), "Jono a".into())),
)
.check(
vec!["1"],
user_query().filter(EntityFilter::Equal("userSearch".into(), "Jono 'a".into())),
)
.check(
vec!["1"],
user_query().filter(EntityFilter::Equal(
Expand Down

0 comments on commit 5d2d6c2

Please sign in to comment.