Reputation: 2942
I want to query a field using MySQLs "SOUNDS LIKE".
SQL:
WHERE field SOUNDS LIKE "blah"
How to query this with diesel framework?
I thought of
.filter(sql("field SOUNDS LIKE ???"))
But how to inject (bind()
) my value here with correct escaping?
Or is there a better way to filter using unsupported SQL?
EDIT1:
I found the infix_operator macro but it doesn't work. SqlType
, TypedExpressionType
and infix_operator macro is not found. But according to Github it's exactly there:
use diesel::sql_types::SqlType;
use diesel::expression::TypedExpressionType;
use diesel::expression::AsExpression;
use diesel::expression::Expression;
diesel::infix_operator!(SoundsLike, " SOUNDS LIKE ");
fn sounds_like<T, U, ST>(left: T, right: U) -> SoundsLike<T, U::Expression>
where
T: Expression<SqlType = ST>,
U: AsExpression<ST>,
ST: SqlType + TypedExpressionType,
{
SoundsLike::new(left, right.as_expression())
}
Upvotes: 1
Views: 207
Reputation: 3435
I found the infix_operator macro but it doesn't work. SqlType, TypedExpressionType and infix_operator macro is not found. But according to Github it's exactly there:
That's because you've looked at the master branch, which contains unreleased changes. One of them is the renaming of diesel::infix_operator!
from diesel_infix_operator!
By just using the variant from the latest release your code should just work:
#[macro_use] extern crate diesel;
use diesel::expression::AsExpression;
use diesel::expression::Expression;
diesel_infix_operator!(SoundsLike, " SOUNDS LIKE ");
fn sounds_like<T, U, ST>(left: T, right: U) -> SoundsLike<T, U::Expression>
where
T: Expression<SqlType = ST>,
U: AsExpression<ST>,
{
SoundsLike::new(left, right.as_expression())
}
Upvotes: 1
Reputation: 13518
You can bind parameters with the bind
method:
table.filter(sql("SOUNDS LIKE ").bind::<Text, _>(input);
Upvotes: 1