diff --git a/src/query.rs b/src/query.rs index 3254c9b..99f5fce 100644 --- a/src/query.rs +++ b/src/query.rs @@ -390,3 +390,42 @@ impl SQParam for String { self.as_str().append_param(buf) } } + +const LIST_SYM_OPEN: u8 = 0x09; +const LIST_SYM_CLOSE: u8 = ']' as u8; + +/// A list type representing a Skyhash list type, used in parameter lists +pub struct List<'a, T: SQParam> { + l: &'a [T], +} + +impl<'a, T: SQParam> List<'a, T> { + /// create a new list + pub fn new(l: &'a [T]) -> Self { + Self { l } + } +} + +impl<'a, T: SQParam> SQParam for List<'a, T> { + fn append_param(&self, q: &mut Vec) -> usize { + q.push(LIST_SYM_OPEN); + for param in self.l { + param.append_param(q); + } + q.push(LIST_SYM_CLOSE); + 1 + } +} + +#[test] +fn list_param() { + let data = vec!["hello", "giant", "world"]; + let list = List::new(&data); + let q = query!( + "insert into apps.social(?, ?, ?)", + "username", + "password", + list + ); + assert_eq!(q.param_cnt(), 3); +}