mysql::value::FromValue 在调用 mysql::from_row 时没有为大元组实现

标签 mysql rust

我想转换结构 User 以添加十个字母命名的属性,从 afmp

#[macro_use]
extern crate mysql;

use mysql::Pool;

#[derive(Debug, Default)]
pub struct User {
    /// The ID number of the user.
    pub id: u32,
    /// The name of the user / machine.
    pub user: String,

    /// The name of the company the user is attached to.
    pub company: Option<String>,
    /// The mass of the weight in the machine.
    pub mass_of_weight: Option<f64>,
    /// The total height of the machine.
    pub total_height: Option<f64>,

    /// The callibration coefficient for BMWI calculations involving this machine.
    pub k_coefficient: Option<f64>,
    /// The EU value for the machine at this point in time.
    pub energy_utilisation: Option<f64>,
    pub a: Option<f64>,
    pub b: Option<f64>,
    pub c: Option<f64>,
    pub d: Option<f64>,
    pub e: Option<f64>,
    pub f: Option<f64>,
    pub m: Option<f64>,
    pub n: Option<f64>,
    pub o: Option<f64>,
    pub p: Option<f64>,
}

impl User {
    /// Selects a user from the database, given their id.
    pub fn new(id: u32, pool: &Pool) -> Option<Self> {
        let (
            user,
            company,
            mass_of_weight,
            total_height,
            k_coefficient,
            energy_utilisation,
            a,
            b,
            c,
            d,
            e,
            f,
            m,
            n,
            o,
            p,
        ) = match pool.prep_exec(
            include_str!("../../resources/sql/users/select.mysql"),
            params!{"id" => &id},
        ).unwrap()
            .next()
        {
            Some(Ok(row)) => mysql::from_row(row),
            _ => return None,
        };

        Some(User {
            id: id,
            user: user,
            company: company,
            mass_of_weight: mass_of_weight,
            total_height: total_height,
            k_coefficient: k_coefficient,
            energy_utilisation: energy_utilisation,
            a: a,
            b: b,
            c: c,
            d: d,
            e: e,
            f: f,
            m: m,
            n: n,
            o: o,
            p: p,
        })
    }
}

fn main() {}

当我运行 cargo build 时,我得到这个错误:

error[E0277]: the trait bound `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _): mysql::prelude::FromValue` is not satisfied
  --> src/main.rs:62:30
   |
62 |             Some(Ok(row)) => mysql::from_row(row),
   |                              ^^^^^^^^^^^^^^^ the trait `mysql::prelude::FromValue` is not implemented for `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _)`
   |
   = note: required because of the requirements on the impl of `mysql::prelude::FromRow` for `(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _)`
   = note: required by `mysql::from_row`

SQL 是:

SELECT id, user, company, mass_of_weight, total_height, k_coefficient,
       energy_utilisation, a, b, c, d, e, f, m, n, o, p
       FROM users

我该如何处理这个错误?我试过 cargo clean;它没有帮助。

最佳答案

编译器没有骗你。 FromRow仅针对特定大小的元组实现。 author of the library suggests calling Row::take :

pub fn new(id: u32, pool: &Pool) -> Option<Self> {
    let sql = include_str!("../../resources/sql/users/select.mysql");

    let mut row = match pool.prep_exec(sql, params!{"id" => &id}).unwrap().next() {
        Some(Ok(row)) => row,
        _ => return None,
    };

    Some(User {
        id: row.take("id").unwrap(),
        user: row.take("user").unwrap(),
        company: row.take("company").unwrap(),
        mass_of_weight: row.take("mass_of_weight").unwrap(),
        total_height: row.take("total_height").unwrap(),
        k_coefficient: row.take("k_coefficient").unwrap(),
        energy_utilisation: row.take("energy_utilisation").unwrap(),
        a: row.take("a").unwrap(),
        b: row.take("b").unwrap(),
        c: row.take("c").unwrap(),
        d: row.take("d").unwrap(),
        e: row.take("e").unwrap(),
        f: row.take("f").unwrap(),
        m: row.take("m").unwrap(),
        n: row.take("n").unwrap(),
        o: row.take("o").unwrap(),
        p: row.take("p").unwrap(),
    })
}

由于您没有对数据执行任何转换,您可能想看看 Diesel更适合您的应用。

关于mysql::value::FromValue 在调用 mysql::from_row 时没有为大元组实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49443070/

相关文章:

mysql - SQL 将日期格式从 yyyy-mm-dd 更改为 dd-mm-yyyy

php - WordPress:结合类别和标签

mysql - SQL : I search the good query with select max() and select count()

Rust 线​​程 'main' 已溢出其堆栈

error-handling - 替代适用于迭代器映射的 try (?) 运算符

php - 检索所有行但缺少第一条记录

php - 在 PHP 中从远程数据库检索数据时出错

rust - 如何在另一个闭包中捕获一个闭包?

rust - 我如何告诉 Rust 我的 Option 的值实际上比传递给 and_then 的闭包还长?

rust - 我可以在 Rust 中创建一个包含字符串和该字符串切片的结构吗?