"SOUNDS LIKE" 在柴油查询中
"SOUNDS LIKE" in diesel query
我想使用 MySQLs“SOUNDS LIKE”查询字段。
SQL:
WHERE field SOUNDS LIKE "blah"
如何用diesel框架查询这个?
我想到了
.filter(sql("field SOUNDS LIKE ???"))
但是如何通过正确的转义在此处注入 (bind()
) 我的值?
或者是否有更好的方法使用不受支持的 SQL 进行过滤?
编辑 1:
我找到了 infix_operator 宏,但它不起作用。未找到 SqlType
、TypedExpressionType
和 infix_operator 宏。但是根据 Github 它就在那里:
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())
}
您可以使用bind
方法绑定参数:
table.filter(sql("SOUNDS LIKE ").bind::<Text, _>(input);
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:
那是因为您查看了 master 分支,其中包含未发布的更改。其中之一是将 diesel::infix_operator!
从 diesel_infix_operator!
重命名
通过使用最新版本的变体,您的代码应该可以正常工作:
#[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())
}
我想使用 MySQLs“SOUNDS LIKE”查询字段。
SQL:
WHERE field SOUNDS LIKE "blah"
如何用diesel框架查询这个?
我想到了
.filter(sql("field SOUNDS LIKE ???"))
但是如何通过正确的转义在此处注入 (bind()
) 我的值?
或者是否有更好的方法使用不受支持的 SQL 进行过滤?
编辑 1:
我找到了 infix_operator 宏,但它不起作用。未找到 SqlType
、TypedExpressionType
和 infix_operator 宏。但是根据 Github 它就在那里:
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())
}
您可以使用bind
方法绑定参数:
table.filter(sql("SOUNDS LIKE ").bind::<Text, _>(input);
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:
那是因为您查看了 master 分支,其中包含未发布的更改。其中之一是将 diesel::infix_operator!
从 diesel_infix_operator!
重命名
通过使用最新版本的变体,您的代码应该可以正常工作:
#[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())
}