I'm trying to run a stored procedure of MySQL DB.
I'm able to handle when the SP returns only one table as a result. But when multiple tables are being returned as a result, then I am facing issues.
File_1.rs:
use sqlx::mysql::MySqlArguments;
use sqlx::mysql::MySqlPoolOptions;
use sqlx::mysql::MySqlRow;
use sqlx::query::Query;
use sqlx::MySql;
use sqlx::Pool;
pub async fn run_sp(
query: Query<'_, MySql, MySqlArguments>,
) -> Result<Vec<MySqlRow>, &'static str> {
let pool: Pool<MySql> = MySqlPoolOptions::new()
.max_connections(5)
.connect("mysql://username:password@localhost:3306/database_name")
.await
.map_err(|_| "Failed to connect to MySQL database.")?;
match query.fetch_all(&pool).await {
Ok(rows) => Ok(rows),
Err(err) => {
eprintln!("Error: {}", err);
Err("Error.")
}
}
}
File_2.rs:
let query_string = format!(
"CALL {}(?, ?, ?)",
sp_constants::SP_NAME
);
let query = query::<MySql>(&query_string)
.bind(param_1)
.bind(param_2)
.bind(param_3);
let rows: Vec<MySqlRow> = match run_sp(query).await {
Ok(result) => result,
Err(_) => vec![],
};
As you can see, in the code provided above, I'm setting query in File_2.rs and returning the result from File_1.rs.
Currently I'm getting five tables as a result. Each table has only one row, but contains two to five columns.
How can I read data from each table and assign them to five different variables whose data-types are struct (for ref. see code-snippet provided below)?
// example snippet
#[derive(Debug, Deserialize, Serialize)]
pub struct UserDetails{
pub id: i32,
pub name: String
}
let user_details: UserDetails;