繁体   English   中英

如何在 Rust Diesel 中使用时间戳和间隔进行算术运算

[英]How to do arithmetic with timestamps and intervals in Rust Diesel

我一直在尝试使用过滤器表达式构建一个柴油查询,这些过滤器表达式对时间戳和时间间隔进行算术和比较。 我相信使用开箱即用的柴油无法编写以下表达式。 不幸的是,我采用了使用sql(...)函数的大锤方法:

let query = videos::table
    .inner_join(events::table)
    .filter(sql(r#""events"."start_date" <= NOW() + interval '60 seconds'"#))
    .filter(sql(r#""events"."start_date" + ("videos"."duration" * interval '1 second') > NOW()"#))

我的(缩写)架构:

table! {
    events (id) {
        id -> Int4,
        video_id -> Int4,
        start_date -> Timestamp,
    }
}

table! {
    videos (id) {
        id -> Int4,
        duration -> Float4,
    }
}

我的问题:

  1. 我错了吗? 这可以用没有自定义类型的柴油编写吗?
  2. 如果这不能用香草柴油来写,我怎么能把它分解成一个更类型安全、对柴油友好的表达式? 哪些部分是自定义的,我需要实现哪些特征?

我错了吗? 这可以用没有自定义类型的柴油编写吗?

是的,这可以用柴油提供的方法和一些小的架构更改来编写。 查看相应的文档

Diesel 要求您只能向时间戳添加/减去间隔。 这意味着您需要一点架构来实现这一点:

table! {
    videos (id) {
        id -> Int4,
        duration -> Interval,
    }
}

然后你的查询是这样写的:

let query = videos::table
    .inner_join(events::table)
    .filter(events::start_date.le(now + 60.seconds()))
    .filter(now.lt(events::start_date + videos::duration))

如果这不能用香草柴油来写,我怎么能把它分解成一个更类型安全、对柴油友好的表达式? 哪些部分是自定义的,我需要实现哪些特征?

从理论上讲,应该可以在柴油之外实施。 该代码很可能与用于在柴油本身中实现该代码的代码相同。 可能您需要使用本地类型和更具体的实现。 因此,如果您无法更改videos::duration的类型videos::duration那么沿着这条路线走下去可能很有意义。

Rust 如何实现 diesel::Insertable<div id="text_translate"><p> 我正在尝试在 Rust 中实现特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 这是因为以下代码导致此错误:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文档,我相信我应该让自己成为一个派生可插入的结构</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后来我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑为什么编译器说它找不到包含我正在尝试定义的结构的板条箱或模块。</p></div><table></table>

[英]Rust how to implement diesel::Insertable<table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用锈柴油进行查询分组 如何使用锈柴油做全文查询 使用rust柴油时如何进行计数查询 如何在Rust中执行指针算术? Rust柴油Postgres如何添加分页查询 Rust 如果表格行存在于柴油中,如何 select? 如何处理 Rust-Diesel 中的多个错误? Rust 如何实现 diesel::Insertable<div id="text_translate"><p> 我正在尝试在 Rust 中实现特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 这是因为以下代码导致此错误:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文档,我相信我应该让自己成为一个派生可插入的结构</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后来我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑为什么编译器说它找不到包含我正在尝试定义的结构的板条箱或模块。</p></div><table></table> 如何在红豆杉或任何前端使用生锈柴油? 如何使用生锈柴油获得柱的平均值?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM