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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use protocol::util;
use {HdbError, HdbResult};

use byteorder::{LittleEndian, ReadBytesExt};
use cesu8;
use std::fmt;
use std::io;
use std::u32;
use vec_map::VecMap;

/// Metadata for the fields in a result set.
#[derive(Clone, Debug)]
pub struct ResultSetMetadata {
    fields: Vec<FieldMetadata>,
    names: VecMap<String>,
}
impl ResultSetMetadata {
    /// Returns the number of fields.
    pub fn number_of_fields(&self) -> usize {
        self.fields.len()
    }

    /// Returns true if the set of fields is empty.
    pub fn is_empty(&self) -> bool {
        self.fields.len() == 0
    }

    fn add_to_names(&mut self, offset: u32) {
        if offset != u32::MAX {
            let tn = offset as usize;
            if !self.names.contains_key(tn) {
                self.names.insert(tn, "".to_string());
            };
        }
    }

    fn get(&self, index: usize) -> HdbResult<&FieldMetadata> {
        self.fields
            .get(index)
            .ok_or_else(|| HdbError::usage_("schemaname(): invalid field index"))
    }

    /// Database schema of the i'th column in the resultset.
    pub fn schemaname(&self, i: usize) -> HdbResult<&String> {
        Ok(self
            .names
            .get(self.get(i)?.schemaname_idx() as usize)
            .ok_or_else(|| HdbError::usage_("get_fieldname(): invalid field index"))?)
    }

    /// Database table of the i'th column in the resultset.
    pub fn tablename(&self, i: usize) -> HdbResult<&String> {
        Ok(self
            .names
            .get(self.get(i)?.tablename_idx() as usize)
            .ok_or_else(|| HdbError::usage_("tablename(): invalid field index"))?)
    }

    /// Name of the i'th column in the resultset.
    pub fn columnname(&self, i: usize) -> HdbResult<&String> {
        Ok(self
            .names
            .get(self.get(i)?.columnname_idx() as usize)
            .ok_or_else(|| HdbError::usage_("columnname(): invalid field index"))?)
    }

    // For large resultsets, this method will be called very often - is caching
    // meaningful?
    /// Display name of the column.
    #[inline]
    pub fn displayname(&self, index: usize) -> HdbResult<&String> {
        Ok(self
            .names
            .get(self.get(index)?.displayname_idx() as usize)
            .ok_or_else(|| HdbError::usage_("get_fieldname(): invalid field index"))?)
    }

    /// True if column can contain NULL values.
    pub fn is_nullable(&self, i: usize) -> HdbResult<bool> {
        Ok(self.get(i)?.is_nullable())
    }

    /// Returns true if the column has a default value.
    pub fn has_default(&self, i: usize) -> HdbResult<bool> {
        Ok(self.get(i)?.has_default())
    }
    // 3 = Escape_char
    // ???
    ///  Returns true if the column is readonly.
    pub fn is_readonly(&self, i: usize) -> HdbResult<bool> {
        Ok(self.get(i)?.is_readonly())
    }
    /// Returns true if the column is auto-incremented.
    pub fn is_auto_incremented(&self, i: usize) -> HdbResult<bool> {
        Ok(self.get(i)?.is_auto_incremented())
    }
    // 6 = ArrayType
    /// Returns true if the column is of array type.
    pub fn is_array_type(&self, i: usize) -> HdbResult<bool> {
        Ok(self.get(i)?.is_array_type())
    }

    /// Returns the id of the value type. See module
    /// `hdbconnect::metadata::type_id`.
    pub fn type_id(&self, i: usize) -> HdbResult<u8> {
        Ok(self.get(i)?.type_id())
    }

    /// Scale length (for some numeric types only).
    pub fn scale(&self, i: usize) -> HdbResult<i16> {
        Ok(self.get(i)?.scale())
    }

    /// Precision (for some numeric types only).
    pub fn precision(&self, i: usize) -> HdbResult<i16> {
        Ok(self.get(i)?.precision())
    }
}

// this just writes a headline with field names as it is handy in Display for
// ResultSet
impl fmt::Display for ResultSetMetadata {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        writeln!(fmt).unwrap();
        for field_metadata in &self.fields {
            match self.names.get(field_metadata.displayname_idx as usize) {
                Some(fieldname) => write!(fmt, "{}, ", fieldname).unwrap(),
                None => write!(fmt, "<unnamed>, ").unwrap(),
            };
        }
        Ok(())
    }
}

