2 ms·
This suggests a simple API using a Rust macro: fill_in_sql!(query_string, query_params) Where query_params is a structure containing the parameters:
by Animats 2y ago
This suggests a simple API using a Rust macro:
fill_in_sql!(query_string, query_params)
Where query_params is a structure containing the parameters:
struct some query {
name: &str,
id: u32,
}
The macro would parse the query string, find the variables to be filled in, match them to fields in the "params" structure, and generate the code to safely escape and fill in the params.
Any matching failures are found at compile time.
This is similar to what the usual "derive" macros do - examine a data structure at compile time and generate appropriate code.
- kelnos 2y agoI've used systems like this in Scala. It's actually pretty magical to write something like: val foos = sql"SELECT * from foos WHERE status = 'frobbed'".as[List[Foo]] ... and have the macro system parse the query at compile time and verify that the query will indeed return a `List[Foo]`. I think this is a nice middle ground, but constructing dynamic queries often can't be checked at compile-time. (I'm probably getting the syntax somewhat wrong; I haven't done Scala in quite a few years at this point.)