Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

mysql - "SOUNDS LIKE" in diesel query

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())
}
question from:https://stackoverflow.com/questions/66066750/sounds-like-in-diesel-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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())
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...