1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::borrow::Borrow;
use {Column, Value};
use myc;
pub struct ParamParser<'a>(pub(crate) &'a [u8]);
impl<'a> ParamParser<'a> {
pub fn iter<I, E>(&self, types: I) -> Params<'a, <I as IntoIterator>::IntoIter>
where
I: IntoIterator<Item = E>,
E: Borrow<Column>,
{
Params {
input: self.0,
nullmap: None,
typmap: None,
types: types.into_iter(),
col: 0,
}
}
}
pub struct Params<'a, I> {
input: &'a [u8],
nullmap: Option<&'a [u8]>,
typmap: Option<&'a [u8]>,
types: I,
col: usize,
}
impl<'a, I, E> Iterator for Params<'a, I>
where
I: Iterator<Item = E> + ExactSizeIterator,
E: Borrow<Column>,
{
type Item = Value<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.nullmap.is_none() {
let nullmap_len = (self.types.len() + 7) / 8;
let (nullmap, rest) = self.input.split_at(nullmap_len);
self.nullmap = Some(nullmap);
self.input = rest;
if !rest.is_empty() && rest[0] != 0x00 {
let (typmap, rest) = rest[1..].split_at(2 * self.types.len());
self.typmap = Some(typmap);
self.input = rest;
}
}
let pt = self.types.next()?;
if let Some(nullmap) = self.nullmap {
let byte = self.col / 8;
if byte >= nullmap.len() {
return None;
}
if (nullmap[byte] & 1u8 << (self.col % 8)) != 0 {
self.col += 1;
return Some(Value::null());
}
} else {
unreachable!();
}
let pt = self.typmap
.map(|typmap| {
(
myc::constants::ColumnType::from(typmap[2 * self.col]),
(typmap[2 * self.col + 1] & 128) != 0,
)
})
.unwrap_or_else(move || {
let pt = pt.borrow();
(
pt.coltype,
pt.colflags
.contains(myc::constants::ColumnFlags::UNSIGNED_FLAG),
)
});
self.col += 1;
Some(Value::parse_from(&mut self.input, pt.0, pt.1).unwrap())
}
}