pub trait IntoParams: Sealed { }Expand description
Converts some type into parameters that can be passed to libsql.
The trait is sealed and not designed to be implemented by hand but instead provides a few ways to use it.
§Passing parameters to libsql
Many functions in this library let you pass parameters to libsql. Doing this lets you avoid any risk of SQL injection, and is simpler than escaping things manually. These functions generally contain some paramter that generically accepts some implementation this trait.
§Positional parameters
These can be supplied in a few ways:
- For heterogeneous parameter lists of 16 or less items a tuple syntax is supported
by doing
(1, "foo"). - For hetergeneous parameter lists of 16 or greater, the [
libsql::params!] is supported by doinglibsql::params![1, "foo"]. - For homogeneous paramter types (where they are all the same type), const arrays are
supported by doing
[1, 2, 3].
§Example (positional)
let mut stmt = conn.prepare("INSERT INTO test (a, b) VALUES (?1, ?2)").await?;
// Using a tuple:
stmt.execute((0, "foobar")).await?;
// Using `libsql::params!`:
stmt.execute(params![1i32, "blah"]).await?;
// array literal — non-references
stmt.execute([2i32, 3i32]).await?;
// array literal — references
stmt.execute(["foo", "bar"]).await?;
// Slice literal, references:
stmt.execute([2i32, 3i32]).await?;
§Named paramters
- For heterogeneous parameter lists of 16 or less items a tuple syntax is supported
by doing
(("key1", 1), ("key2", "foo")). - For hetergeneous parameter lists of 16 or greater, the [
libsql::params!] is supported by doinglibsql::named_params!["key1": 1, "key2": "foo"]. - For homogeneous paramter types (where they are all the same type), const arrays are
supported by doing
[("key1", 1), ("key2, 2), ("key3", 3)].
§Example (named)
let mut stmt = conn.prepare("INSERT INTO test (a, b) VALUES (:key1, :key2)").await?;
// Using a tuple:
stmt.execute(((":key1", 0), (":key2", "foobar"))).await?;
// Using `libsql::named_params!`:
stmt.execute(named_params! {":key1": 1i32, ":key2": "blah" }).await?;
// const array:
stmt.execute([(":key1", 2i32), (":key2", 3i32)]).await?;