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
Comments
返回数据是 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 |
是的,很难被解析,我fork了一份代码,想修改试试,emmmm 我想多了 /(ㄒoㄒ)/~~ |
Yes, it is difficult to parse. I forked a copy of the code and wanted to modify it. emmmm I think too much /(ㄒoㄒ)/~~ |
也许有点绕,但是我通过学习大佬你的代码,通过这种方式转了过来 ,哈哈 🤣 /// 查询结果转为 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)
}
};
} |
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)
}
};
} |
建议 用unwrap_or_default() 否则unwrap 在空值导致panic |
It is recommended to use unwrap_or_default() otherwise unwrap will cause panic on null values. |
好的,感谢指点 |
OK, thanks for the tip |
一个小建议
目前我在使用中发现不支持这种写法,逻辑上都是解析到结构体中,
对于这种单个字段的.希望能直接返回 Vec,像下面这样
(另外,rbatis代码对我来说写的很好,我在一边使用,一边阅读,学到了很多东西,感谢~)
The text was updated successfully, but these errors were encountered: