Hi to all gusy ,
I'am new here.. I am a programmer working in c# with experience in .cpp and C as my background.
recently i have heard of Rust and my curiosity to understand what's the state of work on it.
so I decide to install the compiler and try some work specifically with database , because , at least in my opinion, this is a crucial node.
so what's the problem i stumble on and why i ask your help ?.. well ..this is my code ..
use sqlx::{Error, FromRow, MySql, MySqlConnection, MySqlPool, Pool};
#[derive(FromRow, Debug)]
#[sqlx(rename_all = "camelCase")]
pub struct Tabmag {
tmAnnu: String,
tmPref: String,
tmKeyf: String,
tmDesc: String,
tmCsv: String,
tmDati: String,
}
pub struct TabmagFilter {
pm_zone: String,
pm_country: String,
pm_datemin: i32,
pm_datemax: i32,
}
pub async fn get_tabmag_all(db_ctx: Pool<MySql>, filter: TabmagFilter) -> Result<Vec<Tabmag>, Error> {
let mut sql = String::from("SELECT * FROM tabmag WHERE 1=1"); // Starting with a dummy condition
let mut params = Vec::new(); // vec[];
if !filter.pm_zone.is_empty() {
sql.push_str(" AND tmzone = ?");
params.push(filter.pm_zone);
}
if filter.pm_datemin > 0 {
sql.push_str(" AND tmdate >= ? AND tmdate <= ?");
params.push(filter.pm_datemin.to_string());
params.push(filter.pm_datemax.to_string());
}
// Add other filters as needed
if !filter.pm_country.is_empty() {
sql.push_str(" AND tmcountry = ?");
params.push(filter.pm_country);
}
sql.push_str(" ORDER BY tmcountry, tmdate");
let result = sqlx::query_as(&sql)
.bind(params) // <--- ERROR
.fetch_all(&db_ctx)
.await?;
Ok(result)
}
I tryed to write a query with medium complex filter ( as in the real life ) to see how is sqlx support about it.
when I compile this code i get
the trait bound `Vec<String>: sqlx::Encode<'_, _>` is not satisfied
the trait `sqlx::Encode<'_, MySql>` is implemented for `Vec<u8>`
for that trait implementation, expected `u8`, found `String`
I read these link what the problem could be
https://users.rust-lang.org/t/how-to-create-vec-for-sqlx-values/66505
https://stackoverflow.com/questions/70029671/how-to-query-using-an-in-clause-and-a-vec-as-parameter-in-rust-sqlx-for-mysql
what is the correct way to add a Vec to bind to the query ??
also i read that the current support ( in sqlx ) for FromRow is limited and stated i have tables with 30-50 ..and more than 60 field i should think to create my own implementation of FromRow ...how to do that ?? could i have any suggestion ??
thank for your help