Escaping generated SQL correctly for MATCH clauses #3166
Unanswered
jaseemabid
asked this question in
Q&A
Replies: 3 comments 2 replies
-
No idea but does it work if you set |
Beta Was this translation helpful? Give feedback.
1 reply
-
I was not able to reproduce this. For reference I've used the following code: use diesel::{
expression::{AsExpression, Expression, TypedExpressionType},
sql_types::SqlType,
};
diesel::infix_operator!(Match, " MATCH ");
// Normally you would put this on a trait instead
pub fn match_<T, U, ST>(left: T, right: U) -> Match<T, U::Expression>
where
T: Expression<SqlType = ST>,
U: AsExpression<ST>,
ST: SqlType + TypedExpressionType,
{
Match::new(left, right.as_expression())
}
use diesel::prelude::*;
table! {
fts(id) {
id -> Integer,
text -> Text,
rank -> Integer,
}
}
fn main() {
use self::fts::dsl::*;
let mut conn = SqliteConnection::establish(":memory:").unwrap();
diesel::sql_query("CREATE VIRTUAL TABLE fts USING fts4(id INTEGER NOT NULL PRIMARY KEY, text TEXT NOT NULL, rank INTEGER NOT NULL);").execute(&mut conn).unwrap();
diesel::insert_into(fts).values((id.eq(42), text.eq("foo"), rank.eq(1))).execute(&mut conn).unwrap();
let q = "@foo";
let query = fts.select(id).filter(match_(text, q)).order_by(rank);
dbg!(diesel::debug_query(&query));
let res = query.load::<i32>(&mut conn).unwrap();
dbg!(fts.load::<(i32, String, i32)>(&mut conn));
} [package]
name = "diesel_fst_test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
diesel = { version = "2.0.0-rc.0", features = ["sqlite"] }
|
Beta Was this translation helpful? Give feedback.
1 reply
-
For now this did the trick for me, but I'm not sure this covers all the edge cases. pub fn quote(q: &str) -> String {
q.split_whitespace()
.map(|w| {
if w.starts_with(['#', '@']) {
format!("\"{}\"", w)
} else {
w.to_string()
}
})
.collect::<Vec<_>>()
.join(" ")
}
#[test]
fn q() {
assert_eq!(quote("hello world"), "hello world");
assert_eq!(quote("@A OR @B"), "\"@A\" OR \"@B\"");
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using diesel with sqlite's full text search. I have a fairly simple table with schema
(id, text)
. The query string must be quoted with an extra double quote to handle special characters.The first example works, while the second doesn't.
Since diesel doesn't support
MATCH
out of the box, I made a new infix operatorAnd its used like
Now this works fine for simple strings, but fails when the query contains a
@
.Example with error returned by sqlite:
❓ How would you go about fixing this? 🙏
Beta Was this translation helpful? Give feedback.
All reactions