pub fn parse(count: i32, arg_size: u32, rdr: &mut io::BufRead) -> HdbResult<ResultSetMetadata> {
    let mut rsm = ResultSetMetadata {
        fields: Vec::<FieldMetadata>::new(),
        names: VecMap::<String>::new(),
    };
    trace!("Got count {}", count);
    for _ in 0..count {
        let column_options = rdr.read_u8()?; // U1 (documented as I1)
        let type_id = rdr.read_u8()?; // I1
        let scale = rdr.read_i16::<LittleEndian>()?; // I2
        let precision = rdr.read_i16::<LittleEndian>()?; // I2
        rdr.read_i16::<LittleEndian>()?; // I2
        let tablename_idx = rdr.read_u32::<LittleEndian>()?; // I4
        rsm.add_to_names(tablename_idx);
        let schemaname_idx = rdr.read_u32::<LittleEndian>()?; // I4
        rsm.add_to_names(schemaname_idx);
        let columnname_idx = rdr.read_u32::<LittleEndian>()?; // I4
        rsm.add_to_names(columnname_idx);
        let displayname_idx = rdr.read_u32::<LittleEndian>()?; // I4
        rsm.add_to_names(displayname_idx);

        let fm = FieldMetadata {
            column_options,
            type_id,
            scale,
            precision,
            tablename_idx,
            schemaname_idx,
            columnname_idx,
            displayname_idx,
        };
        rsm.fields.push(fm);
    }
    trace!("Read ResultSetMetadata phase 1: {:?}", rsm);
    // now we read the names
    let mut offset = 0;
    let limit = arg_size - (count as u32) * 22;
    trace!(
        "arg_size = {}, count = {}, limit = {} ",
        arg_size,
        count,
        limit
    );
    for _ in 0..rsm.names.len() {
        if offset >= limit {
            return Err(HdbError::Impl(
                "Error in reading ResultSetMetadata".to_owned(),
            ));
        }
        let nl = rdr.read_u8()?; // UI1
        let buffer: Vec<u8> = util::parse_bytes(nl as usize, rdr)?; // variable
        let name = cesu8::from_cesu8(&buffer)?;
        trace!("offset = {}, name = {}", offset, name);
        rsm.names.insert(offset as usize, name.to_string());
        offset += u32::from(nl) + 1;
    }
    Ok(rsm)
}

/// Describes a single field (column) in a result set.
#[derive(Clone, Debug)]
struct FieldMetadata {
    // Database schema.
    schemaname_idx: u32,
    // Database table.
    tablename_idx: u32,
    // Name of the column.
    columnname_idx: u32,
    // Display name of a column.
    displayname_idx: u32,
    // Column_options.
    // Bit pattern:
    // 0 = Mandatory
    // 1 = Optional
    // 2 = Default
    // 3 = Escape_char
    // 4 = Readonly
    // 5 = Autoincrement
    // 6 = ArrayType
    column_options: u8,
    // The id of the value type.
    type_id: u8,
    // scale length (for some numeric types only).
    scale: i16,
    // Precision (for some numeric types only).
    precision: i16,
}
impl FieldMetadata {
    // Database schema.
    pub fn schemaname_idx(&self) -> u32 {
        self.schemaname_idx
    }
    // Database table.
    pub fn tablename_idx(&self) -> u32 {
        self.tablename_idx
    }
    // Name of the column.
    pub fn columnname_idx(&self) -> u32 {
        self.columnname_idx
    }
    // Display name of a column.
    pub fn displayname_idx(&self) -> u32 {
        self.displayname_idx
    }
    // Returns true if the column can contain NULL values.
    pub fn is_nullable(&self) -> bool {
        (self.column_options & 0b_0000_0010_u8) != 0
    }
    // Returns true if the column has a default value.
    pub fn has_default(&self) -> bool {
        (self.column_options & 0b_0000_0100_u8) != 0
    }
    // 3 = Escape_char
    // ???
    //  Returns true if the column is readonly
    pub fn is_readonly(&self) -> bool {
        (self.column_options & 0b_0001_0000_u8) != 0
    }
    // Returns true if the column is auto-incremented.
    pub fn is_auto_incremented(&self) -> bool {
        (self.column_options & 0b_0010_0000_u8) != 0
    }
    // 6 = ArrayType
    pub fn is_array_type(&self) -> bool {
        (self.column_options & 0b_0100_0000_u8) != 0
    }

    /// The id of the value type.
    pub fn type_id(&self) -> u8 {
        self.type_id
    }
    /// Scale (for some numeric types only).
    pub fn scale(&self) -> i16 {
        self.scale
    }
    /// Precision (for some numeric types only).
    pub fn precision(&self) -> i16 {
        self.precision
    }
}