Skip to content

Commit

Permalink
Merge branch 'dynlists' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed Jul 23, 2024
2 parents e0a130d + 7e50a47 commit aa6df69
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>) -> 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);
}

0 comments on commit aa6df69

Please sign in to comment.