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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use crate::protocol::parts::type_id::TypeId;
use crate::protocol::util;
use crate::{HdbResult, HdbValue};
use byteorder::{LittleEndian, ReadBytesExt};
#[derive(Debug)]
pub struct ParameterDescriptors(Vec<ParameterDescriptor>);
impl ParameterDescriptors {
pub(crate) fn new() -> Self {
Self(Vec::new())
}
pub fn iter_in(&self) -> impl std::iter::Iterator<Item = &ParameterDescriptor> {
self.0.iter().filter(|ms| {
(ms.direction == ParameterDirection::IN) | (ms.direction == ParameterDirection::INOUT)
})
}
pub fn iter_out(&self) -> impl std::iter::Iterator<Item = &ParameterDescriptor> {
self.0.iter().filter(|ms| {
(ms.direction == ParameterDirection::OUT) | (ms.direction == ParameterDirection::INOUT)
})
}
pub fn has_in(&self) -> bool {
self.iter_in().next().is_some()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub(crate) fn parse(count: usize, rdr: &mut dyn std::io::Read) -> std::io::Result<Self> {
let mut vec_pd = Vec::<ParameterDescriptor>::new();
let mut name_offsets = Vec::<u32>::new();
for _ in 0..count {
let option = rdr.read_u8()?;
let value_type = rdr.read_u8()?;
let mode = ParameterDescriptor::direction_from_u8(rdr.read_u8()?)?;
rdr.read_u8()?;
name_offsets.push(rdr.read_u32::<LittleEndian>()?);
let length = rdr.read_i16::<LittleEndian>()?;
let fraction = rdr.read_i16::<LittleEndian>()?;
rdr.read_u32::<LittleEndian>()?;
vec_pd.push(ParameterDescriptor::try_new(
option, value_type, mode, length, fraction,
)?);
}
for (descriptor, name_offset) in vec_pd.iter_mut().zip(name_offsets.iter()) {
if name_offset != &u32::max_value() {
let length = rdr.read_u8()?;
let name = util::string_from_cesu8(util::parse_bytes(length as usize, rdr)?)
.map_err(util::io_error)?;
descriptor.set_name(name);
}
}
Ok(Self(vec_pd))
}
}
#[derive(Clone, Debug)]
pub struct ParameterDescriptor {
name: Option<String>,
type_id: TypeId,
binding: ParameterBinding,
scale: i16,
precision: i16,
direction: ParameterDirection,
auto_incremented: bool,
array_type: bool,
}
impl ParameterDescriptor {
fn try_new(
parameter_option: u8,
type_code: u8,
direction: ParameterDirection,
precision: i16,
scale: i16,
) -> std::io::Result<Self> {
let type_id = TypeId::try_new(type_code)?;
let (binding, auto_incremented, array_type) = evaluate_option(parameter_option);
Ok(Self {
binding,
type_id,
direction,
precision,
scale,
name: None,
auto_incremented,
array_type,
})
}
pub fn binding(&self) -> ParameterBinding {
self.binding
}
pub fn is_nullable(&self) -> bool {
matches!(self.binding, ParameterBinding::Optional)
}
pub fn has_default(&self) -> bool {
matches!(self.binding, ParameterBinding::HasDefault)
}
pub fn is_auto_incremented(&self) -> bool {
self.auto_incremented
}
pub fn is_array_type(&self) -> bool {
self.array_type
}
pub fn type_id(&self) -> TypeId {
self.type_id
}
#[deprecated(since = "0.22.1", note = "use ParameterDescriptor::is_nullable()")]
pub fn nullable(&self) -> bool {
self.is_nullable()
}
pub fn scale(&self) -> i16 {
self.scale
}
pub fn precision(&self) -> i16 {
self.precision
}
pub fn direction(&self) -> ParameterDirection {
self.direction.clone()
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
fn set_name(&mut self, name: String) {
self.name = Some(name);
}
fn direction_from_u8(v: u8) -> std::io::Result<ParameterDirection> {
match v {
1 => Ok(ParameterDirection::IN),
2 => Ok(ParameterDirection::INOUT),
4 => Ok(ParameterDirection::OUT),
_ => Err(util::io_error(format!(
"invalid value for ParameterDirection: {}",
v
))),
}
}
pub fn parse_value<S: AsRef<str>>(&self, s: S) -> HdbResult<HdbValue<'static>> {
Ok(serde_db::ser::DbvFactory::from_str(&self, s.as_ref())?)
}
}
fn evaluate_option(parameter_option: u8) -> (ParameterBinding, bool, bool) {
(
if parameter_option & 0b_0000_0001_u8 > 0 {
ParameterBinding::Mandatory
} else if parameter_option & 0b_0000_0010_u8 > 0 {
ParameterBinding::Optional
} else {
if parameter_option & 0b_0000_0010_u8 == 0 {
log::warn!("ParameterDescriptor got invalid parameter_option, assuming HasDefault");
}
ParameterBinding::HasDefault
},
(parameter_option & 0b_0010_0000_u8) != 0,
(parameter_option & 0b_0100_0000_u8) != 0,
)
}
impl std::fmt::Display for ParameterDescriptor {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(ref s) = self.name {
write!(fmt, "{} ", s,)?;
}
write!(
fmt,
"{:?} {:?} {:?}, Scale({}), Precision({})",
self.type_id,
self.binding(),
self.direction(),
self.precision(),
self.scale()
)?;
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ParameterBinding {
Optional,
Mandatory,
HasDefault,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ParameterDirection {
IN,
INOUT,
OUT,
}