问题:在 rust-postgres 中使用固定精度数的正确方法是什么?

定义和传递固定精度数(Postgres 中的十进制或数字)到 rust-postgres 中的插入的正确方法是什么?

transaction.execute("INSERT INTO commons_account(
            display_name, blocked, balance, blocked_balance, password, salt)
            VALUES ($1, $2, $3, $4, $5, $6);", &[&"bob", &false, &0, &0, &"dfasdfsa", &"dfsdsa"]).unwrap();

余额和冻结余额都是numeric并且运行此代码会出现此错误

thread 'test' panicked at 'called `Result::unwrap()` on an `Err` value: WrongType(Numeric)'

解答

Numeric不在支持的类型中。您需要自己实现 trait ToSql 类似的东西:

struct Float64(f64);

impl ToSql for Float64 { // Or a fixed-precision type.
    to_sql_checked!();

    fn to_sql<W: Write + ?Sized>(&self, _: &Type, mut w: &mut W, _: &SessionInfo) -> Result<IsNull> {
        let num = 0;
        w.write_f64::<BigEndian>(num)?;
        *self = Float64(num);
        Ok(IsNull::No)
    }

    accepts!(Type::Numeric);
}
Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