rust - 将任何 Diesel 表作为参数传递

标签 rust rust-diesel

我已经实现了 Diesel 的 Rust 项目,它生成了包含我所有表的 schema.rs 文件:

table! {
    users (id) {
        id -> Uuid,
        name -> Varchar,
    }
}

table! {
    items (id) {
        id -> Uuid,
        name -> Varchar,
    }
}
如何在我的实用程序函数中将任何表作为参数传递?
例如,
pub trait Search {
    fn internal_get_by_id(
        diesel_table: diesel::table, // this argument should pass any diesel table
        table_id: diesel::table::id, // this argument should pass Uuid from table
        conn: &Conn,
        id: Uuid,
    ) -> Fallible<Option<Self>>
    where
        Self: Sized,
    {
        diesel_table
            .filter(table_id.eq(id))
            .first(conn.raw())
            .optional()
            .map_err(Error::from)
    }

    fn get_by_id(conn: &Conn, id: Uuid) -> Fallible<Option<Self>>
    where
        Self: Sized;
}

impl Search for User {
    fn get_by_id(conn: &Conn, id: Uuid) -> Fallible<Option<User>> {
        Self::internal_get_by_id(users::table, users::id, conn, id)
    }
}

impl Search for Item {
    fn get_by_id(conn: &Conn, id: Uuid) -> Fallible<Option<Item>> {
        Self::internal_get_by_id(items::table, items::id, conn, id)
    }
}

最佳答案

首先:在 Rust 中使用 Diesel 编写跨多个表/列的通用代码通常不是一个好主意,特别是如果您是该语言的新手并且对 trait bound 和 where 子句没有很好的理解然而。
您需要列出允许构建此通用查询所需的所有特征边界,以便可以在编译时检查所有内容。以下实现应该可以解决这个问题(未经测试,希望我没有错过特征绑定(bind))

fn internal_get_by_id<T, C>(
    diesel_table: T, 
    table_id: C,     
    conn: &Conn,
    id: Uuid,
) -> Fallible<Option<Self>>
where
    Self: Sized,
    T: Table + FilterDsl<dsl::Eq<C, Uuid>>,
    C: Column + Expression<SqlType = diesel::sql_types::Uuid>,
    dsl::Filter<T, dsl::Eq<C, Uuid>>: LimitDsl,
    dsl::Limit<dsl::Filter<T, dsl::Eq<C, Uuid>>>: LoadQuery<Conn, Self>,
    Self: Queryable<dsl::SqlTypeOf<dsl::Limit<dsl::Filter<T, dsl::Eq<C, Uuid>>>>, Conn::Backend>,
{
    diesel_table
        .filter(table_id.eq(id))
        .first(conn.raw())
        .optional()
        .map_err(Error::from)
}

关于rust - 将任何 Diesel 表作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64120177/

相关文章:

multithreading - 如何在线程之间共享非发送对象?

rust - 如何使用新的 println 显示结构!格式?

rust - 包含文件后,柴油和火箭的进口就会中断

rust - 为什么 len() 和 is_empty() 没有在特征中定义?

loops - 是否有展开或继续循环的快捷方式?

function - rust 封闭和fn不匹配

rust - diesel 应该使用同步 actor、actix_web::web::block 还是 futures-cpupool 来运行?

rust - 关系的渴望

rust - 带有 eq_any 的子查询无法编译