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
100
101
102
103
104
105
106
use crate::conn::AmConnCore;
use crate::protocol::parts::hdb_value::HdbValue;
use crate::protocol::parts::parameter_descriptor::ParameterDescriptor;
use crate::protocol::parts::parameter_descriptor::ParameterDescriptors;
use crate::protocol::util;
use crate::{HdbError, HdbResult};
use serde_db::de::DeserializableRow;
#[derive(Debug)]
pub struct OutputParameters {
descriptors: Vec<ParameterDescriptor>,
value_iter: <Vec<HdbValue<'static>> as IntoIterator>::IntoIter,
}
impl OutputParameters {
pub fn try_into<'de, T>(self) -> HdbResult<T>
where
T: serde::de::Deserialize<'de>,
{
trace!("OutputParameters::into_typed()");
Ok(DeserializableRow::into_typed(self)?)
}
pub fn descriptor(&self, i: usize) -> HdbResult<&ParameterDescriptor> {
trace!("OutputParameters::descriptor()");
Ok(self
.descriptors
.get(i)
.ok_or_else(|| HdbError::Usage("wrong index: no such parameter"))?)
}
pub fn descriptors(&self) -> &Vec<ParameterDescriptor> {
&(self.descriptors)
}
pub(crate) fn values(&self) -> &<Vec<HdbValue<'static>> as IntoIterator>::IntoIter {
&self.value_iter
}
pub fn values_mut(&mut self) -> &mut <Vec<HdbValue<'static>> as IntoIterator>::IntoIter {
&mut self.value_iter
}
pub(crate) fn parse<T: std::io::BufRead>(
o_am_conn_core: Option<&AmConnCore>,
parameter_descriptors: &ParameterDescriptors,
rdr: &mut T,
) -> std::io::Result<Self> {
trace!("OutputParameters::parse()");
let am_conn_core = o_am_conn_core
.ok_or_else(|| util::io_error("Cannot parse output parameters without am_conn_core"))?;
let mut descriptors = Vec::<ParameterDescriptor>::new();
let mut values = Vec::<HdbValue>::new();
for descriptor in parameter_descriptors.iter_out() {
trace!("Parsing value with descriptor {}", descriptor);
let value = HdbValue::parse_from_reply(
descriptor.type_id(),
descriptor.scale(),
descriptor.nullable(),
am_conn_core,
&None,
rdr,
)?;
trace!("Found value {:?}", value);
descriptors.push(descriptor.clone());
values.push(value);
}
Ok(Self {
descriptors,
value_iter: values.into_iter(),
})
}
}
impl std::fmt::Display for OutputParameters {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(fmt)?;
for parameter_descriptor in &self.descriptors {
write!(fmt, "{}, ", parameter_descriptor.name().unwrap_or(""))?;
}
writeln!(fmt)?;
for value in self.value_iter.as_slice() {
write!(fmt, "{}, ", &value)?;
}
writeln!(fmt)?;
Ok(())
}
}