Replies: 9 comments 22 replies
-
I tried to tell the compiler that I am thinking about this solution:
Another alternative would be:
Yet another alternative:
I like the last of the three alternatives the most ATM, but I did not think about them too much (they are more-less just momentary thoughts), so I may have missed some problems (e.g. I can't get rid of the feeling that the Also, both of them look like a lot of typing, which should really be part of EDIT: It occured to me that there is also third alternative, so I added it (and adjusted the comment at the bottom accordingly). |
Beta Was this translation helpful? Give feedback.
-
We're aware of how.. frustrating it can be when trying to abstract code across multiple points. Most of the time, I tend to make functions take In the async fn some_function(mut executor: impl PgExecutor) {
// [...]
.fetch_one(&mut executor)
} and directly pass |
Beta Was this translation helpful? Give feedback.
-
WHEW I came here to post the exact same question. My goal was to be able to pass both For others who may stumble across this, I was able to solve this although for reasons I don't understand: This does not work for both (only for PgPool):
While this version, differing subtly only in parameter typing and trait bounds, does work:
In previously attempts, I had tried to change the I am not sophisticated enough to understand why this incredibly simple solution works (and especially why removal of lifetime annotation), and would be glad to hear from @mehcode or @jplatte if they are able to comment! |
Beta Was this translation helpful? Give feedback.
-
Any news pls? |
Beta Was this translation helpful? Give feedback.
-
Please look at the examples in https://docs.rs/sqlx/latest/sqlx/trait.Acquire.html, that should give you an answer. |
Beta Was this translation helpful? Give feedback.
-
This discussion comes so close to what I'm looking for and yet still doesn't bring an end to my hours of digging. I also want to have functions that accept
Accepting My next shot at this (thanks @mseele !) is from the Acquire page it appears the way to do this is to take a generic with Bonus: it'd be nice to go one level up still and make a trait for this imagined |
Beta Was this translation helpful? Give feedback.
-
use https://github.com/launchbadge/sqlx/blob/main/CHANGELOG.md#breaking under 7.0.7 |
Beta Was this translation helpful? Give feedback.
-
I just had to face this and it is hitting the limits of my understanding of RUST. Here is what worked for me: pub async fn insert_rec<'e>(ex: impl MySqlExecutor<'e>) -> Result<i32, Error> {
let sql = "insert into `table` (`col`) values(?)";
sqlx::query(sql).bind(col_val).execute(ex).await.map(|op|op.last_insert_id() as i32)
}
// inside test case:
let db_pool = MySqlPool::connect(database_url.as_str()).await.unwrap();
let mut tx = db_pool.begin().await.unwrap();
let id = insert_rec(tx.as_mut()).await.unwrap(); The context is I have a series of CRUD functions that I need to chain and I'd like to be able to use Transaction or Pool as function argument depending on context. |
Beta Was this translation helpful? Give feedback.
-
Thanks!, saved my life after a frustrating day of trying to make a generic exe_query function... now i just have two variants for mysql and for Pg, if anyone could can figure out how to make one like this work: pub async fn insert_rec<'e>(sql: String, ex: impl Executor<'e>) -> Result<i32, Error> {
sqlx::query(sql).bind(col_val).execute(ex).await.map(|op|op.last_insert_id() as i32)
} That would be awesome! |
Beta Was this translation helpful? Give feedback.
-
The code below pretty much says it (or use the link to the Rust playground if you prefer that). The pool has
Executor
implemented on immutable reference, transaction has it implemented on mutable reference (I do not know about others - connection?). I am trying to write single function that accepts them all, similar to e.g.QueryAs::fetch_one
.I am running into problems with borrow checker when I need to run more than single query inside such function. The
E
type below indeed is a reference in both cases (pool, transaction) and therefore it should be borrowed, but I do not seem to be able to tell it to the compiler.Any help anyone please?
Beta Was this translation helpful? Give feedback.
All reactions