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

All results need to be wrapped in a structure, including results with only one field #454

Open
FoldingFan opened this issue Nov 6, 2023 · 9 comments
Labels
enhancement New feature or request

Comments

@FoldingFan
Copy link
Contributor

FoldingFan commented Nov 6, 2023

一个小建议

目前我在使用中发现不支持这种写法,逻辑上都是解析到结构体中,

对于这种单个字段的.希望能直接返回 Vec,像下面这样

 pysql!(query_all_ids(rb: &dyn rbatis::executor::Executor)-> Result<Vec<u64>, rbatis::Error> => "
    select id from biz_activity
");

(另外,rbatis代码对我来说写的很好,我在一边使用,一边阅读,学到了很多东西,感谢~)

@Issues-translate-bot Issues-translate-bot changed the title 所有结果都需要用结构体包装,包括只有一个字段的结果 All results need to be wrapped in a structure, including results with only one field Nov 6, 2023
@zhuxiujia
Copy link
Member

zhuxiujia commented Nov 6, 2023

一个小建议

目前我在使用中发现不支持这种写法,逻辑上都是解析到结构体中,

对于这种单个字段的.希望能直接返回 Vec,像下面这样

 pysql!(query_all_ids(rb: &dyn rbatis::executor::Executor)-> Result<Vec<u64>, rbatis::Error> => "
    select id from biz_activity
");

(另外,rbatis代码对我来说写的很好,我在一边使用,一边阅读,学到了很多东西,感谢~)

返回数据是

2023-11-06 16:51:54.732407  INFO  [rbatis]  query <= len=1,rows=[{"id":"2"},{"id":"3"},{"id":"4"}]

因此Vec 很难被智能解析,所以我建议你使用数组+结构体
例如

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Table {
    pub id: String,
}

pysql!(query_all_ids(rb: &dyn rbatis::executor::Executor)-> Result<Vec<Table>, rbatis::Error> => "
    select id from biz_activity

@zhuxiujia zhuxiujia added the enhancement New feature or request label Nov 6, 2023
@FoldingFan
Copy link
Contributor Author

是的,很难被解析,我fork了一份代码,想修改试试,emmmm

我想多了 /(ㄒoㄒ)/~~

@Issues-translate-bot
Copy link
Collaborator

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


Yes, it is difficult to parse. I forked a copy of the code and wanted to modify it. emmmm

I think too much /(ㄒoㄒ)/~~

@FoldingFan
Copy link
Contributor Author

也许有点绕,但是我通过学习大佬你的代码,通过这种方式转了过来 ,哈哈 🤣

/// 查询结果转为 Vec<u64>
#[macro_export]
macro_rules! pysql_to_vec_u64 {
    ($fn_name:ident($($param_key:ident:$param_type:ty$(,)?)*) => $py_file:expr) => {
        pub async fn $fn_name($($param_key: $param_type,)*) -> Result<Vec<u64>, rbatis::Error> {
            pub struct Inner{}
            impl Inner{
                pysql!($fn_name($($param_key:$param_type,)*)-> Result<Vec<rbs::Value>, rbatis::Error> => $py_file);
            }
            let result = Inner::$fn_name($($param_key,)*).await?;

            let vec_u64: Vec<u64> = result
                .iter()
                .map(|v| v.as_map().unwrap().0.get(0).unwrap().1.as_u64().unwrap())
                .collect();

            Ok(vec_u64)
        }
    };
}

@Issues-translate-bot
Copy link
Collaborator

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


Maybe it’s a bit convoluted, but I learned the code from you, and I came around this way, haha ​​🤣

/// Query results are converted to Vec<u64>
#[macro_export]
macro_rules! pysql_to_vec_u64 {
    ($fn_name:ident($($param_key:ident:$param_type:ty$(,)?)*) => $py_file:expr) => {
        pub async fn $fn_name($($param_key: $param_type,)*) -> Result<Vec<u64>, rbatis::Error> {
            pub struct Inner{}
            impl Inner{
                pysql!($fn_name($($param_key:$param_type,)*)-> Result<Vec<rbs::Value>, rbatis::Error> => $py_file);
            }
            let result = Inner::$fn_name($($param_key,)*).await?;

            let vec_u64: Vec<u64> = result
                .iter()
                .map(|v| v.as_map().unwrap().0.get(0).unwrap().1.as_u64().unwrap())
                .collect();

            Ok(vec_u64)
        }
    };
}

@zhuxiujia
Copy link
Member

unwrap()

建议 用unwrap_or_default() 否则unwrap 在空值导致panic

@Issues-translate-bot
Copy link
Collaborator

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


unwrap()

It is recommended to use unwrap_or_default() otherwise unwrap will cause panic on null values.

@FoldingFan
Copy link
Contributor Author

unwrap()

建议 用unwrap_or_default() 否则unwrap 在空值导致panic

好的,感谢指点

@Issues-translate-bot
Copy link
Collaborator

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


unwrap()

It is recommended to use unwrap_or_default() otherwise unwrap will cause panic on null values.

OK, thanks for the tip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants