在玩Diesel时,我遇到了写一个函数的问题,该函数将Strings 的向量作为输入并执行以下操作:
Strings 组合到一个大型查询中ConnectionVec如果我一步构建查询,它可以正常工作:
fn get_books(authors: Vec<String>, connection: SqliteConnection) {
use schema::ebook::dsl::*;
let inner = author
.like(format!("%{}%", authors[0]))
.and(author.like(format!("%{}%", authors[1])))
.and(author.like(format!("%{}%", authors[2])));
ebook
.filter(inner)
.load::<Ebook>(&connection)
.expect("Error loading ebook");
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试以更多步骤生成查询(为了使用输入向量的可变长度需要),我无法编译它:
fn get_books(authors: Vec<String>, connection: SqliteConnection) {
use schema::ebook::dsl::*;
let mut inner = author
.like(format!("%{}%", authors[0]))
.and(author.like(format!("%{}%", authors[1]))); // <1>
inner = inner.and(author.like(format!("%{}%", authors[2]))); // <2>
ebook
.filter(inner)
.load::<Ebook>(&connection)
.expect("Error loading ebook");
}
Run Code Online (Sandbox Code Playgroud)
这会生成以下错误:
inner = inner.and(author.like(format!("%{}%",authors[2])));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `diesel::expression::operators::Like`, found struct `diesel::expression::operators::And`
Run Code Online (Sandbox Code Playgroud)
我不明白为什么Rust要求a Like而不是an And.标记的线条的功能<1>返回一个And,因此And存储在其中inner.
我错过了什么?为什么第一部分代码编译而第二部分不编译?生成此类查询的正确方法是什么?
您需要做的第一件事是查看完整的错误消息:
error[E0308]: mismatched types
--> src/main.rs:23:13
|
23 | inner = inner.and(author.like(format!("%{}%", authors[2])));//<2>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `diesel::expression::operators::Like`, found struct `diesel::expression::operators::And`
|
= note: expected type `diesel::expression::operators::And<diesel::expression::operators::Like<_, _>, _>`
found type `diesel::expression::operators::And<diesel::expression::operators::And<diesel::expression::operators::Like<_, _>, diesel::expression::operators::Like<schema::ebook::columns::author, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>>, _>`
Run Code Online (Sandbox Code Playgroud)
它很长,但那是因为它是完全合格的。让我们把最后一部分缩短一点:
expected type `And<Like<_, _>, _>`
found type `And<And<Like<_, _>, Like<author, Bound<Text, String>>>, _>`
Run Code Online (Sandbox Code Playgroud)
如果您查看 的文档and,您会看到每次调用都and使用接收器并返回一个全新的类型 — And:
fn and<T: AsExpression<Bool>>(self, other: T) -> And<Self, T::Expression>
Run Code Online (Sandbox Code Playgroud)
这是 Diesel 能够在没有运行时开销的情况下生成强类型 SQL 表达式的能力的核心。所有的工作都委托给类型系统。事实上,Diesel 的创建者有一个完整的演讲,他展示了 Diesel 推动类型系统的程度以及它有什么好处。
回到您的问题,将 anAnd<_, _>与 an存储在同一位置是不可能的,And<And<_, _>, _>因为它们将具有不同的大小并且实际上是不同的类型。从根本上说,这与尝试将整数存储在布尔值中是一样的。
事实上,没有办法知道你需要什么具体类型,因为你甚至不知道你将拥有多少条件——这取决于向量的大小。
在这种情况下,我们必须放弃静态调度并通过trait object转向动态调度。柴油有这种情况下的特定性状(其中也有很好的例子): BoxableExpression。
剩下的部分是将您的作者转换为like表达式并将它们组合起来。然而,我们需要一个基本情况,因为 whenauthors是空的。我们author = author为此构造了一个非常正确的陈述 ( )。
#[macro_use]
extern crate diesel;
use diesel::SqliteConnection;
use diesel::prelude::*;
use diesel::sql_types::Bool;
mod schema {
table! {
ebook (id) {
id -> Int4,
author -> Text,
}
}
}
fn get_books(authors: Vec<String>, connection: SqliteConnection) {
use schema::ebook::dsl::*;
let always_true = Box::new(author.eq(author));
let query: Box<BoxableExpression<schema::ebook::table, _, SqlType = Bool>> = authors
.into_iter()
.map(|name| author.like(format!("%{}%", name)))
.fold(always_true, |query, item| {
Box::new(query.and(item))
});
ebook
.filter(query)
.load::<(i32, String)>(&connection)
.expect("Error loading ebook");
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
如果没有更好的SQL方法来执行此操作,我也不会感到惊讶。例如,PostgreSQL 似乎具有WHERE col LIKE ANY( subselect )和WHERE col LIKE ALL( subselect )形式。
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |