Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to determine whether the select result is no record found? #457

Open
kggg opened this issue Nov 9, 2023 · 12 comments
Open

How to determine whether the select result is no record found? #457

kggg opened this issue Nov 9, 2023 · 12 comments
Labels
help wanted Extra attention is needed

Comments

@kggg
Copy link

kggg commented Nov 9, 2023

怎么判断select结果是no record found?

pub async fn select_todo_by_id(id: u64) -> Result<Todolist, rbatis::rbdc::Error> { let rs = RB .query_decode("select * from todolist where id = ?", vec![Value::U64(id)]) .await?; Ok(rs) }
现在错误打印出来的是以下信息
E("error while decoding value: invalid type: unit value, expected struct Todolist")

@zhuxiujia
Copy link
Member

Todolist

Todolist 这个结构体 解析uint失败的column成员

@kggg
Copy link
Author

kggg commented Nov 9, 2023

todolist中没有存在的记录就会报这个错, 有记录就没有问题,查询结果正常。

@Issues-translate-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


If there is no record in the todolist, this error will be reported. If there is a record, there will be no problem and the query result will be normal.

@zhuxiujia
Copy link
Member

todolist中没有存在的记录就会报这个错, 有记录就没有问题,查询结果正常。

可以告诉我数据库类型,表结构体的定义,以及 数据库表的数据吗?

@Issues-translate-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


If there is no record in the todolist, this error will be reported. If there is a record, there will be no problem and the query result will be normal.

Can you tell me the database type, the definition of the table structure, and the data of the database table?

@kggg
Copy link
Author

kggg commented Nov 9, 2023

mysql 8

mysql> desc todolist;
+------------+-----------------+------+-----+---------+----------------+
| Field      | Type            | Null | Key | Default | Extra          |
+------------+-----------------+------+-----+---------+----------------+
| id         | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| created_at | datetime(3)     | NO   |     | NULL    |                |
| updated_at | datetime(3)     | NO   |     | NULL    |                |
| deleted_at | datetime(3)     | YES  | MUL | NULL    |                |
| type_id    | bigint          | YES  |     | NULL    |                |
| user       | varchar(20)     | NO   |     | NULL    |                |
| title      | varchar(30)     | NO   |     | NULL    |                |
| status     | int             | NO   |     | 0       |                |
| level      | varchar(8)      | NO   |     | 一般    |                |
| content    | text            | NO   |     | NULL    |                |
+------------+-----------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

···
#[derive(Debug, Deserialize, PartialEq, Eq, Clone, Serialize)]
pub struct Todolist {
id: Option,
created_at: DateTime,
updated_at: DateTime,
deleted_at: Option,
type_id: u64,
user: String,
title: String,
status: u8,
level: String,
content: String,
}

@Issues-translate-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


mysql 8

mysql> desc todolist;
+------------+-----------------+------+-----+---------+----------------+
| Field      | Type            | Null | Key | Default | Extra          |
+------------+-----------------+------+-----+---------+----------------+
| id         | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| created_at | datetime(3)     | NO   |     | NULL    |                |
| updated_at | datetime(3)     | NO   |     | NULL    |                |
| deleted_at | datetime(3)     | YES  | MUL | NULL    |                |
| type_id    | bigint          | YES  |     | NULL    |                |
| user       | varchar(20)     | NO   |     | NULL    |                |
| title      | varchar(30)     | NO   |     | NULL    |                |
| status     | int             | NO   |     | 0       |                |
| level      | varchar(8)      | NO   |     | 一般    |                |
| content    | text            | NO   |     | NULL    |                |
+------------+-----------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

···
#[derive(Debug, Deserialize, PartialEq, Eq, Clone, Serialize)]
pub struct Todolist {
id: Option,
created_at: DateTime,
updated_at: DateTime,
deleted_at: Option,
type_id: u64,
user: String,
title: String,
status: u8,
level: String,
content: String,
}

      

@zhuxiujia
Copy link
Member

