Writing functions
TODO This chapter needs to be written
In bigger projects you need to structure your code with functions.
There as two common ways, each with different tradeoffs
- Pass the database driver to the function
- Pass ToqlApi to the function
Passing the database
If you decide to pass the database you give up on database independence, but less trait bounds are needed:
For MySQL this looks like this:
use toql::prelude::ToqlApi;
use toql_mysql_async::prelude::{MySqlAsync, Queryable};
fn do_stuff<C>(toql: &mut MySqlAsync<'_,C>)
where C:Queryable -> Result
{
let q = query!(...)
let users = toql.load_many(&q).await?;
toql.insert_many(users, paths!(top)).await?;
toql.update_many(users, fields!(top)).await?;
toql.delete_many(q).await?;
}
The Queryable
trait makes the MySqlAsync
work with a connection or a transaction.
Database independed functions
It's also possible to pass a struct that implements ToqlApi
.
However this requires more trait bounds to satisfy the bounds on ToqlApi
.
Unfortunately rust Rust compiler has a problem with associated type bounds, so it looks more complicated than it had to be.