I'm going to do a lot of mysql queries, and I'm looking for a more flexible approach to this:
// Let's select payments from database. Type inference should do the trick here.
let selected_payments = conn
.query_map(
"SELECT customer_id, amount, account_name from payment",
|(customer_id, amount, account_name)| {
Payment { customer_id, amount, account_name }
},
)?;
The problem here is that
- I have to explicitly specify which columns I want to retrieve.
- I have to manually do the mapping.
It would be cool if the mysql crate could match the resultset columns with the struct's fields automatically. Is there any way to achieve something similar? I know Rust doesn't have reflection, but diesel can do it, so I guess there is a way.
If it's not possible and I have to do the struct instantiaton manually, can I access the resultset fields by name, instead of accessing a tuple? Imagine I want to SELECT * FROM payment, and acces a few columns by name instead of position in the tuple (I know I'm being a bit lazy here, but I just want to avoid errors the current approach may involve).
Any hint appreciated, thanks!