pub trait SQParam {
// Required method
fn append_param(&self, q: &mut Vec<u8>) -> usize;
}
Expand description
An SQParam
should be implemented by any type that is expected to be used as a parameter
§Example implementation
Say you have a custom type which has to store <username>-<id>
in your database and your database schema looks like
create model myspace.mymodel(username: string, id: uint64, password: string)
. You can do that directly:
use skytable::{query, query::SQParam};
struct MyType {
username: String,
id: u64,
password: String,
}
impl MyType {
fn new(username: String, id: u64, password: String) -> Self {
Self { username, id, password }
}
}
impl SQParam for MyType {
fn append_param(&self, buf: &mut Vec<u8>) -> usize {
self.username.append_param(buf) +
self.id.append_param(buf) +
self.password.append_param(buf)
}
}
// You can now directly do this!
let query = query!("insert into myspace.mymodel(?, ?, ?)", MyType::new("sayan".to_owned(), 0, "pass123".to_owned()));
assert_eq!(query.param_cnt(), 3);
// You can also used it beside normal params
// assume schema is `create model mymomdel2(uname: string, id: uint64, pass: string, age: uint8)`
let query = query!("insert into myspace.mymodel(?, ?, ?, ?)", MyType::new("sayan".to_owned(), 0, "pass123".to_owned()), 101);
assert_eq!(query.param_cnt(), 4);
Required Methods§
sourcefn append_param(&self, q: &mut Vec<u8>) -> usize
fn append_param(&self, q: &mut Vec<u8>) -> usize
Append this element to the raw parameter buffer
Return the number of parameters appended (see example above)