zhuxiujia commented Nov 9, 2023

mysql 8

mysql> desc todolist;
+------------+-----------------+------+-----+---------+----------------+
| Field      | Type            | Null | Key | Default | Extra          |
+------------+-----------------+------+-----+---------+----------------+
| id         | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| created_at | datetime(3)     | NO   |     | NULL    |                |
| updated_at | datetime(3)     | NO   |     | NULL    |                |
| deleted_at | datetime(3)     | YES  | MUL | NULL    |                |
| type_id    | bigint          | YES  |     | NULL    |                |
| user       | varchar(20)     | NO   |     | NULL    |                |
| title      | varchar(30)     | NO   |     | NULL    |                |
| status     | int             | NO   |     | 0       |                |
| level      | varchar(8)      | NO   |     | 一般    |                |
| content    | text            | NO   |     | NULL    |                |
+------------+-----------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

··· #[derive(Debug, Deserialize, PartialEq, Eq, Clone, Serialize)] pub struct Todolist { id: Option, created_at: DateTime, updated_at: DateTime, deleted_at: Option, type_id: u64, user: String, title: String, status: u8, level: String, content: String, }

todolist中没有存在的记录就会报这个错,
1 Result<Todolist, rbatis::rbdc::Error> 应该改成 Result<Option<Todolist>, rbatis::rbdc::Error>
这样查不到记录的时候是None才对
2 deleted_at 和type_id 在表里可以为null,那么这2个column 都应该用Option<xxx>才对

@zhuxiujia zhuxiujia added the help wanted Extra attention is needed label Nov 9, 2023
@kggg
Copy link
Author

kggg commented Nov 9, 2023

谢谢, OK 了

@Issues-translate-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Thank you, OK

zhuxiujia added a commit that referenced this issue Nov 9, 2023
@zhuxiujia
Copy link
Member

谢谢, OK 了

我更新rbs序列化框架,提示更新为error while decoding value: invalid type: Option value, expected struct Activity 这样可读性更高。你可以使用cargo update命令更新依赖即可

@zhuxiujia
Copy link
Member

怎么判断select结果是no record found?

pub async fn select_todo_by_id(id: u64) -> Result<Todolist, rbatis::rbdc::Error> { let rs = RB .query_decode("select * from todolist where id = ?", vec![Value::U64(id)]) .await?; Ok(rs) } 现在错误打印出来的是以下信息 E("error while decoding value: invalid type: unit value, expected struct Todolist")

推荐使用crud! 宏内置的函数,更方便。

#[macro_use]
extern crate rbatis;

pub mod init;

use crate::init::init_db;
use rbatis::rbdc::datetime::DateTime;
use serde_json::json;

/// table
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Activity {
    pub id: Option<String>,
    pub name: Option<String>,
    pub pc_link: Option<String>,
    pub h5_link: Option<String>,
    pub pc_banner_img: Option<String>,
    pub h5_banner_img: Option<String>,
    pub sort: Option<String>,
    pub status: Option<i32>,
    pub remark: Option<String>,
    pub create_time: Option<DateTime>,
    pub version: Option<i64>,
    pub delete_flag: Option<i32>,
}
crud!(Activity {});
impl_select!(Activity{select_all_by_id(id:&str,name:&str) => "`where id = #{id} and name = #{name}`"});
impl_select!(Activity{select_by_id(id:&str) -> Option => "`where id = #{id} limit 1`"});

#[tokio::main]
pub async fn main() {
    fast_log::init(
        fast_log::Config::new()
            .console()
            .level(log::LevelFilter::Debug),
    )
    .expect("rbatis init fail");
    let rb = init_db().await;

    let data = Activity::select_by_column(&rb, "id", "1").await;
    println!("select_by_id = {}", json!(data));

    let data = Activity::select_all_by_id(&rb, "1", "1").await;
    println!("select_all_by_id = {}", json!(data));

    let data = Activity::select_by_id(&rb, "1").await;
    println!("select_by_id = {}", json!(data));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants